test_modules.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Jun 24 09:43:53 2014
  4. @author: pietro
  5. """
  6. import sys
  7. from fnmatch import fnmatch
  8. from grass.gunittest.case import TestCase
  9. from grass.gunittest.main import test
  10. from grass.script.core import get_commands
  11. from grass.exceptions import ParameterError
  12. from grass.pygrass.modules.interface import Module
  13. PY2 = sys.version_info[0] == 2
  14. if PY2:
  15. from StringIO import StringIO
  16. else:
  17. from io import BytesIO as StringIO
  18. SKIP = [
  19. "g.parser",
  20. ]
  21. # taken from six
  22. def with_metaclass(meta, *bases):
  23. """Create a base class with a metaclass."""
  24. # This requires a bit of explanation: the basic idea is to make a dummy
  25. # metaclass for one level of class instantiation that replaces itself with
  26. # the actual metaclass.
  27. class metaclass(meta):
  28. def __new__(cls, name, this_bases, d):
  29. return meta(name, bases, d)
  30. return type.__new__(metaclass, "temporary_class", (), {})
  31. class ModulesMeta(type):
  32. def __new__(mcs, name, bases, dict):
  33. def gen_test(cmd):
  34. def test(self):
  35. Module(cmd)
  36. return test
  37. cmds = [
  38. c
  39. for c in sorted(list(get_commands()[0]))
  40. if c not in SKIP and not fnmatch(c, "g.gui.*")
  41. ]
  42. for cmd in cmds:
  43. test_name = "test__%s" % cmd.replace(".", "_")
  44. dict[test_name] = gen_test(cmd)
  45. return type.__new__(mcs, name, bases, dict)
  46. class TestModules(with_metaclass(ModulesMeta, TestCase)):
  47. pass
  48. class TestModulesPickability(TestCase):
  49. def test_rsun(self):
  50. """Test if a Module instance is pickable"""
  51. import pickle
  52. out = StringIO()
  53. pickle.dump(Module("r.sun"), out)
  54. out.close()
  55. class TestModulesCheck(TestCase):
  56. def test_flags_with_suppress_required(self):
  57. """Test if flags with suppress required are handle correctly"""
  58. gextension = Module("g.extension")
  59. # check if raise an error if required parameter are missing
  60. with self.assertRaises(ParameterError):
  61. gextension.check()
  62. # check if the flag suppress the required parameters
  63. gextension.flags.a = True
  64. self.assertIsNone(gextension.check())
  65. if __name__ == "__main__":
  66. test()