Coverage for databases/sync_tests/test_models.py: 100%
108 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.models import User
5from ..utils import RawQueries
8def create_user() -> User:
9 user = User.prisma().create(
10 data={
11 'name': 'Robert',
12 },
13 )
14 assert isinstance(user, User)
15 return user
18@pytest.mark.prisma
19def test_create() -> None:
20 """Creating a record using model-based access"""
21 user = User.prisma().create({'name': 'Robert'})
22 assert isinstance(user, User)
23 assert user.name == 'Robert'
25 user = User.prisma().create(
26 data={
27 'name': 'Tegan',
28 'posts': {
29 'create': {
30 'title': 'My first post!',
31 'published': True,
32 },
33 },
34 },
35 include={
36 'posts': True,
37 },
38 )
39 assert user.name == 'Tegan'
40 assert user.posts is not None
41 assert len(user.posts) == 1
42 assert user.posts[0].title == 'My first post!'
45@pytest.mark.prisma
46def test_delete() -> None:
47 """Deleting a record using model-based access"""
48 deleted = User.prisma().delete(
49 where={
50 'id': 'abc',
51 },
52 )
53 assert deleted is None
55 user = User.prisma().create({'name': 'Robert'})
56 assert isinstance(user, User)
58 deleted2 = User.prisma().delete(
59 where={
60 'id': user.id,
61 },
62 )
63 assert deleted2 is not None
64 assert deleted2.name == 'Robert'
67@pytest.mark.prisma
68def test_find_unique() -> None:
69 """Finding a unique record using model-based access"""
70 found = User.prisma().find_unique(
71 where={
72 'id': 'abc',
73 },
74 )
75 assert found is None
77 user = create_user()
78 found = User.prisma().find_unique(
79 where={
80 'id': user.id,
81 },
82 )
83 assert found is not None
84 assert found.name == 'Robert'
87@pytest.mark.prisma
88def test_find_many() -> None:
89 """Finding many records using model-based access"""
90 users = [
91 User.prisma().create({'name': 'Robert'}),
92 User.prisma().create({'name': 'Robert 2'}),
93 User.prisma().create({'name': 'Tegan'}),
94 ]
96 found = User.prisma().find_many(
97 where={
98 'name': {
99 'startswith': 'Robert',
100 },
101 },
102 order={
103 'id': 'asc',
104 },
105 )
106 assert len(found) == 2
107 assert found[0].id == users[0].id
108 assert found[1].id == users[1].id
111@pytest.mark.prisma
112def test_find_first() -> None:
113 """Finding a record using model-based access"""
114 found = User.prisma().find_first(where={'name': 'Robert'})
115 assert found is None
117 create_user()
119 found = User.prisma().find_first(where={'name': 'Robert'})
120 assert found is not None
121 assert found.name == 'Robert'
124@pytest.mark.prisma
125def test_update() -> None:
126 """Updating a record using model-based access"""
127 user = create_user()
128 assert user.name == 'Robert'
130 updated = User.prisma().update(
131 data={
132 'name': 'Tegan',
133 },
134 where={
135 'id': user.id,
136 },
137 )
138 assert updated is not None
139 assert updated.id == user.id
140 assert updated.name == 'Tegan'
143@pytest.mark.prisma
144def test_upsert() -> None:
145 """Upserting a record using model-based access"""
146 user = create_user()
147 new = User.prisma().upsert(
148 where={
149 'id': user.id,
150 },
151 data={
152 'create': {
153 'name': 'Robert',
154 },
155 'update': {
156 'name': 'Robert 2',
157 },
158 },
159 )
160 assert new.name == 'Robert 2'
163@pytest.mark.prisma
164def test_update_many() -> None:
165 """Updating many records using model-based access"""
166 users = [
167 User.prisma().create({'name': 'Robert'}),
168 User.prisma().create({'name': 'Robert 2'}),
169 User.prisma().create({'name': 'Tegan'}),
170 ]
171 total = User.prisma().update_many(
172 where={
173 'name': {
174 'startswith': 'Robert',
175 },
176 },
177 data={
178 'name': 'Robert',
179 },
180 )
181 assert total == 2
183 for id_ in [users[0].id, users[1].id]:
184 user = User.prisma().find_unique(
185 where={
186 'id': id_,
187 },
188 )
189 assert user is not None
190 assert user.name == 'Robert'
192 user = User.prisma().find_unique(
193 where={
194 'id': users[2].id,
195 },
196 )
197 assert user is not None
198 assert user.name == 'Tegan'
201@pytest.mark.prisma
202def test_count() -> None:
203 """Counting records using model-based access"""
204 assert User.prisma().count() == 0
205 create_user()
206 assert User.prisma().count() == 1
208 total = User.prisma().count(where={'NOT': [{'name': 'Robert'}]})
209 assert total == 0
212@pytest.mark.prisma
213def test_delete_many() -> None:
214 """Deleting many records using model-based access"""
215 _ = [
216 User.prisma().create({'name': 'Robert'}),
217 User.prisma().create({'name': 'Robert 2'}),
218 User.prisma().create({'name': 'Tegan'}),
219 ]
220 total = User.prisma().delete_many(
221 where={
222 'name': 'Tegan',
223 },
224 )
225 assert total == 1
226 assert User.prisma().count() == 2
229@pytest.mark.prisma
230def test_query_raw(raw_queries: RawQueries) -> None:
231 """Ensure results are transformed to the expected BaseModel"""
232 users = [
233 User.prisma().create({'name': 'Robert'}),
234 User.prisma().create({'name': 'Tegan'}),
235 ]
236 results = User.prisma().query_raw(
237 raw_queries.find_user_by_id,
238 users[1].id,
239 )
240 assert len(results) == 1
241 assert results[0].name == 'Tegan'
244@pytest.mark.prisma
245def test_query_first(raw_queries: RawQueries) -> None:
246 """Ensure results are transformed to the expected BaseModel"""
247 users = [
248 User.prisma().create({'name': 'Robert'}),
249 User.prisma().create({'name': 'Tegan'}),
250 ]
251 user = User.prisma().query_first(raw_queries.find_user_by_id_limit_1, users[1].id)
252 assert user is not None
253 assert user.name == 'Tegan'
256class MyUser(User):
257 @property
258 def info(self) -> str:
259 return f'{self.id}: {self.name}'
262@pytest.mark.prisma
263def test_custom_model() -> None:
264 """Subclassed prisma model is returned by actions"""
265 user = MyUser.prisma().create(
266 data={
267 'name': 'Robert',
268 },
269 )
270 assert user.info == f'{user.id}: Robert'
271 assert isinstance(user, MyUser)