test_gmodules.py 3.2 KB

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