Coverage for databases/sync_tests/test_find_first_or_raise.py: 100%
80 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import pytest
3from prisma import Prisma, errors
4from prisma.types import UserWhereInput
7def test_find_first_or_raise(client: Prisma) -> None:
8 """Skips multiple non-matching records"""
9 posts = [
10 client.post.create(
11 {
12 'title': 'Test post 1',
13 'published': False,
14 'views': 100,
15 }
16 ),
17 client.post.create(
18 {
19 'title': 'Test post 2',
20 'published': False,
21 }
22 ),
23 client.post.create(
24 {
25 'title': 'Test post 3',
26 'published': False,
27 }
28 ),
29 client.post.create(
30 {
31 'title': 'Test post 4',
32 'published': True,
33 'views': 500,
34 }
35 ),
36 client.post.create(
37 {
38 'title': 'Test post 5',
39 'published': False,
40 }
41 ),
42 client.post.create(
43 {
44 'title': 'Test post 6',
45 'published': True,
46 }
47 ),
48 ]
50 post = client.post.find_first_or_raise(
51 where={
52 'published': True,
53 },
54 order={
55 'title': 'asc',
56 },
57 )
58 assert post.id == posts[3].id
59 assert post.title == 'Test post 4'
60 assert post.published is True
62 with pytest.raises(
63 errors.RecordNotFoundError,
64 match=r'depends on one or more records that were required but not found',
65 ):
66 client.post.find_first_or_raise(
67 where={
68 'title': {
69 'contains': 'not found',
70 }
71 }
72 )
74 post = client.post.find_first_or_raise(
75 where={
76 'published': True,
77 },
78 order={
79 'title': 'asc',
80 },
81 skip=1,
82 )
83 assert post.id == posts[5].id
84 assert post.title == 'Test post 6'
85 assert post.published is True
87 post = client.post.find_first_or_raise(
88 where={
89 'NOT': [
90 {
91 'published': True,
92 },
93 ],
94 },
95 order={
96 'created_at': 'asc',
97 },
98 )
99 assert post.title == 'Test post 1'
101 post = client.post.find_first_or_raise(
102 where={
103 'NOT': [
104 {
105 'title': {
106 'contains': '1',
107 },
108 },
109 {
110 'title': {
111 'contains': '2',
112 },
113 },
114 ],
115 },
116 order={
117 'created_at': 'asc',
118 },
119 )
120 assert post.title == 'Test post 3'
122 post = client.post.find_first_or_raise(
123 where={
124 'title': {
125 'contains': 'Test',
126 },
127 'AND': [
128 {
129 'published': True,
130 },
131 ],
132 },
133 order={
134 'created_at': 'asc',
135 },
136 )
137 assert post.title == 'Test post 4'
139 post = client.post.find_first_or_raise(
140 where={
141 'AND': [
142 {
143 'published': True,
144 },
145 {
146 'title': {
147 'contains': 'Test',
148 }
149 },
150 ],
151 },
152 order={
153 'created_at': 'asc',
154 },
155 )
156 assert post.title == 'Test post 4'
158 with pytest.raises(errors.RecordNotFoundError):
159 client.post.find_first_or_raise(
160 where={
161 'views': {
162 'gt': 100,
163 },
164 'OR': [
165 {
166 'published': False,
167 },
168 ],
169 }
170 )
172 post = client.post.find_first_or_raise(
173 where={
174 'OR': [
175 {
176 'views': {
177 'gt': 100,
178 },
179 },
180 {
181 'published': False,
182 },
183 ]
184 },
185 order={
186 'created_at': 'asc',
187 },
188 )
189 assert post.title == 'Test post 1'
191 post = client.post.find_first_or_raise(
192 where={
193 'OR': [
194 {
195 'views': {
196 'gt': 100,
197 },
198 },
199 ]
200 }
201 )
202 assert post.title == 'Test post 4'
205def test_filtering_one_to_one_relation(client: Prisma) -> None:
206 """Filtering by a 1-1 relational field and negating the filter"""
207 with client.batch_() as batcher:
208 batcher.user.create(
209 {
210 'name': 'Robert',
211 'profile': {
212 'create': {
213 'description': 'My very cool bio.',
214 'country': 'Scotland',
215 },
216 },
217 },
218 )
219 batcher.user.create(
220 {
221 'name': 'Tegan',
222 'profile': {
223 'create': {
224 'description': 'Hello world, this is my bio.',
225 'country': 'Scotland',
226 },
227 },
228 },
229 )
230 batcher.user.create({'name': 'Callum'})
232 user = client.user.find_first_or_raise(
233 where={
234 'profile': {
235 'is': {
236 'description': {
237 'contains': 'cool',
238 }
239 }
240 }
241 }
242 )
243 assert user.name == 'Robert'
244 assert user.profile is None
246 user = client.user.find_first_or_raise(
247 where={
248 'profile': {
249 'is_not': {
250 'description': {
251 'contains': 'bio',
252 }
253 }
254 }
255 }
256 )
257 assert user.name == 'Callum'
258 assert user.profile is None
261def test_filtering_and_ordering_one_to_many_relation(
262 client: Prisma,
263) -> None:
264 """Filtering with every, some, none and ordering by a 1-M relational field"""
265 with client.batch_() as batcher:
266 batcher.user.create(
267 {
268 'name': 'Robert',
269 'posts': {
270 'create': [
271 {'title': 'My first post', 'published': True},
272 {'title': 'My second post', 'published': False},
273 ]
274 },
275 }
276 )
277 batcher.user.create(
278 {
279 'name': 'Tegan',
280 'posts': {
281 'create': [
282 {'title': 'Hello, world!', 'published': True},
283 {'title': 'My test post', 'published': False},
284 ]
285 },
286 }
287 )
288 batcher.user.create({'name': 'Callum'})
290 user = client.user.find_first_or_raise(
291 where={
292 'posts': {
293 'every': {
294 'title': {
295 'contains': 'post',
296 }
297 }
298 }
299 },
300 )
301 assert user.name == 'Robert'
303 user = client.user.find_first_or_raise(
304 where={
305 'posts': {
306 'none': {
307 'title': {
308 'contains': 'Post',
309 }
310 }
311 }
312 },
313 order={
314 'name': 'asc',
315 },
316 )
317 assert user.name == 'Callum'
319 with pytest.raises(errors.RecordNotFoundError):
320 client.user.find_first_or_raise(
321 where={
322 'posts': {
323 'some': {
324 'title': 'foo',
325 }
326 }
327 }
328 )
330 # ordering
332 user = client.user.find_first_or_raise(
333 where={
334 'posts': {
335 'some': {
336 'title': {
337 'contains': 'post',
338 }
339 }
340 }
341 },
342 order={'name': 'asc'},
343 )
344 assert user.name == 'Robert'
346 user = client.user.find_first_or_raise(
347 where={
348 'posts': {
349 'some': {
350 'title': {
351 'contains': 'post',
352 }
353 }
354 }
355 },
356 order={'name': 'desc'},
357 )
358 assert user.name == 'Tegan'
361def test_list_wrapper_query_transformation(client: Prisma) -> None:
362 """Queries wrapped within a list transform global aliases"""
363 query: UserWhereInput = {
364 'OR': [
365 {'name': {'startswith': '40'}},
366 {'name': {'contains': ', 40'}},
367 {'name': {'contains': 'house'}},
368 ]
369 }
371 client.user.create({'name': 'Robert house'})
372 found = client.user.find_first_or_raise(where=query, order={'created_at': 'asc'})
373 assert found.name == 'Robert house'
375 client.user.create({'name': '40 robert'})
376 found = client.user.find_first_or_raise(skip=1, where=query, order={'created_at': 'asc'})
377 assert found.name == '40 robert'
380def test_distinct(client: Prisma) -> None:
381 """Filtering by distinct combinations of fields"""
382 users = [
383 client.user.create(
384 data={
385 'name': 'Robert',
386 },
387 ),
388 client.user.create(
389 data={
390 'name': 'Tegan',
391 },
392 ),
393 client.user.create(
394 data={
395 'name': 'Patrick',
396 },
397 ),
398 ]
399 with client.batch_() as batcher:
400 batcher.profile.create(
401 {
402 'city': 'Dundee',
403 'country': 'Scotland',
404 'description': 'Foo',
405 'user_id': users[0].id,
406 }
407 )
408 batcher.profile.create(
409 {
410 'city': 'Edinburgh',
411 'country': 'Scotland',
412 'description': 'Foo',
413 'user_id': users[1].id,
414 }
415 )
416 batcher.profile.create(
417 {
418 'city': 'London',
419 'country': 'England',
420 'description': 'Foo',
421 'user_id': users[2].id,
422 }
423 )
425 found = client.profile.find_first_or_raise(
426 where={'country': 'Scotland'},
427 distinct=['city'],
428 order={'city': 'asc'},
429 )
430 assert found.city == 'Dundee'
432 found = client.profile.find_first_or_raise(
433 where={'country': 'Scotland'},
434 distinct=['city'],
435 order={'city': 'desc'},
436 )
437 assert found.city == 'Edinburgh'
440def test_distinct_relations(client: Prisma) -> None:
441 """Using `distinct` across relations"""
442 user = client.user.create(
443 {
444 'name': 'Robert',
445 'posts': {
446 'create': [
447 {
448 'title': 'Post 1',
449 'published': True,
450 },
451 {
452 'title': 'Post 2',
453 'published': False,
454 },
455 {
456 'title': 'Post 3',
457 'published': True,
458 },
459 ]
460 },
461 }
462 )
464 found = client.user.find_first_or_raise(
465 where={
466 'id': user.id,
467 },
468 include={
469 'posts': {
470 'order_by': {'title': 'asc'},
471 'distinct': ['published'],
472 }
473 },
474 )
475 assert found.posts is not None
476 assert len(found.posts) == 2
477 assert found.posts[0].published is True
478 assert found.posts[1].published is False