r_object_geometry_test.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. """
  2. Name: r_object_geometry_test
  3. Purpose: This script is to demonstrate a unit test for r.object.geometry
  4. module.
  5. """
  6. import os
  7. from grass.gunittest.case import TestCase
  8. from grass.gunittest.main import test
  9. testraster1 = """\
  10. north: 250000
  11. south: 200000
  12. east: 670000
  13. west: 610000
  14. rows: 4
  15. cols: 4
  16. 1 1 2 2
  17. 1 1 2 2
  18. 3 3 2 2
  19. 3 3 2 2
  20. """
  21. class TestObjectGeometryPixel(TestCase):
  22. """Test case for object geometry module"""
  23. # Setup variables to be used for outputs
  24. test_objects1 = "test_objects1"
  25. output_file_pixel = "output_file_pixel.csv"
  26. @classmethod
  27. def setUpClass(cls):
  28. """Imports test raster(s), ensures expected computational region and setup"""
  29. cls.runModule(
  30. "r.in.ascii",
  31. input="-",
  32. type="CELL",
  33. stdin_=testraster1,
  34. output=cls.test_objects1,
  35. )
  36. cls.use_temp_region()
  37. cls.runModule("g.region", raster=cls.test_objects1)
  38. @classmethod
  39. def tearDownClass(cls):
  40. """Remove the temporary region"""
  41. cls.del_temp_region()
  42. def tearDown(self):
  43. """Remove the outputs created from the object geometry module
  44. This is executed after each test run.
  45. """
  46. if os.path.isfile(self.output_file_pixel):
  47. os.remove(self.output_file_pixel)
  48. self.runModule("g.remove", flags="f", type="raster", name=self.test_objects1)
  49. def test_object_geometry_pixel(self):
  50. """Test to see if the outputs are created and are correct in pixel units"""
  51. # run the object geometry module
  52. self.assertModule(
  53. "r.object.geometry", input=self.test_objects1, output=self.output_file_pixel
  54. )
  55. # check to see if output file exists
  56. self.assertFileExists(self.output_file_pixel, msg="Output file does not exist")
  57. # check if the output file is equal to the reference file
  58. self.assertFilesEqualMd5(
  59. self.output_file_pixel,
  60. "data/file_pixel.csv",
  61. msg="Output file is not equal to reference file",
  62. )
  63. class TestObjectGeometryMeter(TestCase):
  64. """Test case for object geometry module"""
  65. # Setup variables to be used for outputs
  66. test_objects1 = "test_objects1"
  67. output_file_meter = "output_file_meter.csv"
  68. @classmethod
  69. def setUpClass(cls):
  70. """Imports test raster(s), ensures expected computational region and setup"""
  71. cls.runModule(
  72. "r.in.ascii",
  73. input="-",
  74. type="CELL",
  75. stdin_=testraster1,
  76. output=cls.test_objects1,
  77. )
  78. cls.use_temp_region()
  79. cls.runModule("g.region", raster=cls.test_objects1)
  80. @classmethod
  81. def tearDownClass(cls):
  82. """Remove the temporary region"""
  83. cls.del_temp_region()
  84. def tearDown(self):
  85. """Remove the outputs created from the object geometry module
  86. This is executed after each test run.
  87. """
  88. if os.path.isfile(self.output_file_meter):
  89. os.remove(self.output_file_meter)
  90. self.runModule("g.remove", flags="f", type="raster", name=self.test_objects1)
  91. def test_object_geometry_meter(self):
  92. """Test to see if the outputs are created and are correct in meter units"""
  93. # run the object geometry module
  94. self.assertModule(
  95. "r.object.geometry",
  96. input=self.test_objects1,
  97. output=self.output_file_meter,
  98. flags="m",
  99. )
  100. # check to see if output file exists
  101. self.assertFileExists(self.output_file_meter, msg="Output file does not exist")
  102. # check if the output file is equal to the reference file
  103. self.assertFilesEqualMd5(
  104. self.output_file_meter,
  105. "data/file_meter.csv",
  106. msg="Output file is not equal to reference file",
  107. )
  108. if __name__ == "__main__":
  109. test()