forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_jsonb.py
More file actions
75 lines (61 loc) · 2.01 KB
/
test_jsonb.py
File metadata and controls
75 lines (61 loc) · 2.01 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
from unittest import TestCase
from piccolo.table import Table
from piccolo.columns.column_types import JSONB
from ..base import postgres_only
class MyTable(Table):
json = JSONB()
@postgres_only
class TestJSONB(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}')
def test_arrow(self):
"""
Test using the arrow function to retrieve a subset of the JSON.
"""
MyTable(json='{"a": 1}').save().run_sync()
row = MyTable.select(MyTable.json.arrow("a")).first().run_sync()
self.assertEqual(row["?column?"], "1")
def test_arrow_as_alias(self):
"""
Test using the arrow function to retrieve a subset of the JSON.
"""
MyTable(json='{"a": 1}').save().run_sync()
row = (
MyTable.select(MyTable.json.arrow("a").as_alias("a"))
.first()
.run_sync()
)
self.assertEqual(row["a"], "1")
def test_arrow_where(self):
"""
Make sure the arrow function can be used within a WHERE clause.
"""
MyTable(json='{"a": 1}').save().run_sync()
self.assertEqual(
MyTable.count().where(MyTable.json.arrow("a") == "1").run_sync(), 1
)
self.assertEqual(
MyTable.count().where(MyTable.json.arrow("a") == "2").run_sync(), 0
)
def test_arrow_first(self):
"""
Make sure the arrow function can be used with the first clause.
"""
MyTable.insert(
MyTable(json='{"a": 1}'), MyTable(json='{"b": 2}'),
).run_sync()
self.assertEqual(
MyTable.select(MyTable.json.arrow("a").as_alias("json"))
.first()
.run_sync(),
{"json": "1"},
)