test_gmodules.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import subprocess
  2. from grass.gunittest.case import TestCase
  3. from grass.gunittest.main import test
  4. from grass.gunittest.gmodules import call_module, CalledModuleError
  5. G_REGION_OUTPUT = """...
  6. 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(
  25. "m.proj", flags="i", input="-", stdin="50.0 41.5", capture_stdout=False
  26. )
  27. self.assertIsNone(output)
  28. def test_merge_stderr(self):
  29. output = call_module(
  30. "m.proj",
  31. flags="i",
  32. input="-",
  33. stdin="50.0 41.5",
  34. verbose=True,
  35. merge_stderr=True,
  36. )
  37. self.assertLooksLike(output, "...+proj=longlat +datum=WGS84...")
  38. self.assertLooksLike(output, "...|...\n")
  39. def test_merge_stderr_with_wrong_stdin_stderr(self):
  40. self.assertRaises(
  41. ValueError,
  42. call_module,
  43. "m.proj",
  44. flags="i",
  45. input="-",
  46. stdin="50.0 41.5",
  47. verbose=True,
  48. merge_stderr=True,
  49. capture_stdout=False,
  50. )
  51. self.assertRaises(
  52. ValueError,
  53. call_module,
  54. "m.proj",
  55. flags="i",
  56. input="-",
  57. stdin="50.0 41.5",
  58. verbose=True,
  59. merge_stderr=True,
  60. capture_stderr=False,
  61. )
  62. self.assertRaises(
  63. ValueError,
  64. call_module,
  65. "m.proj",
  66. flags="i",
  67. input="-",
  68. stdin="50.0 41.5",
  69. verbose=True,
  70. merge_stderr=True,
  71. capture_stdout=False,
  72. capture_stderr=False,
  73. )
  74. def test_wrong_module_params(self):
  75. self.assertRaises(
  76. CalledModuleError, call_module, "g.region", aabbbccc="notexist"
  77. )
  78. def test_module_input_param_wrong(self):
  79. self.assertRaises(
  80. ValueError,
  81. call_module,
  82. "m.proj",
  83. flags="i",
  84. input="does_not_exist",
  85. stdin="50.0 41.5",
  86. )
  87. def test_missing_stdin_with_input_param(self):
  88. self.assertRaises(ValueError, call_module, "m.proj", flags="i", input="-")
  89. def test_wrong_usage_of_popen_like_interface(self):
  90. self.assertRaises(
  91. ValueError,
  92. call_module,
  93. "m.proj",
  94. flags="i",
  95. input="-",
  96. stdin=subprocess.PIPE,
  97. )
  98. self.assertRaises(
  99. TypeError,
  100. call_module,
  101. "m.proj",
  102. flags="i",
  103. input="-",
  104. stdin="50.0 41.5",
  105. stdout="any_value_or_type_here",
  106. )
  107. self.assertRaises(
  108. TypeError,
  109. call_module,
  110. "m.proj",
  111. flags="i",
  112. input="-",
  113. stdin="50.0 41.5",
  114. stderr="any_value_or_type_here",
  115. )
  116. if __name__ == "__main__":
  117. test()