test_modules.py 2.2 KB

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