comparison tests/test_schema.py @ 44:ea8c2d01a9d9

Begin pickling support for ValidatenProblems, _Schema and Context
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 02 Aug 2023 09:31:49 +0200
parents 4ca530618303
children 92ae1e882cef
comparison
equal deleted inserted replaced
43:4ca530618303 44:ea8c2d01a9d9
1 1
2 import copy 2 import copy
3 import datetime 3 import datetime
4 import functools 4 import functools
5 import pickle
5 import re 6 import re
6 import unittest 7 import unittest
7 8
8 import _config 9 import _config
9 10
18 19
19 20
20 def _test_generic_validator_for_yaml(obj, schema, context): 21 def _test_generic_validator_for_yaml(obj, schema, context):
21 """Callback for loading test1.schema.yml: Always successful""" 22 """Callback for loading test1.schema.yml: Always successful"""
22 yield from () 23 yield from ()
24
25
26 class Pickling(unittest.TestCase):
27
28 def test_severity(self):
29 for sev in SEVERITY:
30 b = pickle.dumps(sev)
31 sev2 = pickle.loads(b)
32 self.assertEqual(sev, sev2)
33
34 def test_errors(self):
35 for err in ERRORS:
36 b = pickle.dumps(err)
37 err2 = pickle.loads(b)
38 self.assertEqual(err, err2)
39
40 def test_warnings(self):
41 for warn in ERRORS:
42 b = pickle.dumps(warn)
43 warn2 = pickle.loads(b)
44 self.assertEqual(warn, warn2)
45
46 def test_combination(self):
47 obj = (SEVERITY.ERROR, ERRORS.E10001, WARNINGS.W80000)
48 b = pickle.dumps(obj)
49 obj2 = pickle.loads(b)
50 self.assertEqual(obj, obj2)
51
52 def test_problem_1(self):
53 problem = data_schema.ValidationProblem(
54 code=ERRORS.E10002,
55 severity=SEVERITY.ERROR,
56 hint="a-hint")
57 b = pickle.dumps(problem)
58 problem2 = pickle.loads(b)
59 self.assertIsInstance(problem2, data_schema.ValidationProblem)
60 self.assertEqual(problem, problem2)
61
62 def test_problem_2(self):
63 problem = data_schema.ValidationProblem(
64 code=ERRORS.E10002,
65 severity=SEVERITY.ERROR,
66 hint="a-hint")
67
68 def _cmp(a, b):
69 return a == b
70
71 self.assertFalse(_cmp(problem, 1))
23 72
24 73
25 class YAML(unittest.TestCase): 74 class YAML(unittest.TestCase):
26 75
27 """Tests to load Python objects from YAML with complex Python-specific 76 """Tests to load Python objects from YAML with complex Python-specific
2481 2530
2482 pr = list(data_schema.validate({}, {"$type": "number"})) 2531 pr = list(data_schema.validate({}, {"$type": "number"}))
2483 self.assertEqual(1, len(pr)) 2532 self.assertEqual(1, len(pr))
2484 self.assertEqual(ERRORS.E10030, pr[0].code) 2533 self.assertEqual(ERRORS.E10030, pr[0].code)
2485 2534
2535 def test_number_pickle(self):
2536 pr = list(data_schema.validate(1.8, {"$type": "number"}))
2537 self.assertEqual(0, len(pr))
2538 pr = list(data_schema.validate(1, {"$type": "num"}))
2539 self.assertEqual(0, len(pr))
2540
2541 pr = list(data_schema.validate(
2542 2.0,
2543 {"$type": "number",
2544 "min-value": 3,
2545 "max-value": 1.3}))
2546
2547 pr2 = pickle.loads(pickle.dumps(pr))
2548 self.assertEqual(2, len(pr2))
2549 self.assertEqual(ERRORS.E10031, pr2[0].code)
2550 self.assertEqual(ERRORS.E10032, pr2[1].code)
2551 self.maxDiff = None
2552 self.assertEqual(repr(pr), repr(pr2))
2553
2486 def test_bool(self): 2554 def test_bool(self):
2487 pr = list(data_schema.validate(True, {"$type": "bool"})) 2555 pr = list(data_schema.validate(True, {"$type": "bool"}))
2488 self.assertEqual(0, len(pr)) 2556 self.assertEqual(0, len(pr))
2489 2557
2490 pr = list(data_schema.validate(True, {"$type": "boolean", 2558 pr = list(data_schema.validate(True, {"$type": "boolean",