Coverage for databases/sync_tests/test_include.py: 100%
84 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 typing import List
3import pytest
5from prisma import Prisma
6from lib.testing import async_fixture
7from prisma.models import Post
10@async_fixture(scope='module', name='user_id')
11def user_id_fixture(client: Prisma) -> str:
12 user = client.user.create({'name': 'Robert'})
13 posts = create_or_get_posts(client, user.id)
14 client.custom_category.create(
15 {
16 'name': 'My Category',
17 'posts': {'connect': [{'id': posts[0].id}, {'id': posts[1].id}]},
18 }
19 )
20 return user.id
23@async_fixture(scope='module', name='posts')
24def posts_fixture(client: Prisma, user_id: str) -> List[Post]:
25 return create_or_get_posts(client, user_id)
28def create_or_get_posts(client: Prisma, user_id: str) -> List[Post]:
29 user = client.user.find_unique(
30 where={'id': user_id},
31 include={
32 'posts': {
33 'order_by': {
34 'title': 'asc',
35 },
36 },
37 },
38 )
39 assert user is not None
41 if user.posts:
42 return user.posts
44 return [
45 client.post.create(
46 {
47 'title': 'Post 1',
48 'published': False,
49 'author': {'connect': {'id': user_id}},
50 }
51 ),
52 client.post.create(
53 {
54 'title': 'Post 2',
55 'published': True,
56 'author': {'connect': {'id': user_id}},
57 }
58 ),
59 client.post.create(
60 {
61 'title': 'Post 3',
62 'published': True,
63 'author': {'connect': {'id': user_id}},
64 }
65 ),
66 client.post.create(
67 {
68 'title': 'Post 4',
69 'published': False,
70 'author': {'connect': {'id': user_id}},
71 }
72 ),
73 ]
76@pytest.mark.persist_data
77def test_find_unique_include(client: Prisma, user_id: str) -> None:
78 """Including a one-to-many relationship returns all records as a list of models"""
79 user = client.user.find_unique(
80 where={'id': user_id},
81 include={
82 'posts': {
83 'order_by': {'title': 'asc'},
84 }
85 },
86 )
87 assert user is not None
88 assert user.name == 'Robert'
89 assert user.posts is not None
90 assert len(user.posts) == 4
92 for i, post in enumerate(
93 user.posts,
94 start=1,
95 ):
96 assert post.author is None
97 assert post.author_id == user.id
98 assert post.title == f'Post {i}'
101@pytest.mark.persist_data
102def test_find_unique_include_take(client: Prisma, user_id: str) -> None:
103 """Including a one-to-many relationship with take limits amount of returned models"""
104 user = client.user.find_unique(
105 where={
106 'id': user_id,
107 },
108 include={
109 'posts': {
110 'take': 1,
111 },
112 },
113 )
114 assert user is not None
115 assert user.posts is not None
116 assert len(user.posts) == 1
119@pytest.mark.persist_data
120def test_find_unique_include_where(client: Prisma, user_id: str, posts: List[Post]) -> None:
121 """Including a one-to-many relationship with a where argument filters results"""
122 user = client.user.find_unique(
123 where={'id': user_id},
124 include={'posts': {'where': {'created_at': posts[0].created_at}}},
125 )
126 assert user is not None
127 assert user.posts is not None
128 assert len(user.posts) == 1
129 assert user.posts[0].id == posts[0].id
132@pytest.mark.persist_data
133def test_find_unique_include_pagination(client: Prisma, user_id: str, posts: List[Post]) -> None:
134 """Pagination by cursor id works forwards and backwards"""
135 user = client.user.find_unique(
136 where={'id': user_id},
137 include={
138 'posts': {
139 'cursor': {'id': posts[0].id},
140 'take': 1,
141 'skip': 1,
142 'order_by': {
143 'title': 'asc',
144 },
145 }
146 },
147 )
148 assert user is not None
149 assert user.posts is not None
150 assert len(user.posts) == 1
151 assert user.posts[0].id == posts[1].id
153 user = client.user.find_unique(
154 where={'id': user_id},
155 include={
156 'posts': {
157 'cursor': {'id': posts[1].id},
158 'take': -1,
159 'skip': 1,
160 'order_by': {
161 'title': 'asc',
162 },
163 },
164 },
165 )
166 assert user is not None
167 assert user.posts is not None
168 assert len(user.posts) == 1
169 assert user.posts[0].id == posts[0].id
172@pytest.mark.persist_data
173def test_find_unique_include_nested_where_or(client: Prisma, user_id: str, posts: List[Post]) -> None:
174 """Include with nested or argument"""
175 user = client.user.find_unique(
176 where={'id': user_id},
177 include={
178 'posts': {
179 'where': {
180 'OR': [
181 {'id': posts[0].id},
182 {'published': True},
183 ],
184 },
185 'order_by': {
186 'title': 'asc',
187 },
188 }
189 },
190 )
191 assert user is not None
193 assert posts[0].published is False
194 assert user.posts is not None
195 assert len(user.posts) == 3
197 assert user.posts[0].id == posts[0].id
198 assert user.posts[1].id == posts[1].id
199 assert user.posts[2].id == posts[2].id
201 assert user.posts[0].published is False
202 assert user.posts[1].published is True
203 assert user.posts[2].published is True
206@pytest.mark.persist_data
207def test_find_unique_include_nested_include(client: Prisma, user_id: str) -> None:
208 """Multiple nested include arguments returns all models"""
209 user = client.user.find_unique(
210 where={'id': user_id},
211 include={
212 'profile': True,
213 'posts': {'include': {'categories': {'include': {'posts': True}}}},
214 },
215 )
216 assert user is not None
217 assert user.profile is None
218 assert user.posts is not None
219 for post in user.posts:
220 assert post.categories is not None
221 for category in post.categories:
222 assert category.posts is not None
225@pytest.mark.persist_data
226def test_create_include(client: Prisma) -> None:
227 """Creating a record and including it at the same time"""
228 post = client.post.create(
229 {
230 'title': 'Post 4',
231 'published': False,
232 'author': {'create': {'name': 'Bob'}},
233 },
234 include={'author': True},
235 )
236 assert post.author is not None
237 assert post.author.name == 'Bob'