-
Notifications
You must be signed in to change notification settings - Fork 121
/
run.py
executable file
·129 lines (117 loc) · 5.18 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
from __future__ import unicode_literals
import json
import os.path
import unittest
from collections import OrderedDict
from decimal import Decimal
from pyangbind.lib.serialise import pybindJSONDecoder
from tests.base import PyangBindTestCase
class IETFJSONDeserialiseTests(PyangBindTestCase):
yang_files = ["ietf-json-deserialise.yang"]
maxDiff = None
def test_multi_key_list_load(self):
expected_json = {
"mkey": {"one 1": {"leaf-two": 1, "leaf-one": "one"}, "three 2": {"leaf-two": 2, "leaf-one": "three"}}
}
with open(os.path.join(os.path.dirname(__file__), "json", "mkeylist.json")) as fp:
actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get(
filter=True
)
self.assertEqual(actual_json, expected_json, "Multikey list load did not return expected JSON")
def test_single_key_list_load(self):
expected_json = {
"skey": {"one": {"leaf-one": "one"}, "three": {"leaf-one": "three"}, "two": {"leaf-one": "two"}}
}
with open(os.path.join(os.path.dirname(__file__), "json", "skeylist.json")) as fp:
actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get(
filter=True
)
self.assertEqual(actual_json, expected_json, "Single key list load did not return expected JSON")
def test_list_with_children_load(self):
expected_json = {
"chlist": {
1: {"keyleaf": 1, "child": {"number": 1, "string": "one"}},
2: {"keyleaf": 2, "child": {"number": 2, "string": "two"}},
}
}
with open(os.path.join(os.path.dirname(__file__), "json", "chlist.json")) as fp:
actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get(
filter=True
)
self.assertEqual(actual_json, expected_json, "List with children load did not return expected JSON")
def test_all_the_types(self):
expected_json = {
"c1": {
"l1": {
1: {
"one-leaf": "hi",
"typedef-one": "test",
"boolean": True,
"binary": b"yang",
"union": "16",
"identityref": "idone",
"enumeration": "one",
"k1": 1,
"uint16": 1,
"union-list": [16, "chicken"],
"uint32": 1,
"int32": 1,
"int16": 1,
"string": "bear",
"typedef-two": 8,
"uint8": 1,
"restricted-integer": 6,
"leafref": "16",
"int8": 1,
"uint64": 1,
"remote-identityref": "stilton",
"int64": 1,
"restricted-string": "aardvark",
"decimal": Decimal("16.32"),
"empty": True,
}
},
"l2": OrderedDict(
[
(1, {"k1": 1}),
(2, {"k1": 2}),
(3, {"k1": 3}),
(4, {"k1": 4}),
(5, {"k1": 5}),
(6, {"k1": 6}),
(7, {"k1": 7}),
(8, {"k1": 8}),
(9, {"k1": 9}),
]
),
"t1": {"32": {"target": "32"}, "16": {"target": "16"}},
}
}
with open(os.path.join(os.path.dirname(__file__), "json", "complete-obj.json")) as fp:
actual_json = pybindJSONDecoder.load_ietf_json(json.load(fp), self.bindings, "ietf_json_deserialise").get(
filter=True
)
self.assertEqual(actual_json, expected_json, "Deserialisation of complete object not as expected.")
def test_skip_unknown_keys(self):
allowed = True
try:
with open(os.path.join(os.path.dirname(__file__), "json", "nonexistkey.json")) as fp:
pybindJSONDecoder.load_ietf_json(
json.load(fp), self.bindings, "ietf_json_deserialise", skip_unknown=True
)
except AttributeError:
allowed = False
self.assertTrue(allowed, "Skipping keys that did not exist was not successfully handled.")
def test_dont_skip_unknown_keys(self):
allowed = True
try:
with open(os.path.join(os.path.dirname(__file__), "json", "nonexistkey.json")) as fp:
pybindJSONDecoder.load_ietf_json(
json.load(fp), self.bindings, "ietf_json_deserialise", skip_unknown=False
)
except AttributeError:
allowed = False
self.assertFalse(allowed, "Skipping keys that did not exist was not successfully handled.")
if __name__ == "__main__":
unittest.main()