test_assertions.py 14 KB

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