r_object_geometry_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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", input="-", stdin_=testraster1, output=cls.test_objects1
  31. )
  32. cls.use_temp_region()
  33. cls.runModule("g.region", raster=cls.test_objects1)
  34. @classmethod
  35. def tearDownClass(cls):
  36. """Remove the temporary region"""
  37. cls.del_temp_region()
  38. def tearDown(self):
  39. """Remove the outputs created from the object geometry module
  40. This is executed after each test run.
  41. """
  42. if os.path.isfile(self.output_file_pixel):
  43. os.remove(self.output_file_pixel)
  44. self.runModule("g.remove", flags="f", type="raster", name=self.test_objects1)
  45. def test_object_geometry_pixel(self):
  46. """Test to see if the outputs are created and are correct in pixel units"""
  47. # run the object geometry module
  48. self.assertModule(
  49. "r.object.geometry", input=self.test_objects1, output=self.output_file_pixel
  50. )
  51. # check to see if output file exists
  52. self.assertFileExists(self.output_file_pixel, msg="Output file does not exist")
  53. # check if the output file is equal to the reference file
  54. self.assertFilesEqualMd5(
  55. self.output_file_pixel,
  56. "data/file_pixel.csv",
  57. msg="Output file is not equal to reference file",
  58. )
  59. class TestObjectGeometryMeter(TestCase):
  60. """Test case for object geometry module"""
  61. # Setup variables to be used for outputs
  62. test_objects1 = "test_objects1"
  63. output_file_meter = "output_file_meter.csv"
  64. @classmethod
  65. def setUpClass(cls):
  66. """Imports test raster(s), ensures expected computational region and setup"""
  67. cls.runModule(
  68. "r.in.ascii", input="-", stdin_=testraster1, output=cls.test_objects1
  69. )
  70. cls.use_temp_region()
  71. cls.runModule("g.region", raster=cls.test_objects1)
  72. @classmethod
  73. def tearDownClass(cls):
  74. """Remove the temporary region"""
  75. cls.del_temp_region()
  76. def tearDown(self):
  77. """Remove the outputs created from the object geometry module
  78. This is executed after each test run.
  79. """
  80. if os.path.isfile(self.output_file_meter):
  81. os.remove(self.output_file_meter)
  82. self.runModule("g.remove", flags="f", type="raster", name=self.test_objects1)
  83. def test_object_geometry_meter(self):
  84. """Test to see if the outputs are created and are correct in meter units"""
  85. # run the object geometry module
  86. self.assertModule(
  87. "r.object.geometry",
  88. input=self.test_objects1,
  89. output=self.output_file_meter,
  90. flags="m",
  91. )
  92. # check to see if output file exists
  93. self.assertFileExists(self.output_file_meter, msg="Output file does not exist")
  94. # check if the output file is equal to the reference file
  95. self.assertFilesEqualMd5(
  96. self.output_file_meter,
  97. "data/file_meter.csv",
  98. msg="Output file is not equal to reference file",
  99. )
  100. if __name__ == "__main__":
  101. test()