test_checkers.py 12 KB

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