test_assertions.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. V_UNIVAR_BRIDGES_WIDTH_SUBSET = """n=10938
  60. nmissing=0
  61. nnull=0
  62. min=0
  63. max=1451
  64. range=1451
  65. sum=2.6299e+06
  66. mean=240.437
  67. """
  68. class TestAssertCommandKeyValue(grass.gunittest.TestCase):
  69. """Test usage of `.assertModuleKeyValue` method."""
  70. # pylint: disable=R0904
  71. @classmethod
  72. def setUpClass(cls):
  73. cls.use_temp_region()
  74. cls.runModule(SimpleModule('g.region', rast='elevation'))
  75. @classmethod
  76. def tearDownClass(cls):
  77. cls.del_temp_region()
  78. def test_pygrass_module(self):
  79. """Test syntax with Module as module"""
  80. module = Module('r.info', map='elevation', flags='gr',
  81. run_=False, finish_=False)
  82. self.assertModuleKeyValue(module,
  83. reference=dict(min=55.58, max=156.33),
  84. precision=0.01, sep='=')
  85. def test_pygrass_simple_module(self):
  86. """Test syntax with SimpleModule as module"""
  87. module = SimpleModule('r.info', map='elevation', flags='gr')
  88. self.assertModuleKeyValue(module,
  89. reference=dict(min=55.58, max=156.33),
  90. precision=0.01, sep='=')
  91. def test_direct_parameters(self):
  92. """Test syntax with module and its parameters as fnction parameters"""
  93. self.assertModuleKeyValue('r.info', map='elevation', flags='gr',
  94. reference=dict(min=55.58, max=156.33),
  95. precision=0.01, sep='=')
  96. def test_parameters_parameter(self):
  97. """Test syntax with module parameters in one parameters dictionary"""
  98. self.assertModuleKeyValue(module='r.info',
  99. parameters=dict(map='elevation', flags='gr'),
  100. reference=dict(min=55.58, max=156.33),
  101. precision=0.01, sep='=')
  102. class TestRasterMapAssertations(grass.gunittest.TestCase):
  103. # pylint: disable=R0904
  104. @classmethod
  105. def setUpClass(cls):
  106. cls.use_temp_region()
  107. # TODO: here we should actually not call self.runModule but call_module
  108. cls.runModule(SimpleModule('g.region', rast='elevation'))
  109. @classmethod
  110. def tearDownClass(cls):
  111. cls.del_temp_region()
  112. def test_assertRasterFitsUnivar(self):
  113. self.assertRasterFitsUnivar('elevation', R_UNIVAR_ELEVATION_SUBSET,
  114. precision=0.01)
  115. self.assertRaises(self.failureException,
  116. self.assertRasterFitsUnivar,
  117. 'aspect', R_UNIVAR_ELEVATION_SUBSET, precision=0.01)
  118. self.assertRaises(ValueError,
  119. self.assertRasterFitsUnivar,
  120. 'elevation', RANDOM_KEYVALUES)
  121. def test_assertRasterFitsInfo(self):
  122. self.assertRasterFitsInfo('elevation', R_INFO_ELEVATION_SUBSET)
  123. self.assertRaises(self.failureException,
  124. self.assertRasterFitsInfo,
  125. 'elev_lid792_1m', R_INFO_ELEVATION_SUBSET)
  126. self.assertRaises(ValueError,
  127. self.assertRasterFitsInfo,
  128. 'elevation', RANDOM_KEYVALUES)
  129. def test_common_values_info_univar(self):
  130. self.assertRasterFitsUnivar('elevation',
  131. ELEVATION_MINMAX, precision=0.01)
  132. self.assertRasterFitsInfo('elevation',
  133. ELEVATION_MINMAX, precision=0.01)
  134. def test_dict_as_parameter(self):
  135. # this also tests if we are using r.info -e flag
  136. self.assertRasterFitsInfo('elevation', ELEVATION_MAPSET_DICT)
  137. def test_assertRastersNoDifference(self):
  138. """Test basic usage of assertRastersNoDifference"""
  139. self.assertRastersNoDifference(actual='elevation',
  140. reference='elevation',
  141. precision=0, # this might need to be increased
  142. msg="The same maps should have no difference")
  143. self.assertRaises(self.failureException,
  144. self.assertRastersNoDifference,
  145. actual='elevation',
  146. reference='aspect',
  147. precision=1,
  148. msg="Different maps should have difference")
  149. def test_assertRastersNoDifference_mean(self):
  150. """Test usage of assertRastersNoDifference with mean"""
  151. self.assertRastersNoDifference(actual='elevation',
  152. reference='elevation',
  153. precision=0, # this might need to be increased
  154. statistics=dict(mean=0),
  155. msg="The difference of same maps should have small mean")
  156. self.assertRaises(self.failureException,
  157. self.assertRastersNoDifference,
  158. actual='elevation',
  159. reference='aspect',
  160. precision=1,
  161. statistics=dict(mean=0),
  162. msg="The difference of different maps should have huge mean")
  163. class TestVectorMapAssertations(grass.gunittest.TestCase):
  164. # pylint: disable=R0904
  165. def test_assertVectorFitsUnivar(self):
  166. self.assertVectorFitsUnivar(map='bridges', column='WIDTH',
  167. reference=V_UNIVAR_BRIDGES_WIDTH_SUBSET,
  168. precision=0.01)
  169. self.assertRaises(self.failureException,
  170. self.assertVectorFitsUnivar,
  171. map='bridges', column='YEAR_BUILT',
  172. reference=V_UNIVAR_BRIDGES_WIDTH_SUBSET,
  173. precision=0.01)
  174. self.assertRaises(ValueError,
  175. self.assertVectorFitsUnivar,
  176. map='bridges', column='WIDTH',
  177. reference=RANDOM_KEYVALUES)
  178. class TestFileAssertations(grass.gunittest.TestCase):
  179. # pylint: disable=R0904
  180. @classmethod
  181. def setUpClass(cls):
  182. # we expect WIND to be always present
  183. gisenv = gcore.gisenv()
  184. cls.existing_file = os.path.join(gisenv['GISDBASE'],
  185. gisenv['LOCATION_NAME'],
  186. 'PERMANENT', 'WIND')
  187. cls.emtpy_file = cls.__name__ + '_this_is_an_empty_file'
  188. open(cls.emtpy_file, 'w').close()
  189. cls.file_with_md5 = cls.__name__ + '_this_is_a_file_with_known_md5'
  190. file_content = 'Content of the file with known MD5.\n'
  191. with open(cls.file_with_md5, 'w') as f:
  192. f.write(file_content)
  193. # MD5 sum created using:
  194. # echo 'Content of the file with known MD5.' > some_file.txt
  195. # md5sum some_file.txt
  196. cls.file_md5 = '807bba4ffac4bb351bc3f27853009949'
  197. cls.file_with_same_content = cls.__name__ + '_file_with_same_content'
  198. with open(cls.file_with_same_content, 'w') as f:
  199. f.write(file_content)
  200. cls.file_with_different_content = cls.__name__ + '_file_with_different_content'
  201. with open(cls.file_with_different_content, 'w') as f:
  202. f.write(file_content + ' Something else here.')
  203. @classmethod
  204. def tearDownClass(cls):
  205. os.remove(cls.emtpy_file)
  206. os.remove(cls.file_with_md5)
  207. os.remove(cls.file_with_same_content)
  208. os.remove(cls.file_with_different_content)
  209. def test_assertFileExists(self):
  210. self.assertFileExists(filename=self.existing_file)
  211. self.assertRaises(self.failureException,
  212. self.assertFileExists,
  213. filename='this_one_does_not_exists')
  214. def test_assertFileExists_empty_file(self):
  215. self.assertFileExists(filename=self.emtpy_file, skip_size_check=True)
  216. self.assertRaises(self.failureException,
  217. self.assertFileExists,
  218. filename=self.emtpy_file)
  219. def test_assertFileMd5(self):
  220. self.assertFileMd5(filename=self.file_with_md5, md5=self.file_md5)
  221. self.assertRaises(self.failureException,
  222. self.assertFileMd5,
  223. filename=self.file_with_md5, md5='wrongmd5')
  224. def test_assertFilesEqualMd5(self):
  225. self.assertFilesEqualMd5(filename=self.file_with_md5,
  226. reference=self.file_with_same_content)
  227. self.assertRaises(self.failureException,
  228. self.assertFilesEqualMd5,
  229. filename=self.file_with_md5,
  230. reference=self.file_with_different_content)
  231. if __name__ == '__main__':
  232. grass.gunittest.test()