forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_join.py
More file actions
76 lines (57 loc) · 2.03 KB
/
test_join.py
File metadata and controls
76 lines (57 loc) · 2.03 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
76
from unittest import TestCase
from ..example_app.tables import Band, Manager, Concert, Venue
TABLES = [Manager, Band, Venue, Concert]
class TestCreateJoin:
def test_create_join(self):
for table in TABLES:
table.create_table().run_sync()
for table in reversed(TABLES):
table.alter().drop_table().run_sync()
class TestJoin(TestCase):
"""
Test instantiating Table instances
"""
tables = [Manager, Band, Venue, Concert]
def setUp(self):
for table in self.tables:
table.create_table().run_sync()
def tearDown(self):
for table in reversed(self.tables):
table.alter().drop_table().run_sync()
def test_join(self):
manager_1 = Manager(name="Guido")
manager_1.save().run_sync()
band_1 = Band(name="Pythonistas", manager=manager_1.id)
band_1.save().run_sync()
manager_2 = Manager(name="Graydon")
manager_2.save().run_sync()
band_2 = Band(name="Rustaceans", manager=manager_2.id)
band_2.save().run_sync()
venue = Venue(name="Grand Central")
venue.save().run_sync()
save_query = Concert(
band_1=band_1.id, band_2=band_2.id, venue=venue.id
).save()
save_query.run_sync()
select_query = Concert.select(
Concert.band_1.name,
Concert.band_2.name,
Concert.venue.name,
Concert.band_1.manager,
)
response = select_query.run_sync()
self.assertEqual(
response,
[
{
"band_1.name": "Pythonistas",
"band_2.name": "Rustaceans",
"venue.name": "Grand Central",
"band_1.manager": 1,
}
],
)
# Now make sure that even deeper joins work:
select_query = Concert.select(Concert.band_1.manager.name)
response = select_query.run_sync()
self.assertEqual(response, [{"band_1.manager.name": "Guido"}])