forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_objects.py
More file actions
220 lines (182 loc) · 6.44 KB
/
test_objects.py
File metadata and controls
220 lines (182 loc) · 6.44 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from tests.base import DBTestCase, postgres_only, sqlite_only
from tests.example_apps.music.tables import Band, Manager
class TestObjects(DBTestCase):
def test_get_all(self):
self.insert_row()
response = Band.objects().run_sync()
self.assertTrue(len(response) == 1)
instance = response[0]
self.assertTrue(isinstance(instance, Band))
self.assertTrue(instance.name == "Pythonistas")
# Now try changing the value and saving it.
instance.name = "Rustaceans"
save_query = instance.save()
save_query.run_sync()
self.assertTrue(
Band.select(Band.name).output(as_list=True).run_sync()[0]
== "Rustaceans"
)
@postgres_only
def test_offset_postgres(self):
"""
Postgres can do an offset without a limit clause.
"""
self.insert_rows()
response = Band.objects().order_by(Band.name).offset(1).run_sync()
self.assertEqual(
[i.name for i in response], ["Pythonistas", "Rustaceans"]
)
@sqlite_only
def test_offset_sqlite(self):
"""
SQLite requires a limit clause for offset to work.
"""
self.insert_rows()
query = Band.objects().order_by(Band.name).offset(1)
with self.assertRaises(ValueError):
query.run_sync()
query = query.limit(5)
response = query.run_sync()
self.assertEqual(
[i.name for i in response], ["Pythonistas", "Rustaceans"]
)
def test_get(self):
self.insert_row()
band = Band.objects().get(Band.name == "Pythonistas").run_sync()
self.assertTrue(band.name == "Pythonistas")
def test_get__prefetch(self):
self.insert_rows()
# With prefetch clause
band = (
Band.objects()
.get((Band.name == "Pythonistas"))
.prefetch(Band.manager)
.run_sync()
)
self.assertIsInstance(band.manager, Manager)
# Just passing it straight into objects
band = (
Band.objects(Band.manager)
.get((Band.name == "Pythonistas"))
.run_sync()
)
self.assertIsInstance(band.manager, Manager)
def test_get_or_create(self):
"""
Make sure `get_or_create` works for simple where clauses.
"""
# When the row doesn't exist in the db:
Band.objects().get_or_create(
Band.name == "Pink Floyd", defaults={"popularity": 100}
).run_sync()
instance = (
Band.objects().where(Band.name == "Pink Floyd").first().run_sync()
)
self.assertIsInstance(instance, Band)
self.assertTrue(instance.name == "Pink Floyd")
self.assertTrue(instance.popularity == 100)
# When the row already exists in the db:
Band.objects().get_or_create(
Band.name == "Pink Floyd", defaults={Band.popularity: 100}
).run_sync()
instance = (
Band.objects().where(Band.name == "Pink Floyd").first().run_sync()
)
self.assertIsInstance(instance, Band)
self.assertTrue(instance.name == "Pink Floyd")
self.assertTrue(instance.popularity == 100)
def test_get_or_create_complex(self):
"""
Make sure `get_or_create` works with complex where clauses.
"""
self.insert_rows()
# When the row already exists in the db:
instance = (
Band.objects()
.get_or_create(
(Band.name == "Pythonistas") & (Band.popularity == 1000)
)
.run_sync()
)
self.assertIsInstance(instance, Band)
self.assertEqual(instance._was_created, False)
# When the row doesn't exist in the db:
instance = (
Band.objects()
.get_or_create(
(Band.name == "Pythonistas2") & (Band.popularity == 2000)
)
.run_sync()
)
self.assertIsInstance(instance, Band)
self.assertEqual(instance._was_created, True)
def test_get_or_create_very_complex(self):
"""
Make sure `get_or_create` works with very complex where clauses.
"""
self.insert_rows()
# When the row already exists in the db:
instance = (
Band.objects()
.get_or_create(
(Band.name == "Pythonistas")
& (Band.popularity > 0)
& (Band.popularity < 5000)
)
.run_sync()
)
self.assertIsInstance(instance, Band)
self.assertEqual(instance._was_created, False)
# When the row doesn't exist in the db:
instance = (
Band.objects()
.get_or_create(
(Band.name == "Pythonistas2")
& (Band.popularity > 10)
& (Band.popularity < 5000)
)
.run_sync()
)
self.assertIsInstance(instance, Band)
self.assertEqual(instance._was_created, True)
# The values in the > and < should be ignored, and the default should
# be used for the column.
self.assertEqual(instance.popularity, 0)
def test_get_or_create_with_joins(self):
"""
Make sure that that `get_or_create` creates rows correctly when using
joins.
"""
instance = (
Band.objects()
.get_or_create(
(Band.name == "My new band")
& (Band.manager.name == "Excellent manager")
)
.run_sync()
)
self.assertIsInstance(instance, Band)
self.assertEqual(instance._was_created, True)
# We want to make sure the band name isn't 'Excellent manager' by
# mistake.
self.assertEqual(Band.name, "My new band")
def test_get_or_create__prefetch(self):
"""
Make sure that that `get_or_create` works with the `prefetch` clause.
"""
self.insert_rows()
# With prefetch clause
band = (
Band.objects()
.get_or_create((Band.name == "Pythonistas"))
.prefetch(Band.manager)
.run_sync()
)
self.assertIsInstance(band.manager, Manager)
# Just passing it straight into objects
band = (
Band.objects(Band.manager)
.get_or_create((Band.name == "Pythonistas"))
.run_sync()
)
self.assertIsInstance(band.manager, Manager)