test_assertions.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 TestMapExistsAssertions(grass.gunittest.TestCase):
  157. # pylint: disable=R0904
  158. raster_cell = 'TestMapExistsAssertions_raster_cell'
  159. raster_dcell = 'TestMapExistsAssertions_raster_dcell'
  160. raster3d = 'TestMapExistsAssertions_raster3D'
  161. vector = 'TestMapExistsAssertions_vector'
  162. @classmethod
  163. def setUpClass(cls):
  164. cls.use_temp_region()
  165. cls.runModule('g.region', n=10, e=10, s=0, w=0, t=10, b=0, res=1)
  166. cls.runModule('r.mapcalc', expression=cls.raster_cell + ' = 1')
  167. cls.runModule('r.mapcalc', expression=cls.raster_dcell + ' = 1.0')
  168. cls.runModule('r3.mapcalc', expression=cls.raster3d + ' = 1.0')
  169. cls.runModule('v.edit', map=cls.vector, tool='create')
  170. @classmethod
  171. def tearDownClass(cls):
  172. cls.runModule('g.remove', flags='f',
  173. type=['raster', 'raster3d', 'vector'],
  174. name=[cls.raster_cell, cls.raster_dcell,
  175. cls.raster3d, cls.vector])
  176. cls.del_temp_region()
  177. def test_rast_cell_exists(self):
  178. self.assertRasterExists(self.raster_cell)
  179. def test_rast_dcell_exists(self):
  180. self.assertRasterExists(self.raster_dcell)
  181. def test_rast_does_not_exist(self):
  182. self.assertRaises(self.failureException,
  183. self.assertRasterExists,
  184. 'does_not_exists')
  185. def test_rast3d_exists(self):
  186. self.assertRaster3dExists(self.raster3d)
  187. def test_rast3d_does_not_exist(self):
  188. self.assertRaises(self.failureException,
  189. self.assertRaster3dExists,
  190. 'does_not_exists')
  191. def test_vect_exists(self):
  192. self.assertVectorExists(self.vector)
  193. def test_vect_does_not_exist(self):
  194. self.assertRaises(self.failureException,
  195. self.assertVectorExists,
  196. 'does_not_exists')
  197. def test_rast_does_not_exist_in_current_mapset(self):
  198. # expecting that there is elevation in PERMANENT
  199. # TODO: use skip decorator
  200. # TODO: add the same tests but for vect and rast3d
  201. self.assertRaises(self.failureException,
  202. self.assertRasterExists,
  203. 'elevation',
  204. msg="Rasters from different mapsets should be ignored")
  205. class TestFileAssertions(grass.gunittest.TestCase):
  206. # pylint: disable=R0904
  207. @classmethod
  208. def setUpClass(cls):
  209. # we expect WIND to be always present
  210. gisenv = gcore.gisenv()
  211. cls.existing_file = os.path.join(gisenv['GISDBASE'],
  212. gisenv['LOCATION_NAME'],
  213. 'PERMANENT', 'WIND')
  214. cls.emtpy_file = cls.__name__ + '_this_is_an_empty_file'
  215. open(cls.emtpy_file, 'w').close()
  216. cls.file_with_md5 = cls.__name__ + '_this_is_a_file_with_known_md5'
  217. file_content = 'Content of the file with known MD5.\n'
  218. with open(cls.file_with_md5, 'w') as f:
  219. f.write(file_content)
  220. # MD5 sum created using:
  221. # echo 'Content of the file with known MD5.' > some_file.txt
  222. # md5sum some_file.txt
  223. cls.file_md5 = '807bba4ffac4bb351bc3f27853009949'
  224. cls.file_with_same_content = cls.__name__ + '_file_with_same_content'
  225. with open(cls.file_with_same_content, 'w') as f:
  226. f.write(file_content)
  227. cls.file_with_different_content = cls.__name__ + '_file_with_different_content'
  228. with open(cls.file_with_different_content, 'w') as f:
  229. f.write(file_content + ' Something else here.')
  230. @classmethod
  231. def tearDownClass(cls):
  232. os.remove(cls.emtpy_file)
  233. os.remove(cls.file_with_md5)
  234. os.remove(cls.file_with_same_content)
  235. os.remove(cls.file_with_different_content)
  236. def test_assertFileExists(self):
  237. self.assertFileExists(filename=self.existing_file)
  238. self.assertRaises(self.failureException,
  239. self.assertFileExists,
  240. filename='this_one_does_not_exists')
  241. def test_assertFileExists_empty_file(self):
  242. self.assertFileExists(filename=self.emtpy_file, skip_size_check=True)
  243. self.assertRaises(self.failureException,
  244. self.assertFileExists,
  245. filename=self.emtpy_file)
  246. def test_assertFileMd5(self):
  247. self.assertFileMd5(filename=self.file_with_md5, md5=self.file_md5)
  248. self.assertRaises(self.failureException,
  249. self.assertFileMd5,
  250. filename=self.file_with_md5, md5='wrongmd5')
  251. def test_assertFilesEqualMd5(self):
  252. self.assertFilesEqualMd5(filename=self.file_with_md5,
  253. reference=self.file_with_same_content)
  254. self.assertRaises(self.failureException,
  255. self.assertFilesEqualMd5,
  256. filename=self.file_with_md5,
  257. reference=self.file_with_different_content)
  258. if __name__ == '__main__':
  259. grass.gunittest.test()