test_module_assertions.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. import copy
  3. import subprocess
  4. from grass.pygrass.modules import Module
  5. from grass.gunittest.gmodules import SimpleModule
  6. import grass.gunittest
  7. from grass.gunittest.gmodules import CalledModuleError
  8. class TestModuleAssertions(grass.gunittest.TestCase):
  9. """Test assertions using PyGRASS Module"""
  10. # pylint: disable=R0904
  11. def setUp(self):
  12. """Create two Module instances one correct and one with wrong map"""
  13. self.rinfo = Module('r.info', map='elevation', flags='g',
  14. stdout_=subprocess.PIPE, run_=False, finish_=True)
  15. self.rinfo_wrong = copy.deepcopy(self.rinfo)
  16. self.wrong_map = 'does_not_exists'
  17. self.rinfo_wrong.inputs['map'].value = self.wrong_map
  18. def test_runModule(self):
  19. """Correct and incorrect Module used in runModule()"""
  20. self.runModule(self.rinfo)
  21. self.assertTrue(self.rinfo.outputs['stdout'].value)
  22. self.assertRaises(CalledModuleError, self.runModule, self.rinfo_wrong)
  23. def test_assertModule(self):
  24. """Correct and incorrect Module used in assertModule()"""
  25. self.assertModule(self.rinfo)
  26. self.assertTrue(self.rinfo.outputs['stdout'].value)
  27. self.assertRaises(self.failureException, self.assertModule, self.rinfo_wrong)
  28. def test_assertModuleFail(self):
  29. """Correct and incorrect Module used in assertModuleFail()"""
  30. self.assertModuleFail(self.rinfo_wrong)
  31. stderr = self.rinfo_wrong.outputs['stderr'].value
  32. self.assertTrue(stderr)
  33. self.assertIn(self.wrong_map, stderr)
  34. self.assertRaises(self.failureException, self.assertModuleFail, self.rinfo)
  35. class TestSimpleModuleAssertions(grass.gunittest.TestCase):
  36. """Test assertions using SimpleModule"""
  37. # pylint: disable=R0904
  38. def setUp(self):
  39. """Create two SimpleModule instances one correct and one with wrong map
  40. """
  41. self.rinfo = SimpleModule('r.info', map='elevation', flags='g')
  42. self.rinfo_wrong = copy.deepcopy(self.rinfo)
  43. self.wrong_map = 'does_not_exists'
  44. self.rinfo_wrong.inputs['map'].value = self.wrong_map
  45. def test_runModule(self):
  46. """Correct and incorrect SimpleModule used in runModule()"""
  47. self.runModule(self.rinfo)
  48. self.assertTrue(self.rinfo.outputs['stdout'].value)
  49. self.assertRaises(CalledModuleError, self.runModule, self.rinfo_wrong)
  50. def test_assertModule(self):
  51. """Correct and incorrect SimpleModule used in assertModule()"""
  52. self.assertModule(self.rinfo)
  53. self.assertTrue(self.rinfo.outputs['stdout'].value)
  54. self.assertRaises(self.failureException, self.assertModule, self.rinfo_wrong)
  55. def test_assertModuleFail(self):
  56. """Correct and incorrect SimpleModule used in assertModuleFail()"""
  57. self.assertModuleFail(self.rinfo_wrong)
  58. stderr = self.rinfo_wrong.outputs['stderr'].value
  59. self.assertTrue(stderr)
  60. self.assertIn(self.wrong_map, stderr)
  61. self.assertRaises(self.failureException, self.assertModuleFail, self.rinfo)
  62. if __name__ == '__main__':
  63. grass.gunittest.test()