forked from jsog/jsog-python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_jsog.py
97 lines (73 loc) · 2.37 KB
/
test_jsog.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
from jsog3 import jsog
import unittest
class TestJSOG(unittest.TestCase):
def test_encode_reference(self):
inner = { "foo": "bar" }
outer = { "inner1": inner, "inner2": inner }
encoded = jsog.encode(outer)
inner1 = encoded['inner1']
inner2 = encoded['inner2']
# one has @id, one has @ref
self.assertNotEqual('@id' in inner1, '@id' in inner2)
self.assertNotEqual('@ref' in inner1, '@ref' in inner2)
if '@id' in inner1:
self.assertEqual('0', inner1['@id'])
self.assertEqual('0', inner2['@ref'])
else:
self.assertEqual('0', inner2['@id'])
self.assertEqual('0', inner1['@ref'])
def test_decode_reference(self):
JSOGIFIED = '{"@id":"1","foo":"foo","inner1":{"@id":"2","bar":"bar"},"inner2":{"@ref":"2"}}'
parsed = jsog.loads(JSOGIFIED)
inner1 = parsed['inner1']
inner2 = parsed['inner2']
self.assertTrue(inner1 is inner2)
def test_encode_circular(self):
thing = {}
thing['me'] = thing
thing['list'] = [thing]
encoded = jsog.encode(thing)
self.assertEqual(encoded, {
'@id': '0',
'me': { '@ref': '0' },
'list': [ { '@ref': '0' } ],
})
def test_decode_circular(self):
thing = {}
thing['me'] = thing
thing['list'] = [thing]
encoded = jsog.encode(thing)
back = jsog.decode(encoded)
self.assertFalse('@id' in back)
self.assertTrue(back['me'] is back)
self.assertTrue(back['list'][0] is back)
def test_encode_null(self):
encoded = jsog.encode(None)
self.assertEqual(encoded, None)
def test_decode_null(self):
decoded = jsog.decode(None)
self.assertEqual(decoded, None)
def test_decode_plain_json(self):
json = { "foo": "bar" }
decoded = jsog.decode(json)
self.assertEqual(json, decoded)
def test_decode_list_reference(self):
JSOGIFIED = '{"@id":"1","foo":"foo","inner1":{"@id":"2","bar":"bar"},"inner2":[{"@ref":"2"}]}'
parsed = jsog.loads(JSOGIFIED)
inner1 = parsed['inner1']
inner2 = parsed['inner2'][0]
self.assertTrue(inner1 is inner2)
def test_decode_missing_id(self):
with self.assertRaises(KeyError):
json = { "foo": { "@ref": "1" }, "bar": { "@ref": "1" } }
jsog.decode(json)
def test_decode_duplicate_id(self):
with self.assertRaises(ValueError):
json = { "foo": { "@id": "1" }, "bar": { "@id": "1" } }
jsog.decode(json)
def test_decode_root_ref(self):
with self.assertRaises(KeyError):
json = { "@ref": "0" }
jsog.decode(json)
if __name__ == '__main__':
unittest.main()