-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_find_many.py
400 lines (360 loc) · 11.3 KB
/
test_find_many.py
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import pytest
import prisma
from prisma import Prisma
@pytest.mark.asyncio
async def test_find_many(client: Prisma) -> None:
"""Filters and ordering work as suggested"""
posts = [
await client.post.create({'title': 'Test post 1', 'published': False}),
await client.post.create({'title': 'Test post 2', 'published': False}),
]
found = await client.post.find_many(where={'title': 'Test post 1'})
assert len(found) == 1
assert found[0].id == posts[0].id
posts = await client.post.find_many(where={'OR': [{'title': 'Test post 1'}, {'title': 'Test post 2'}]})
assert len(posts) == 2
posts = await client.post.find_many(where={'title': {'contains': 'Test post'}})
assert len(posts) == 2
posts = await client.post.find_many(where={'title': {'startswith': 'Test post'}})
assert len(posts) == 2
posts = await client.post.find_many(where={'title': {'not_in': ['Test post 1']}})
assert len(posts) == 1
assert posts[0].title == 'Test post 2'
posts = await client.post.find_many(where={'title': {'equals': 'Test post 2'}})
assert len(posts) == 1
assert posts[0].title == 'Test post 2'
posts = await client.post.find_many(where={'title': 'Test post 2'})
assert len(posts) == 1
assert posts[0].title == 'Test post 2'
posts = await client.post.find_many(order={'title': 'desc'})
assert len(posts) == 2
assert posts[0].title == 'Test post 2'
assert posts[1].title == 'Test post 1'
posts = await client.post.find_many(order={'title': 'asc'})
assert len(posts) == 2
assert posts[0].title == 'Test post 1'
assert posts[1].title == 'Test post 2'
@pytest.mark.asyncio
async def test_cursor(client: Prisma) -> None:
"""Cursor argument correctly paginates results"""
posts = [
await client.post.create({'title': 'Foo 1', 'published': False}),
await client.post.create({'title': 'Foo 2', 'published': False}),
await client.post.create({'title': 'Foo 3', 'published': False}),
await client.post.create({'title': 'Foo 4', 'published': False}),
]
found = await client.post.find_many(
cursor={
'id': posts[1].id,
},
order={
'title': 'asc',
},
)
assert len(found) == 3
assert found[0].title == 'Foo 2'
assert found[1].title == 'Foo 3'
assert found[2].title == 'Foo 4'
found = await client.post.find_many(
cursor={
'id': posts[3].id,
},
order={
'title': 'asc',
},
)
assert len(found) == 1
assert found[0].title == 'Foo 4'
@pytest.mark.asyncio
async def test_filtering_one_to_one_relation(client: Prisma) -> None:
"""Filtering by a 1-1 relational field and negating the filter"""
async with client.batch_() as batcher:
batcher.user.create(
{
'name': 'Robert',
'profile': {
'create': {
'description': 'My very cool bio.',
'country': 'Scotland',
}
},
},
)
batcher.user.create(
{
'name': 'Tegan',
'profile': {
'create': {
'description': 'Hello world, this is my bio.',
'country': 'Scotland',
},
},
},
)
batcher.user.create({'name': 'Callum'})
users = await client.user.find_many(
where={
'profile': {
'is': {
'description': {
'contains': 'cool',
}
}
}
}
)
assert len(users) == 1
assert users[0].name == 'Robert'
assert users[0].profile is None
users = await client.user.find_many(
where={
'profile': {
'is': {
'description': {
'contains': 'bio',
},
},
},
},
order={
'name': 'asc',
},
)
assert len(users) == 2
assert users[0].name == 'Robert'
assert users[1].name == 'Tegan'
users = await client.user.find_many(
where={
'profile': {
'is_not': {
'description': {
'contains': 'bio',
}
}
}
}
)
assert len(users) == 1
assert users[0].name == 'Callum'
@pytest.mark.asyncio
async def test_filtering_one_to_many_relation(client: Prisma) -> None:
"""Filtering by a 1-M relational field and negating the filter"""
async with client.batch_() as batcher:
batcher.user.create(
{
'name': 'Robert',
'posts': {
'create': [
{'title': 'My first post', 'published': True},
{'title': 'My second post', 'published': False},
]
},
}
)
batcher.user.create(
{
'name': 'Tegan',
'posts': {
'create': [
{'title': 'Hello, world!', 'published': True},
{'title': 'My test post', 'published': False},
]
},
}
)
batcher.user.create({'name': 'Callum'})
# I guess it makes sense that a record with no relations also matches this
# TODO: this needs to be documented
users = await client.user.find_many(
where={
'posts': {
'every': {
'title': {
'contains': 'post',
}
}
}
},
)
assert len(users) == 2
assert users[0].name == 'Robert'
assert users[1].name == 'Callum'
users = await client.user.find_many(
where={
'posts': {
'some': {
'title': {
'contains': 'post',
}
}
}
},
order={
'name': 'asc',
},
)
assert len(users) == 2
assert users[0].name == 'Robert'
assert users[1].name == 'Tegan'
users = await client.user.find_many(
where={
'posts': {
'none': {
'title': {
'contains': 'post',
}
}
}
}
)
assert len(users) == 1
assert users[0].name == 'Callum'
users = await client.user.find_many(
where={
'posts': {
'some': {
'title': 'foo',
}
}
}
)
assert len(users) == 0
@pytest.mark.asyncio
async def test_ordering(client: Prisma) -> None:
"""Ordering by `asc` and `desc` correctly changes the order of the returned records"""
async with client.batch_() as batcher:
batcher.post.create({'title': 'Test post 1', 'published': False})
batcher.post.create({'title': 'Test post 2', 'published': False})
batcher.post.create({'title': 'Test post 3', 'published': True})
found = await client.post.find_many(
where={'title': {'contains': 'Test'}},
order={'published': 'asc'},
)
assert len(found) == 3
assert found[0].published is False
assert found[1].published is False
assert found[2].published is True
found = await client.post.find_many(
where={'title': {'contains': 'Test'}},
order={'published': 'desc'},
)
assert len(found) == 3
assert found[0].published is True
assert found[1].published is False
assert found[2].published is False
# multiple fields in the same `order` dictionary are not supported
with pytest.raises(prisma.errors.DataError):
await client.post.find_many(
where={
'title': {
'contains': 'Test',
},
},
order={ # type: ignore
'published': 'desc',
'title': 'desc',
},
)
@pytest.mark.asyncio
@pytest.mark.skip(reason='incorrect error is raised here - requires an overhaul of the error system')
async def test_too_many_fields_error(client: Prisma) -> None:
"""Passing in multiple fields in order is not supported"""
with pytest.raises(prisma.errors.DataError) as exc:
await client.post.find_many(
where={
'title': {
'contains': 'Test',
},
},
order={ # type: ignore
'published': 'desc',
'title': 'desc',
},
)
assert exc.match(r'Expected a minimum of 0 and at most 1 fields to be present, got 2')
@pytest.mark.asyncio
async def test_order_field_not_nullable(client: Prisma) -> None:
"""Order by fields, if present, cannot be None"""
with pytest.raises(prisma.errors.FieldNotFoundError, match=r'orderBy.desc'):
await client.post.find_many(order={'desc': None}) # type: ignore
@pytest.mark.asyncio
async def test_distinct(client: Prisma) -> None:
"""Filtering by distinct combinations of fields"""
users = [
await client.user.create(
data={
'name': 'Robert',
},
),
await client.user.create(
data={
'name': 'Tegan',
},
),
await client.user.create(
data={
'name': 'Patrick',
},
),
]
async with client.batch_() as batcher:
batcher.profile.create(
{
'city': 'Paris',
'country': 'France',
'description': 'Foo',
'user_id': users[0].id,
}
)
batcher.profile.create(
{
'city': 'Lyon',
'country': 'France',
'description': 'Foo',
'user_id': users[1].id,
}
)
batcher.profile.create(
{
'city': 'Paris',
'country': 'Denmark',
'description': 'Foo',
'user_id': users[2].id,
}
)
results = await client.profile.find_many(
distinct=['country'],
order={
'country': 'asc',
},
)
assert len(results) == 2
assert results[0].country == 'Denmark'
assert results[1].country == 'France'
results = await client.profile.find_many(
distinct=['city'],
order={
'city': 'asc',
},
)
assert len(results) == 2
assert results[0].city == 'Lyon'
assert results[1].city == 'Paris'
results = await client.profile.find_many(
distinct=['city', 'country'],
order=[
{
'city': 'asc',
},
{
'country': 'asc',
},
],
)
assert len(results) == 3
assert results[0].city == 'Lyon'
assert results[0].country == 'France'
assert results[1].city == 'Paris'
assert results[1].country == 'Denmark'
assert results[2].city == 'Paris'
assert results[2].country == 'France'