test_gmodules.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. import subprocess
  3. from grass.gunittest.case import TestCase
  4. from grass.gunittest.main import test
  5. from grass.gunittest.gmodules import (call_module, CalledModuleError)
  6. G_REGION_OUTPUT = """n=...
  7. s=...
  8. w=...
  9. e=...
  10. nsres=...
  11. ewres=...
  12. rows=...
  13. cols=...
  14. cells=...
  15. """
  16. class TestCallModuleFunction(TestCase):
  17. def test_output(self):
  18. output = call_module('g.region', flags='pg')
  19. self.assertLooksLike(output, G_REGION_OUTPUT)
  20. def test_input_output(self):
  21. output = call_module('m.proj', flags='i', input='-', stdin="50.0 41.5")
  22. self.assertLooksLike(output, '...|...\n')
  23. def test_no_output(self):
  24. output = call_module('m.proj', flags='i', input='-', stdin="50.0 41.5",
  25. capture_stdout=False)
  26. self.assertIsNone(output)
  27. def test_merge_stderr(self):
  28. output = call_module('m.proj', flags='i', input='-', stdin="50.0 41.5",
  29. verbose=True,
  30. merge_stderr=True)
  31. self.assertLooksLike(output, '...+proj=longlat +datum=WGS84...')
  32. self.assertLooksLike(output, '...|...\n')
  33. def test_merge_stderr_with_wrong_stdin_stderr(self):
  34. self.assertRaises(ValueError,
  35. call_module,
  36. 'm.proj', flags='i', input='-', stdin="50.0 41.5",
  37. verbose=True,
  38. merge_stderr=True, capture_stdout=False)
  39. self.assertRaises(ValueError,
  40. call_module,
  41. 'm.proj', flags='i', input='-', stdin="50.0 41.5",
  42. verbose=True,
  43. merge_stderr=True, capture_stderr=False)
  44. self.assertRaises(ValueError,
  45. call_module,
  46. 'm.proj', flags='i', input='-', stdin="50.0 41.5",
  47. verbose=True,
  48. merge_stderr=True,
  49. capture_stdout=False, capture_stderr=False)
  50. def test_wrong_module_params(self):
  51. self.assertRaises(CalledModuleError,
  52. call_module,
  53. 'g.region', aabbbccc='notexist')
  54. def test_module_input_param_wrong(self):
  55. self.assertRaises(ValueError,
  56. call_module,
  57. 'm.proj', flags='i', input='does_not_exist',
  58. stdin="50.0 41.5")
  59. def test_missing_stdin_with_input_param(self):
  60. self.assertRaises(ValueError,
  61. call_module,
  62. 'm.proj', flags='i', input='-')
  63. def test_wrong_usage_of_popen_like_interface(self):
  64. self.assertRaises(ValueError,
  65. call_module,
  66. 'm.proj', flags='i', input='-',
  67. stdin=subprocess.PIPE)
  68. self.assertRaises(TypeError,
  69. call_module,
  70. 'm.proj', flags='i', input='-', stdin="50.0 41.5",
  71. stdout='any_value_or_type_here')
  72. self.assertRaises(TypeError,
  73. call_module,
  74. 'm.proj', flags='i', input='-', stdin="50.0 41.5",
  75. stderr='any_value_or_type_here')
  76. if __name__ == '__main__':
  77. test()