test_assertions.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. def do_all_combidnations(self, first, second):
  38. self.assertMultiLineEqual(first, first)
  39. self.assertMultiLineEqual(first, second)
  40. self.assertMultiLineEqual(second, first)
  41. self.assertMultiLineEqual(second, second)
  42. def test_assertMultiLineEqual(self):
  43. unix_end = "aaa\nbbb\n"
  44. mswindows_end = "aaa\r\nbbb\r\n"
  45. self.do_all_combidnations(unix_end, mswindows_end)
  46. def test_assertMultiLineEqual_raises(self):
  47. """Test mixed line endings"""
  48. self.assertRaises(self.failureException,
  49. self.assertMultiLineEqual,
  50. "aaa\n\rbbb\r",
  51. "aaa\nbbb\n")
  52. def test_assertEqual(self):
  53. """Test for strings (uses overwritten assertMultiLineEqual())"""
  54. unix_end = "aaa\nbbb\n"
  55. mswindows_end = "aaa\r\nbbb\r\n"
  56. self.do_all_combidnations(unix_end, mswindows_end)
  57. self.assertRaises(self.failureException,
  58. self.assertEqual,
  59. "aaa\n\rbbb\r",
  60. "aaa\nbbb\n")
  61. R_UNIVAR_ELEVATION_SUBSET = """n=2025000
  62. null_cells=0
  63. min=55.5787925720215
  64. max=156.329864501953
  65. """
  66. RANDOM_KEYVALUES = """abc=2025000
  67. aaa=55.5787925720215
  68. bbb=156.329864501953
  69. """
  70. R_INFO_ELEVATION_SUBSET = """rows=1350
  71. cols=1500
  72. cells=2025000
  73. datatype=FCELL
  74. """
  75. # r.info -gre map=elevation
  76. ELEVATION_MAPSET_DICT = {'mapset': 'PERMANENT'}
  77. # r.univar map=elevation
  78. ELEVATION_MINMAX = """min=55.5787925720215
  79. max=156.329864501953
  80. """
  81. # values rounded manually to maximal expected perecision
  82. ELEVATION_MINMAX_DICT = {'min': 55.58, 'max': 156.33}
  83. class TestAssertModuleKeyValue(grass.gunittest.TestCase):
  84. """Test usage of `assertModuleKeyValue` method."""
  85. # pylint: disable=R0904
  86. @classmethod
  87. def setUpClass(cls):
  88. cls.use_temp_region()
  89. cls.runModule(SimpleModule('g.region', raster='elevation'))
  90. @classmethod
  91. def tearDownClass(cls):
  92. cls.del_temp_region()
  93. def test_pygrass_module(self):
  94. """Test syntax with Module and required parameters as module"""
  95. module = Module('r.info', map='elevation', flags='gr',
  96. run_=False, finish_=True)
  97. self.assertModuleKeyValue(module,
  98. reference=dict(min=55.58, max=156.33),
  99. precision=0.01, sep='=')
  100. def test_pygrass_simple_module(self):
  101. """Test syntax with SimpleModule as module"""
  102. module = SimpleModule('r.info', map='elevation', flags='gr')
  103. self.assertModuleKeyValue(module,
  104. reference=dict(min=55.58, max=156.33),
  105. precision=0.01, sep='=')
  106. def test_direct_parameters(self):
  107. """Test syntax with module and its parameters as function parameters"""
  108. self.assertModuleKeyValue('r.info', map='elevation', flags='gr',
  109. reference=dict(min=55.58, max=156.33),
  110. precision=0.01, sep='=')
  111. def test_parameters_parameter(self):
  112. """Test syntax with module parameters in one parameters dictionary"""
  113. self.assertModuleKeyValue(module='r.info',
  114. parameters=dict(map='elevation', flags='gr'),
  115. reference=dict(min=55.58, max=156.33),
  116. precision=0.01, sep='=')
  117. class TestRasterMapAssertions(grass.gunittest.TestCase):
  118. # pylint: disable=R0904
  119. @classmethod
  120. def setUpClass(cls):
  121. cls.use_temp_region()
  122. # TODO: here we should actually not call self.runModule but call_module
  123. cls.runModule(SimpleModule('g.region', raster='elevation'))
  124. @classmethod
  125. def tearDownClass(cls):
  126. cls.del_temp_region()
  127. def test_assertRasterFitsUnivar(self):
  128. self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,
  129. precision=0.01)
  130. self.assertRaises(self.failureException,
  131. self.assertRasterFitsUnivar,
  132. 'aspect', R_UNIVAR_ELEVATION_SUBSET, precision=0.01)
  133. self.assertRaises(ValueError,
  134. self.assertRasterFitsUnivar,
  135. 'elevation', RANDOM_KEYVALUES)
  136. def test_assertRasterFitsInfo(self):
  137. self.assertRasterFitsInfo('elevation', R_INFO_ELEVATION_SUBSET)
  138. self.assertRaises(self.failureException,
  139. self.assertRasterFitsInfo,
  140. 'elev_lid792_1m', R_INFO_ELEVATION_SUBSET)
  141. self.assertRaises(ValueError,
  142. self.assertRasterFitsInfo,
  143. 'elevation', RANDOM_KEYVALUES)
  144. def test_common_values_info_univar(self):
  145. self.assertRasterFitsUnivar('elevation',
  146. ELEVATION_MINMAX, precision=0.01)
  147. self.assertRasterFitsInfo('elevation',
  148. ELEVATION_MINMAX, precision=0.01)
  149. def test_dict_as_parameter(self):
  150. """This also tests if we are using r.info -e flag and that precision is
  151. not required for strings.
  152. """
  153. self.assertRasterFitsInfo('elevation', ELEVATION_MAPSET_DICT)
  154. def test_assertRastersNoDifference(self):
  155. """Test basic usage of assertRastersNoDifference"""
  156. self.assertRastersNoDifference(actual='elevation',
  157. reference='elevation',
  158. precision=0, # this might need to be increased
  159. msg="The same maps should have no difference")
  160. self.assertRaises(self.failureException,
  161. self.assertRastersNoDifference,
  162. actual='elevation',
  163. reference='aspect',
  164. precision=1,
  165. msg="Different maps should have difference")
  166. def test_assertRastersNoDifference_mean(self):
  167. """Test usage of assertRastersNoDifference with mean"""
  168. self.assertRastersNoDifference(actual='elevation',
  169. reference='elevation',
  170. precision=0, # this might need to be increased
  171. statistics=dict(mean=0),
  172. msg="The difference of same maps should have small mean")
  173. self.assertRaises(self.failureException,
  174. self.assertRastersNoDifference,
  175. actual='elevation',
  176. reference='aspect',
  177. precision=1,
  178. statistics=dict(mean=0),
  179. msg="The difference of different maps should have huge mean")
  180. class TestMapExistsAssertions(grass.gunittest.TestCase):
  181. # pylint: disable=R0904
  182. raster_cell = 'TestMapExistsAssertions_raster_cell'
  183. raster_dcell = 'TestMapExistsAssertions_raster_dcell'
  184. raster3d = 'TestMapExistsAssertions_raster3D'
  185. vector = 'TestMapExistsAssertions_vector'
  186. @classmethod
  187. def setUpClass(cls):
  188. cls.use_temp_region()
  189. cls.runModule('g.region', n=10, e=10, s=0, w=0, t=10, b=0, res=1)
  190. cls.runModule('r.mapcalc', expression=cls.raster_cell + ' = 1')
  191. cls.runModule('r.mapcalc', expression=cls.raster_dcell + ' = 1.0')
  192. cls.runModule('r3.mapcalc', expression=cls.raster3d + ' = 1.0')
  193. cls.runModule('v.edit', map=cls.vector, tool='create')
  194. @classmethod
  195. def tearDownClass(cls):
  196. cls.runModule('g.remove', flags='f',
  197. type=['raster', 'raster3d', 'vector'],
  198. name=[cls.raster_cell, cls.raster_dcell,
  199. cls.raster3d, cls.vector])
  200. cls.del_temp_region()
  201. def test_rast_cell_exists(self):
  202. self.assertRasterExists(self.raster_cell)
  203. def test_rast_dcell_exists(self):
  204. self.assertRasterExists(self.raster_dcell)
  205. def test_rast_does_not_exist(self):
  206. self.assertRaises(self.failureException,
  207. self.assertRasterExists,
  208. 'does_not_exists')
  209. def test_rast3d_exists(self):
  210. self.assertRaster3dExists(self.raster3d)
  211. def test_rast3d_does_not_exist(self):
  212. self.assertRaises(self.failureException,
  213. self.assertRaster3dExists,
  214. 'does_not_exists')
  215. def test_vect_exists(self):
  216. self.assertVectorExists(self.vector)
  217. def test_vect_does_not_exist(self):
  218. self.assertRaises(self.failureException,
  219. self.assertVectorExists,
  220. 'does_not_exists')
  221. def test_rast_does_not_exist_in_current_mapset(self):
  222. # expecting that there is elevation in PERMANENT
  223. # TODO: use skip decorator
  224. # TODO: add the same tests but for vect and rast3d
  225. self.assertRaises(self.failureException,
  226. self.assertRasterExists,
  227. 'elevation',
  228. msg="Rasters from different mapsets should be ignored")
  229. class TestFileAssertions(grass.gunittest.TestCase):
  230. # pylint: disable=R0904
  231. @classmethod
  232. def setUpClass(cls):
  233. # we expect WIND to be always present
  234. gisenv = gcore.gisenv()
  235. cls.existing_file = os.path.join(gisenv['GISDBASE'],
  236. gisenv['LOCATION_NAME'],
  237. 'PERMANENT', 'WIND')
  238. cls.emtpy_file = cls.__name__ + '_this_is_an_empty_file'
  239. open(cls.emtpy_file, 'w').close()
  240. cls.file_with_md5 = cls.__name__ + '_this_is_a_file_with_known_md5'
  241. file_content = 'Content of the file with known MD5.\n'
  242. with open(cls.file_with_md5, 'w') as f:
  243. f.write(file_content)
  244. # MD5 sum created using:
  245. # echo 'Content of the file with known MD5.' > some_file.txt
  246. # md5sum some_file.txt
  247. cls.file_md5 = '807bba4ffac4bb351bc3f27853009949'
  248. cls.file_with_same_content = cls.__name__ + '_file_with_same_content'
  249. with open(cls.file_with_same_content, 'w') as f:
  250. f.write(file_content)
  251. cls.file_with_different_content = cls.__name__ + '_file_with_different_content'
  252. with open(cls.file_with_different_content, 'w') as f:
  253. f.write(file_content + ' Something else here.')
  254. @classmethod
  255. def tearDownClass(cls):
  256. os.remove(cls.emtpy_file)
  257. os.remove(cls.file_with_md5)
  258. os.remove(cls.file_with_same_content)
  259. os.remove(cls.file_with_different_content)
  260. def test_assertFileExists(self):
  261. self.assertFileExists(filename=self.existing_file)
  262. self.assertRaises(self.failureException,
  263. self.assertFileExists,
  264. filename='this_one_does_not_exists')
  265. def test_assertFileExists_empty_file(self):
  266. self.assertFileExists(filename=self.emtpy_file, skip_size_check=True)
  267. self.assertRaises(self.failureException,
  268. self.assertFileExists,
  269. filename=self.emtpy_file)
  270. def test_assertFileMd5(self):
  271. self.assertFileMd5(filename=self.file_with_md5, md5=self.file_md5)
  272. self.assertRaises(self.failureException,
  273. self.assertFileMd5,
  274. filename=self.file_with_md5, md5='wrongmd5')
  275. def test_assertFilesEqualMd5(self):
  276. self.assertFilesEqualMd5(filename=self.file_with_md5,
  277. reference=self.file_with_same_content)
  278. self.assertRaises(self.failureException,
  279. self.assertFilesEqualMd5,
  280. filename=self.file_with_md5,
  281. reference=self.file_with_different_content)
  282. if __name__ == '__main__':
  283. grass.gunittest.test()