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
59 lines (43 loc) · 1.41 KB
/
test_json.py
File metadata and controls
59 lines (43 loc) · 1.41 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
from unittest import TestCase
from piccolo.table import Table
from piccolo.columns.column_types import JSON
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 TestJSON(TestCase):
def setUp(self):
MyTable.create_table().run_sync()
def tearDown(self):
MyTable.alter().drop_table().run_sync()
def test_json(self):
"""
Test storing a valid JSON string.
"""
row = MyTable(json='{"a": 1}')
row.save().run_sync()
self.assertEqual(row.json, '{"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)