forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_to_dict.py
More file actions
72 lines (61 loc) · 2.08 KB
/
test_to_dict.py
File metadata and controls
72 lines (61 loc) · 2.08 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
from tests.base import DBTestCase
from tests.example_apps.music.tables import Band, Manager
class TestToDict(DBTestCase):
def test_to_dict(self):
"""
Make sure that `to_dict` works correctly.
"""
self.insert_row()
instance = Manager.objects().first().run_sync()
dictionary = instance.to_dict()
self.assertDictEqual(dictionary, {"id": 1, "name": "Guido"})
def test_nested(self):
"""
Make sure that `to_dict` works correctly, when the object contains
nested objects.
"""
self.insert_row()
instance = Band.objects(Band.manager).first().run_sync()
dictionary = instance.to_dict()
self.assertDictEqual(
dictionary,
{
"id": 1,
"name": "Pythonistas",
"manager": {"id": 1, "name": "Guido"},
"popularity": 1000,
},
)
def test_filter_rows(self):
"""
Make sure that `to_dict` works correctly with a subset of columns.
"""
self.insert_row()
instance = Manager.objects().first().run_sync()
dictionary = instance.to_dict(Manager.name)
self.assertDictEqual(dictionary, {"name": "Guido"})
def test_nested_filter(self):
"""
Make sure that `to_dict` works correctly with nested objects and
filtering.
"""
self.insert_row()
instance = Band.objects(Band.manager).first().run_sync()
dictionary = instance.to_dict(Band.name, Band.manager.id)
self.assertDictEqual(
dictionary,
{
"name": "Pythonistas",
"manager": {"id": 1},
},
)
def test_aliases(self):
"""
Make sure that `to_dict` works correctly with aliases.
"""
self.insert_row()
instance = Manager.objects().first().run_sync()
dictionary = instance.to_dict(
Manager.id, Manager.name.as_alias("title")
)
self.assertDictEqual(dictionary, {"id": 1, "title": "Guido"})