Skip to content

Commit 87ea64e

Browse files
committed
added tests for convert_to_sql_value
1 parent 87c0510 commit 87ea64e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tests/utils/test_sql_values.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from unittest import TestCase
2+
3+
from piccolo.columns.column_types import Integer, JSON, JSONB
4+
from piccolo.table import Table
5+
from piccolo.utils.sql_values import convert_to_sql_value
6+
7+
8+
class TestConvertToSQLValue(TestCase):
9+
def test_convert_json(self):
10+
"""
11+
Make sure Python objects are serialised correctly to JSON strings.
12+
"""
13+
self.assertEqual(
14+
convert_to_sql_value(value={"a": 123}, column=JSON()).replace(
15+
" ", ""
16+
),
17+
'{"a":123}',
18+
)
19+
20+
self.assertEqual(
21+
convert_to_sql_value(value={"a": 123}, column=JSONB()).replace(
22+
" ", ""
23+
),
24+
'{"a":123}',
25+
)
26+
27+
def test_convert_table_instance(self):
28+
"""
29+
Make sure Table instances are converted to integers.
30+
"""
31+
32+
class MyTable(Table):
33+
pass
34+
35+
instance = MyTable(id=1)
36+
37+
self.assertEqual(
38+
convert_to_sql_value(value=instance, column=MyTable.id), 1
39+
)
40+
41+
def test_other(self):
42+
"""
43+
Make sure simple Python values are returned correctly.
44+
"""
45+
self.assertEqual(
46+
convert_to_sql_value(value=1, column=Integer()), 1,
47+
)
48+

0 commit comments

Comments
 (0)