test_assertions.py 11 KB

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