test_start_command_functions.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. Tests of start_command function family (location independent)
  3. """
  4. from grass.gunittest.case import TestCase
  5. from grass.gunittest.main import test
  6. from grass.script.core import start_command, PIPE, run_command, write_command
  7. from grass.script.core import read_command, find_program
  8. from grass.script.utils import encode
  9. class TestPythonKeywordsInParameters(TestCase):
  10. """Tests additional underscore syntax which helps to avoid Python keywords
  11. It works the same for keywords, buildins and any names.
  12. """
  13. raster = b"does_not_exist"
  14. def test_prefixed_underscore(self):
  15. proc = start_command("g.region", _raster=self.raster, stderr=PIPE)
  16. stderr = proc.communicate()[1]
  17. self.assertNotIn(b"_raster", stderr)
  18. self.assertIn(
  19. self.raster, stderr, msg="Raster map name should appear in the error output"
  20. )
  21. def test_suffixed_underscore(self):
  22. proc = start_command("g.region", raster_=self.raster, stderr=PIPE)
  23. stderr = proc.communicate()[1]
  24. self.assertNotIn(b"raster_", stderr)
  25. self.assertIn(
  26. self.raster, stderr, msg="Raster map name should appear in the error output"
  27. )
  28. def test_multiple_underscores(self):
  29. proc = start_command("g.region", _raster_=self.raster, stderr=PIPE)
  30. stderr = proc.communicate()[1]
  31. returncode = proc.poll()
  32. self.assertEquals(returncode, 1)
  33. self.assertIn(b"raster", stderr)
  34. class TestPythonModuleWithUnicodeParameters(TestCase):
  35. """Tests if unicode works in parameters of Python modules
  36. This in fact tests also the `parser()` function (original motivation
  37. for this tests).
  38. Using g.search.module because it takes any option values.
  39. """
  40. def test_python_module_ascii(self):
  41. """This tests if Python module works"""
  42. run_command("g.search.modules", keyword="Priserny kun", encoding=None)
  43. def test_python_module_ascii_with_encoding(self):
  44. """This tests if Python module works with user-specified encoding"""
  45. run_command("g.search.modules", keyword="Priserny kun", encoding="utf-16")
  46. def test_python_module_czech_nonascii(self):
  47. """This likely fails on non-UTF-8 systems (i.e. MS Win)"""
  48. run_command("g.search.modules", keyword="Příšerný kůň")
  49. def test_python_module_czech_unicode(self):
  50. """This likely fails on non-UTF-8 systems (i.e. MS Win)"""
  51. run_command("g.search.modules", keyword="Příšerný kůň")
  52. class TestPythonModuleWithStdinStdout(TestCase):
  53. raster = "rand_raster"
  54. @classmethod
  55. def setUpClass(cls):
  56. cls.runModule("g.region", s=0, n=10, w=0, e=10, res=1)
  57. cls.runModule(
  58. "r.mapcalc",
  59. expression="{} = rand(1, 5)".format(cls.raster),
  60. seed=1,
  61. overwrite=True,
  62. )
  63. @classmethod
  64. def tearDownClass(cls):
  65. cls.runModule("g.remove", type="raster", name=cls.raster, flags="f")
  66. def test_write_labels_unicode(self):
  67. """This tests if Python module works"""
  68. find_program("ls", "--version")
  69. write_command(
  70. "r.category",
  71. map=self.raster,
  72. rules="-",
  73. stdin="1:kůň\n2:kráva\n3:ovečka\n4:býk",
  74. separator=":",
  75. )
  76. res = read_command("r.category", map=self.raster, separator=":").strip()
  77. self.assertEquals(res, "1:kůň\n2:kráva\n3:ovečka\n4:býk")
  78. self.assertIsInstance(res, str)
  79. def test_write_labels_bytes(self):
  80. """This tests if Python module works"""
  81. write_command(
  82. "r.category",
  83. map=self.raster,
  84. rules="-",
  85. stdin="1:kůň\n2:kráva\n3:ovečka\n4:býk",
  86. separator=":",
  87. encoding=None,
  88. )
  89. res = read_command(
  90. "r.category", map=self.raster, separator=":", encoding=None
  91. ).strip()
  92. self.assertEquals(res, encode("1:kůň\n2:kráva\n3:ovečka\n4:býk"))
  93. self.assertIsInstance(res, bytes)
  94. if __name__ == "__main__":
  95. test()