forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_json.py
More file actions
150 lines (118 loc) · 3.66 KB
/
test_json.py
File metadata and controls
150 lines (118 loc) · 3.66 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from unittest import TestCase
from piccolo.columns.column_types import JSON
from piccolo.table import Table
class MyTable(Table):
json = JSON()
class MyTableDefault(Table):
"""
Test the different default types.
"""
json = JSON()
json_str = JSON(default="{}")
json_dict = JSON(default={})
json_list = JSON(default=[])
json_none = JSON(default=None, null=True)
class TestJSONSave(TestCase):
def setUp(self):
MyTable.create_table().run_sync()
def tearDown(self):
MyTable.alter().drop_table().run_sync()
def test_json_string(self):
"""
Test storing a valid JSON string.
"""
row = MyTable(json='{"a": 1}')
row.save().run_sync()
self.assertEqual(
MyTable.select(MyTable.json)
.first()
.run_sync()["json"]
.replace(" ", ""),
'{"a":1}',
)
def test_json_object(self):
"""
Test storing a valid JSON object.
"""
row = MyTable(json={"a": 1})
row.save().run_sync()
self.assertEqual(
MyTable.select(MyTable.json)
.first()
.run_sync()["json"]
.replace(" ", ""),
'{"a":1}',
)
class TestJSONDefault(TestCase):
def setUp(self):
MyTableDefault.create_table().run_sync()
def tearDown(self):
MyTableDefault.alter().drop_table().run_sync()
def test_json_default(self):
row = MyTableDefault()
row.save().run_sync()
self.assertEqual(row.json, "{}")
self.assertEqual(row.json_str, "{}")
self.assertEqual(row.json_dict, "{}")
self.assertEqual(row.json_list, "[]")
self.assertEqual(row.json_none, None)
def test_invalid_default(self):
with self.assertRaises(ValueError):
for value in ("a", 1, ("x", "y", "z")):
JSON(default=value)
class TestJSONInsert(TestCase):
def setUp(self):
MyTable.create_table().run_sync()
def tearDown(self):
MyTable.alter().drop_table().run_sync()
def check_response(self):
self.assertEqual(
MyTable.select(MyTable.json)
.first()
.run_sync()["json"]
.replace(" ", ""),
'{"message":"original"}',
)
def test_json_string(self):
"""
Test inserting using a string.
"""
row = MyTable(json='{"message": "original"}')
MyTable.insert(row).run_sync()
self.check_response()
def test_json_object(self):
"""
Test inserting using an object.
"""
row = MyTable(json={"message": "original"})
MyTable.insert(row).run_sync()
class TestJSONUpdate(TestCase):
def setUp(self):
MyTable.create_table().run_sync()
def tearDown(self):
MyTable.alter().drop_table().run_sync()
def add_row(self):
row = MyTable(json={"message": "original"})
row.save().run_sync()
def check_response(self):
self.assertEqual(
MyTable.select(MyTable.json)
.first()
.run_sync()["json"]
.replace(" ", ""),
'{"message":"updated"}',
)
def test_json_update_string(self):
"""
Test updating a JSON field using a string.
"""
self.add_row()
MyTable.update({MyTable.json: '{"message": "updated"}'}).run_sync()
self.check_response()
def test_json_update_object(self):
"""
Test updating a JSON field using an object.
"""
self.add_row()
MyTable.update({MyTable.json: {"message": "updated"}}).run_sync()
self.check_response()