test_assertions.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests assertion methods.
  4. """
  5. import os
  6. import grass.script.core as gcore
  7. from grass.pygrass.modules import Module
  8. import grass.gunittest
  9. from grass.gunittest.gmodules import SimpleModule
  10. class TestTextAssertions(grass.gunittest.TestCase):
  11. # pylint: disable=R0904
  12. def test_assertLooksLike(self):
  13. self.assertLooksLike("Generated map is <elevation>",
  14. "Generated map is <...>")
  15. self.assertRaises(self.failureException,
  16. self.assertLooksLike,
  17. "Generated map is elevation.",
  18. "Generated map is <...>")
  19. self.assertLooksLike("Projection string: '+proj=longlat +datum=WGS84'",
  20. "Projection string: ...")
  21. def test_assertLooksLike_multiline(self):
  22. self.assertLooksLike("a=123\nb=456\nc=789",
  23. "a=...\nb=...\nc=...")
  24. def test_assertLooksLike_numbers(self):
  25. self.assertLooksLike("abc = 125521",
  26. "abc = 125...")
  27. self.assertLooksLike("abc = 689.156",
  28. "abc = 689...")
  29. self.assertLooksLike("abc = 689.159589",
  30. "abc = 689.15...")
  31. # this should fail accoring to the implementation
  32. # first three dots are considered as ellipses
  33. self.assertRaises(self.failureException,
  34. self.assertLooksLike,
  35. "abc = 689.159589",
  36. "abc = 689....")
  37. R_UNIVAR_ELEVATION_SUBSET = """n=2025000
  38. null_cells=0
  39. min=55.5787925720215
  40. max=156.329864501953
  41. """
  42. RANDOM_KEYVALUES = """abc=2025000
  43. aaa=55.5787925720215
  44. bbb=156.329864501953
  45. """
  46. R_INFO_ELEVATION_SUBSET = """rows=1350
  47. cols=1500
  48. cells=2025000
  49. datatype=FCELL
  50. """
  51. # r.info -gre map=elevation
  52. ELEVATION_MAPSET_DICT = {'mapset': 'PERMANENT'}
  53. # r.univar map=elevation
  54. ELEVATION_MINMAX = """min=55.5787925720215
  55. max=156.329864501953
  56. """
  57. # values rounded manually to maximal expected perecision
  58. ELEVATION_MINMAX_DICT = {'min': 55.58, 'max': 156.33}
  59. class TestAssertModuleKeyValue(grass.gunittest.TestCase):
  60. """Test usage of `assertModuleKeyValue` method."""
  61. # pylint: disable=R0904
  62. @classmethod
  63. def setUpClass(cls):
  64. cls.use_temp_region()
  65. cls.runModule(SimpleModule('g.region', raster='elevation'))
  66. @classmethod
  67. def tearDownClass(cls):
  68. cls.del_temp_region()
  69. def test_pygrass_module(self):
  70. """Test syntax with Module and required parameters as module"""
  71. module = Module('r.info', map='elevation', flags='gr',
  72. run_=False, finish_=True)
  73. self.assertModuleKeyValue(module,
  74. reference=dict(min=55.58, max=156.33),
  75. precision=0.01, sep='=')
  76. def test_pygrass_simple_module(self):
  77. """Test syntax with SimpleModule as module"""
  78. module = SimpleModule('r.info', map='elevation', flags='gr')
  79. self.assertModuleKeyValue(module,
  80. reference=dict(min=55.58, max=156.33),
  81. precision=0.01, sep='=')
  82. def test_direct_parameters(self):
  83. """Test syntax with module and its parameters as function parameters"""
  84. self.assertModuleKeyValue('r.info', map='elevation', flags='gr',
  85. reference=dict(min=55.58, max=156.33),
  86. precision=0.01, sep='=')
  87. def test_parameters_parameter(self):
  88. """Test syntax with module parameters in one parameters dictionary"""
  89. self.assertModuleKeyValue(module='r.info',
  90. parameters=dict(map='elevation', flags='gr'),
  91. reference=dict(min=55.58, max=156.33),
  92. precision=0.01, sep='=')
  93. class TestRasterMapAssertions(grass.gunittest.TestCase):
  94. # pylint: disable=R0904
  95. @classmethod
  96. def setUpClass(cls):
  97. cls.use_temp_region()
  98. # TODO: here we should actually not call self.runModule but call_module
  99. cls.runModule(SimpleModule('g.region', raster='elevation'))
  100. @classmethod
  101. def tearDownClass(cls):
  102. cls.del_temp_region()
  103. def test_assertRasterFitsUnivar(self):
  104. self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,
  105. precision=0.01)
  106. self.assertRaises(self.failureException,
  107. self.assertRasterFitsUnivar,
  108. 'aspect', R_UNIVAR_ELEVATION_SUBSET, precision=0.01)
  109. self.assertRaises(ValueError,
  110. self.assertRasterFitsUnivar,
  111. 'elevation', RANDOM_KEYVALUES)
  112. def test_assertRasterFitsInfo(self):
  113. self.assertRasterFitsInfo('elevation', R_INFO_ELEVATION_SUBSET)
  114. self.assertRaises(self.failureException,
  115. self.assertRasterFitsInfo,
  116. 'elev_lid792_1m', R_INFO_ELEVATION_SUBSET)
  117. self.assertRaises(ValueError,
  118. self.assertRasterFitsInfo,
  119. 'elevation', RANDOM_KEYVALUES)
  120. def test_common_values_info_univar(self):
  121. self.assertRasterFitsUnivar('elevation',
  122. ELEVATION_MINMAX, precision=0.01)
  123. self.assertRasterFitsInfo('elevation',
  124. ELEVATION_MINMAX, precision=0.01)
  125. def test_dict_as_parameter(self):
  126. """This also tests if we are using r.info -e flag and that precision is
  127. not required for strings.
  128. """
  129. self.assertRasterFitsInfo('elevation', ELEVATION_MAPSET_DICT)
  130. def test_assertRastersNoDifference(self):
  131. """Test basic usage of assertRastersNoDifference"""
  132. self.assertRastersNoDifference(actual='elevation',
  133. reference='elevation',
  134. precision=0, # this might need to be increased
  135. msg="The same maps should have no difference")
  136. self.assertRaises(self.failureException,
  137. self.assertRastersNoDifference,
  138. actual='elevation',
  139. reference='aspect',
  140. precision=1,
  141. msg="Different maps should have difference")
  142. def test_assertRastersNoDifference_mean(self):
  143. """Test usage of assertRastersNoDifference with mean"""
  144. self.assertRastersNoDifference(actual='elevation',
  145. reference='elevation',
  146. precision=0, # this might need to be increased
  147. statistics=dict(mean=0),
  148. msg="The difference of same maps should have small mean")
  149. self.assertRaises(self.failureException,
  150. self.assertRastersNoDifference,
  151. actual='elevation',
  152. reference='aspect',
  153. precision=1,
  154. statistics=dict(mean=0),
  155. msg="The difference of different maps should have huge mean")
  156. class TestFileAssertions(grass.gunittest.TestCase):
  157. # pylint: disable=R0904
  158. @classmethod
  159. def setUpClass(cls):
  160. # we expect WIND to be always present
  161. gisenv = gcore.gisenv()
  162. cls.existing_file = os.path.join(gisenv['GISDBASE'],
  163. gisenv['LOCATION_NAME'],
  164. 'PERMANENT', 'WIND')
  165. cls.emtpy_file = cls.__name__ + '_this_is_an_empty_file'
  166. open(cls.emtpy_file, 'w').close()
  167. cls.file_with_md5 = cls.__name__ + '_this_is_a_file_with_known_md5'
  168. file_content = 'Content of the file with known MD5.\n'
  169. with open(cls.file_with_md5, 'w') as f:
  170. f.write(file_content)
  171. # MD5 sum created using:
  172. # echo 'Content of the file with known MD5.' > some_file.txt
  173. # md5sum some_file.txt
  174. cls.file_md5 = '807bba4ffac4bb351bc3f27853009949'
  175. cls.file_with_same_content = cls.__name__ + '_file_with_same_content'
  176. with open(cls.file_with_same_content, 'w') as f:
  177. f.write(file_content)
  178. cls.file_with_different_content = cls.__name__ + '_file_with_different_content'
  179. with open(cls.file_with_different_content, 'w') as f:
  180. f.write(file_content + ' Something else here.')
  181. @classmethod
  182. def tearDownClass(cls):
  183. os.remove(cls.emtpy_file)
  184. os.remove(cls.file_with_md5)
  185. os.remove(cls.file_with_same_content)
  186. os.remove(cls.file_with_different_content)
  187. def test_assertFileExists(self):
  188. self.assertFileExists(filename=self.existing_file)
  189. self.assertRaises(self.failureException,
  190. self.assertFileExists,
  191. filename='this_one_does_not_exists')
  192. def test_assertFileExists_empty_file(self):
  193. self.assertFileExists(filename=self.emtpy_file, skip_size_check=True)
  194. self.assertRaises(self.failureException,
  195. self.assertFileExists,
  196. filename=self.emtpy_file)
  197. def test_assertFileMd5(self):
  198. self.assertFileMd5(filename=self.file_with_md5, md5=self.file_md5)
  199. self.assertRaises(self.failureException,
  200. self.assertFileMd5,
  201. filename=self.file_with_md5, md5='wrongmd5')
  202. def test_assertFilesEqualMd5(self):
  203. self.assertFilesEqualMd5(filename=self.file_with_md5,
  204. reference=self.file_with_same_content)
  205. self.assertRaises(self.failureException,
  206. self.assertFilesEqualMd5,
  207. filename=self.file_with_md5,
  208. reference=self.file_with_different_content)
  209. if __name__ == '__main__':
  210. grass.gunittest.test()