test_gmodules.py 3.3 KB

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