Skip to content

Commit baf35b3

Browse files
committed
add extra tests for inserting and updating JSON
1 parent 690c492 commit baf35b3

File tree

1 file changed

+94
-3
lines changed

1 file changed

+94
-3
lines changed

tests/columns/test_json.py

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,42 @@ class MyTableDefault(Table):
2020
json_none = JSON(default=None, null=True)
2121

2222

23-
class TestJSON(TestCase):
23+
class TestJSONSave(TestCase):
2424
def setUp(self):
2525
MyTable.create_table().run_sync()
2626

2727
def tearDown(self):
2828
MyTable.alter().drop_table().run_sync()
2929

30-
def test_json(self):
30+
def test_json_string(self):
3131
"""
3232
Test storing a valid JSON string.
3333
"""
3434
row = MyTable(json='{"a": 1}')
3535
row.save().run_sync()
36-
self.assertEqual(row.json, '{"a": 1}')
36+
37+
self.assertEqual(
38+
MyTable.select(MyTable.json)
39+
.first()
40+
.run_sync()["json"]
41+
.replace(" ", ""),
42+
'{"a":1}',
43+
)
44+
45+
def test_json_object(self):
46+
"""
47+
Test storing a valid JSON object.
48+
"""
49+
row = MyTable(json={"a": 1})
50+
row.save().run_sync()
51+
52+
self.assertEqual(
53+
MyTable.select(MyTable.json)
54+
.first()
55+
.run_sync()["json"]
56+
.replace(" ", ""),
57+
'{"a":1}',
58+
)
3759

3860

3961
class TestJSONDefault(TestCase):
@@ -57,3 +79,72 @@ def test_invalid_default(self):
5779
with self.assertRaises(ValueError):
5880
for value in ("a", 1, ("x", "y", "z")):
5981
JSON(default=value)
82+
83+
84+
class TestJSONInsert(TestCase):
85+
def setUp(self):
86+
MyTable.create_table().run_sync()
87+
88+
def tearDown(self):
89+
MyTable.alter().drop_table().run_sync()
90+
91+
def check_response(self):
92+
self.assertEqual(
93+
MyTable.select(MyTable.json)
94+
.first()
95+
.run_sync()["json"]
96+
.replace(" ", ""),
97+
'{"message":"original"}',
98+
)
99+
100+
def test_json_string(self):
101+
"""
102+
Test inserting using a string.
103+
"""
104+
row = MyTable(json='{"message": "original"}')
105+
MyTable.insert(row).run_sync()
106+
self.check_response()
107+
108+
def test_json_object(self):
109+
"""
110+
Test inserting using an object.
111+
"""
112+
row = MyTable(json={"message": "original"})
113+
MyTable.insert(row).run_sync()
114+
115+
116+
class TestJSONUpdate(TestCase):
117+
def setUp(self):
118+
MyTable.create_table().run_sync()
119+
120+
def tearDown(self):
121+
MyTable.alter().drop_table().run_sync()
122+
123+
def add_row(self):
124+
row = MyTable(json={"message": "original"})
125+
row.save().run_sync()
126+
127+
def check_response(self):
128+
self.assertEqual(
129+
MyTable.select(MyTable.json)
130+
.first()
131+
.run_sync()["json"]
132+
.replace(" ", ""),
133+
'{"message":"updated"}',
134+
)
135+
136+
def test_json_update_string(self):
137+
"""
138+
Test updating a JSON field using a string.
139+
"""
140+
self.add_row()
141+
MyTable.update({MyTable.json: '{"message": "updated"}'}).run_sync()
142+
self.check_response()
143+
144+
def test_json_update_object(self):
145+
"""
146+
Test updating a JSON field using an object.
147+
"""
148+
self.add_row()
149+
MyTable.update({MyTable.json: {"message": "updated"}}).run_sync()
150+
self.check_response()

0 commit comments

Comments
 (0)