test_checkers.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests checkers functions
  4. @brief Test of GRASS Python testing framework checkers
  5. (C) 2014 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. @author Vaclav Petras
  10. """
  11. from grass.script.utils import parse_key_val, try_remove
  12. from grass.gunittest.case import TestCase
  13. from grass.gunittest.main import test
  14. from grass.gunittest.checkers import (
  15. values_equal, text_to_keyvalue,
  16. keyvalue_equals, proj_info_equals, proj_units_equals,
  17. file_md5, text_file_md5)
  18. class TestValuesEqual(TestCase):
  19. def test_floats(self):
  20. self.assertTrue(values_equal(5.0, 5.0))
  21. self.assertTrue(values_equal(5.1, 5.19, precision=0.1))
  22. self.assertTrue(values_equal(5.00005, 5.000059, precision=0.00001))
  23. self.assertFalse(values_equal(5.125, 5.280))
  24. self.assertFalse(values_equal(5.00005, 5.00006, precision=0.00001))
  25. self.assertFalse(values_equal(2.5, 15.5, precision=5))
  26. def test_ints(self):
  27. self.assertTrue(values_equal(5, 5, precision=0.01))
  28. self.assertFalse(values_equal(5, 6, precision=0.01))
  29. self.assertTrue(values_equal(5, 8, precision=3))
  30. self.assertFalse(values_equal(3600, 3623, precision=20))
  31. self.assertTrue(values_equal(5, 5))
  32. self.assertFalse(values_equal(5, 6))
  33. def test_floats_and_ints(self):
  34. self.assertTrue(values_equal(5.1, 5, precision=0.2))
  35. self.assertFalse(values_equal(5.1, 5, precision=0.01))
  36. def test_strings(self):
  37. self.assertTrue(values_equal('hello', 'hello'))
  38. self.assertFalse(values_equal('Hello', 'hello'))
  39. def test_lists(self):
  40. self.assertTrue(values_equal([1, 2, 3], [1, 2, 3]))
  41. self.assertTrue(values_equal([1.1, 2.0, 3.9],
  42. [1.1, 1.95, 4.0],
  43. precision=0.2))
  44. self.assertFalse(values_equal([1, 2, 3, 4, 5],
  45. [1, 22, 3, 4, 5],
  46. precision=1))
  47. def test_mixed_lists(self):
  48. self.assertTrue(values_equal([1, 'abc', 8], [1, 'abc', 8.2],
  49. precision=0.5))
  50. def test_recursive_lists(self):
  51. self.assertTrue(values_equal([1, 'abc', [5, 9.6, 9.0]],
  52. [1, 'abc', [4.9, 9.2, 9.3]],
  53. precision=0.5))
  54. KEYVAL_TEXT = '''s: Hello
  55. str: Hello world!
  56. f: 1.0
  57. l: 1,2,3,4,5
  58. mixed: hello,8,-25,world!,4-1,5:2,0.1,-9.6
  59. '''
  60. # file location/PERMANENT/PROJ_INFO
  61. PROJ_INFO_TEXT_1 = """name: Lambert Conformal Conic
  62. proj: lcc
  63. datum: nad83
  64. a: 6378137.0
  65. es: 0.006694380022900787
  66. lat_1: 36.16666666666666
  67. lat_2: 34.33333333333334
  68. lat_0: 33.75
  69. lon_0: -79
  70. x_0: 609601.22
  71. y_0: 0
  72. no_defs: defined
  73. """
  74. # file location/PERMANENT/PROJ_UNITS
  75. PROJ_UNITS_TEXT_1 = """unit: Meter
  76. units: Meters
  77. meters: 1
  78. """
  79. PROJ_INFO_TEXT_2 = """name: Lambert Conformal Conic
  80. proj: lcc
  81. datum: nad83
  82. a: 6378137.0000000002
  83. es: 0.006694380022900787
  84. lat_1: 36.166666667
  85. lat_2: 34.333333333
  86. lat_0: 33.75
  87. lon_0: -79
  88. x_0: 609601.22
  89. y_0: 0
  90. no_defs: defined
  91. """
  92. PROJ_UNITS_TEXT_2 = """unit: Metre
  93. units: Meters
  94. meters: 1
  95. """
  96. # what about keys and lower/upper case letters
  97. class TestTextToKeyValue(TestCase):
  98. def test_conversion(self):
  99. keyvals = text_to_keyvalue(KEYVAL_TEXT, sep=':', val_sep=',')
  100. expected = {'s': 'Hello',
  101. 'str': 'Hello world!',
  102. 'f': 1.0,
  103. 'l': [1, 2, 3, 4, 5],
  104. 'mixed': ['hello', 8, -25, 'world!',
  105. '4-1', '5:2', 0.1, -9.6]}
  106. self.assertDictEqual(expected, keyvals)
  107. def test_single_values(self):
  108. keyvals = text_to_keyvalue("a: 1.5", sep=':')
  109. self.assertDictEqual({'a': 1.5}, keyvals)
  110. keyvals = text_to_keyvalue("abc=1", sep='=')
  111. self.assertDictEqual({'abc': 1}, keyvals)
  112. keyvals = text_to_keyvalue("abc=hello", sep='=')
  113. self.assertDictEqual({'abc': 'hello'}, keyvals)
  114. def test_strip(self):
  115. keyvals = text_to_keyvalue("a: 2.8 ", sep=':')
  116. self.assertDictEqual({'a': 2.8}, keyvals)
  117. keyvals = text_to_keyvalue("a: 2 ; 2.8 ; ab cd ",
  118. sep=':', val_sep=';')
  119. self.assertDictEqual({'a': [2, 2.8, 'ab cd']}, keyvals)
  120. keyvals = text_to_keyvalue("a : 2 ; 2.8", sep=':', val_sep=';')
  121. self.assertDictEqual({'a': [2, 2.8]}, keyvals)
  122. keyvals = text_to_keyvalue("a : \t 2 ;\t2.8", sep=':', val_sep=';')
  123. self.assertDictEqual({'a': [2, 2.8]}, keyvals)
  124. def test_empty_list_item(self):
  125. keyvals = text_to_keyvalue("a: 1, ,5,,", sep=':', val_sep=',')
  126. self.assertDictEqual({'a': [1, '', 5, '', '']}, keyvals)
  127. def test_empty_value(self):
  128. keyvals = text_to_keyvalue("a: ", sep=':')
  129. self.assertDictEqual({'a': ''}, keyvals)
  130. keyvals = text_to_keyvalue("a:", sep=':')
  131. self.assertDictEqual({'a': ''}, keyvals)
  132. def test_wrong_lines(self):
  133. # we consider no key-value separator as invalid line
  134. # and we silently ignore these
  135. keyvals = text_to_keyvalue("a", sep=':',
  136. skip_invalid=True, skip_empty=False)
  137. self.assertDictEqual({}, keyvals)
  138. self.assertRaises(ValueError, text_to_keyvalue, "a", sep=':',
  139. skip_invalid=False, skip_empty=False)
  140. # text_to_keyvalue considers the empty string as valid input
  141. keyvals = text_to_keyvalue("", sep=':',
  142. skip_invalid=False, skip_empty=False)
  143. self.assertDictEqual({}, keyvals)
  144. self.assertRaises(ValueError, text_to_keyvalue, "\n", sep=':',
  145. skip_invalid=True, skip_empty=False)
  146. keyvals = text_to_keyvalue("a\n\n", sep=':',
  147. skip_invalid=True, skip_empty=True)
  148. self.assertDictEqual({}, keyvals)
  149. def test_separators(self):
  150. keyvals = text_to_keyvalue("a=a;b;c", sep='=', val_sep=';')
  151. self.assertDictEqual({'a': ['a', 'b', 'c']}, keyvals)
  152. keyvals = text_to_keyvalue("a 1;2;3", sep=' ', val_sep=';')
  153. self.assertDictEqual({'a': [1, 2, 3]}, keyvals)
  154. # spaces as key-value separator and values separators
  155. # this should work (e.g. because of : in DMS),
  156. # although it does not support stripping (we don't merge separators)
  157. keyvals = text_to_keyvalue("a 1 2 3", sep=' ', val_sep=' ')
  158. self.assertDictEqual({'a': [1, 2, 3]}, keyvals)
  159. #def test_projection_files(self):
  160. # obtained by r.univar elevation -g
  161. # floats removed
  162. R_UNIVAR_KEYVAL = """n=2025000
  163. null_cells=57995100
  164. cells=60020100
  165. min=55.5787925720215
  166. max=156.329864501953
  167. range=100.751071929932
  168. mean=110.375440275606
  169. mean_of_abs=110.375440275606
  170. stddev=20.3153233205981
  171. variance=412.712361620436
  172. coeff_var=18.4056555243368
  173. sum=223510266.558102
  174. """
  175. # obtained by r.univar elevation -g
  176. # floats removed
  177. R_UNIVAR_KEYVAL_INT = """n=2025000
  178. null_cells=57995100
  179. cells=60020100
  180. """
  181. R_UNIVAR_KEYVAL_INT_DICT = {'n': 2025000,
  182. 'null_cells': 57995100, 'cells': 60020100}
  183. class TestComapreProjections(TestCase):
  184. def test_compare_proj_info(self):
  185. self.assertTrue(proj_info_equals(PROJ_INFO_TEXT_1, PROJ_INFO_TEXT_2))
  186. self.assertTrue(proj_units_equals(PROJ_UNITS_TEXT_1, PROJ_UNITS_TEXT_2))
  187. class TestParseKeyvalue(TestCase):
  188. def test_shell_script_style(self):
  189. self.assertDictEqual(parse_key_val(R_UNIVAR_KEYVAL_INT, val_type=int),
  190. R_UNIVAR_KEYVAL_INT_DICT)
  191. R_UNIVAR_ELEVATION = """n=2025000
  192. null_cells=57995100
  193. cells=60020100
  194. min=55.5787925720215
  195. max=156.329864501953
  196. range=100.751071929932
  197. mean=110.375440275606
  198. mean_of_abs=110.375440275606
  199. stddev=20.3153233205981
  200. variance=412.712361620436
  201. coeff_var=18.4056555243368
  202. sum=223510266.558102
  203. first_quartile=94.79
  204. median=108.88
  205. third_quartile=126.792
  206. percentile_90=138.66
  207. """
  208. R_UNIVAR_ELEVATION_ROUNDED = """n=2025000
  209. null_cells=57995100
  210. cells=60020100
  211. min=55.5788
  212. max=156.33
  213. range=100.751
  214. mean=110.375
  215. mean_of_abs=110.375
  216. stddev=20.3153
  217. variance=412.712
  218. coeff_var=18.4057
  219. sum=223510266.558
  220. first_quartile=94.79
  221. median=108.88
  222. third_quartile=126.792
  223. percentile_90=138.66
  224. """
  225. R_UNIVAR_ELEVATION_SUBSET = """n=2025000
  226. null_cells=57995100
  227. cells=60020100
  228. min=55.5787925720215
  229. max=156.329864501953
  230. """
  231. class TestRasterMapComparisons(TestCase):
  232. def test_compare_univars(self):
  233. self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  234. sep='='),
  235. text_to_keyvalue(R_UNIVAR_ELEVATION,
  236. sep='='),
  237. precision=0))
  238. self.assertFalse(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  239. sep='='),
  240. text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
  241. sep='='),
  242. precision=0))
  243. def test_compare_univars_subset(self):
  244. self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
  245. sep='='),
  246. text_to_keyvalue(R_UNIVAR_ELEVATION,
  247. sep='='),
  248. a_is_subset=True, precision=0))
  249. self.assertFalse(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  250. sep='='),
  251. text_to_keyvalue(R_UNIVAR_ELEVATION_SUBSET,
  252. sep='='),
  253. a_is_subset=True, precision=0))
  254. def test_compare_univars_rounded(self):
  255. self.assertTrue(keyvalue_equals(text_to_keyvalue(R_UNIVAR_ELEVATION,
  256. sep='='),
  257. text_to_keyvalue(R_UNIVAR_ELEVATION_ROUNDED,
  258. sep='='),
  259. precision=0.001))
  260. CORRECT_LINES = [
  261. "null_cells=57995100",
  262. "cells=60020100",
  263. "min=55.5787925720215",
  264. "max=156.329864501953"
  265. ]
  266. INCORRECT_LINES = [
  267. "null_cells=579951",
  268. "cells=60020100",
  269. "min=5.5787925720215",
  270. "max=156.329864501953"
  271. ]
  272. class TestMd5Sums(TestCase):
  273. r"""
  274. To create MD5 which is used for testing use:
  275. .. code: sh
  276. $ cat > test.txt << EOF
  277. null_cells=57995100
  278. cells=60020100
  279. min=55.5787925720215
  280. max=156.329864501953
  281. EOF
  282. $ md5sum test.txt
  283. 9dd6c4bb9d2cf6051b12f4b5f9d70523 test.txt
  284. """
  285. correct_md5sum = '9dd6c4bb9d2cf6051b12f4b5f9d70523'
  286. correct_file_name_platform_nl = 'md5_sum_correct_file_platform_nl'
  287. correct_file_name_unix_nl = 'md5_sum_correct_file_unix_nl'
  288. wrong_file_name = 'md5_sum_wrong_file'
  289. @classmethod
  290. def setUpClass(cls):
  291. with open(cls.correct_file_name_platform_nl, 'w') as f:
  292. for line in CORRECT_LINES:
  293. # \n should be converted to platform newline
  294. f.write(line + '\n')
  295. with open(cls.correct_file_name_unix_nl, 'wb') as f:
  296. for line in CORRECT_LINES:
  297. # binary mode will write pure \n
  298. f.write(line + '\n')
  299. with open(cls.wrong_file_name, 'w') as f:
  300. for line in INCORRECT_LINES:
  301. # \n should be converted to platform newline
  302. f.write(line + '\n')
  303. @classmethod
  304. def tearDownClass(cls):
  305. try_remove(cls.correct_file_name_platform_nl)
  306. try_remove(cls.correct_file_name_unix_nl)
  307. try_remove(cls.wrong_file_name)
  308. def test_text_file_binary(self):
  309. r"""File with ``\n`` (LF) newlines as binary (MD5 has ``\n``)."""
  310. self.assertEqual(file_md5(self.correct_file_name_unix_nl),
  311. self.correct_md5sum,
  312. msg="MD5 sums different")
  313. def test_text_file_platfrom(self):
  314. r"""Text file with platform dependent newlines"""
  315. self.assertEqual(text_file_md5(self.correct_file_name_platform_nl),
  316. self.correct_md5sum,
  317. msg="MD5 sums different")
  318. def test_text_file_unix(self):
  319. r"""Text file with ``\n`` (LF) newlines"""
  320. self.assertEqual(text_file_md5(self.correct_file_name_unix_nl),
  321. self.correct_md5sum,
  322. msg="MD5 sums different")
  323. def test_text_file_different(self):
  324. r"""Text file with ``\n`` (LF) newlines"""
  325. self.assertNotEqual(text_file_md5(self.wrong_file_name),
  326. self.correct_md5sum,
  327. msg="MD5 sums must be different")
  328. if __name__ == '__main__':
  329. test()