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