-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_models.py
284 lines (241 loc) · 6.91 KB
/
test_models.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
import pytest
from prisma.models import User
from ..utils import RawQueries
async def create_user() -> User:
user = await User.prisma().create(
data={
'name': 'Robert',
},
)
assert isinstance(user, User)
return user
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_create() -> None:
"""Creating a record using model-based access"""
user = await User.prisma().create({'name': 'Robert'})
assert isinstance(user, User)
assert user.name == 'Robert'
user = await User.prisma().create(
data={
'name': 'Tegan',
'posts': {
'create': {
'title': 'My first post!',
'published': True,
},
},
},
include={
'posts': True,
},
)
assert user.name == 'Tegan'
assert user.posts is not None
assert len(user.posts) == 1
assert user.posts[0].title == 'My first post!'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_delete() -> None:
"""Deleting a record using model-based access"""
deleted = await User.prisma().delete(
where={
'id': 'abc',
},
)
assert deleted is None
user = await User.prisma().create({'name': 'Robert'})
assert isinstance(user, User)
deleted2 = await User.prisma().delete(
where={
'id': user.id,
},
)
assert deleted2 is not None
assert deleted2.name == 'Robert'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_find_unique() -> None:
"""Finding a unique record using model-based access"""
found = await User.prisma().find_unique(
where={
'id': 'abc',
},
)
assert found is None
user = await create_user()
found = await User.prisma().find_unique(
where={
'id': user.id,
},
)
assert found is not None
assert found.name == 'Robert'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_find_many() -> None:
"""Finding many records using model-based access"""
users = [
await User.prisma().create({'name': 'Robert'}),
await User.prisma().create({'name': 'Robert 2'}),
await User.prisma().create({'name': 'Tegan'}),
]
found = await User.prisma().find_many(
where={
'name': {
'startswith': 'Robert',
},
},
order={
'id': 'asc',
},
)
assert len(found) == 2
assert found[0].id == users[0].id
assert found[1].id == users[1].id
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_find_first() -> None:
"""Finding a record using model-based access"""
found = await User.prisma().find_first(where={'name': 'Robert'})
assert found is None
await create_user()
found = await User.prisma().find_first(where={'name': 'Robert'})
assert found is not None
assert found.name == 'Robert'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_update() -> None:
"""Updating a record using model-based access"""
user = await create_user()
assert user.name == 'Robert'
updated = await User.prisma().update(
data={
'name': 'Tegan',
},
where={
'id': user.id,
},
)
assert updated is not None
assert updated.id == user.id
assert updated.name == 'Tegan'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_upsert() -> None:
"""Upserting a record using model-based access"""
user = await create_user()
new = await User.prisma().upsert(
where={
'id': user.id,
},
data={
'create': {
'name': 'Robert',
},
'update': {
'name': 'Robert 2',
},
},
)
assert new.name == 'Robert 2'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_update_many() -> None:
"""Updating many records using model-based access"""
users = [
await User.prisma().create({'name': 'Robert'}),
await User.prisma().create({'name': 'Robert 2'}),
await User.prisma().create({'name': 'Tegan'}),
]
total = await User.prisma().update_many(
where={
'name': {
'startswith': 'Robert',
},
},
data={
'name': 'Robert',
},
)
assert total == 2
for id_ in [users[0].id, users[1].id]:
user = await User.prisma().find_unique(
where={
'id': id_,
},
)
assert user is not None
assert user.name == 'Robert'
user = await User.prisma().find_unique(
where={
'id': users[2].id,
},
)
assert user is not None
assert user.name == 'Tegan'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_count() -> None:
"""Counting records using model-based access"""
assert await User.prisma().count() == 0
await create_user()
assert await User.prisma().count() == 1
total = await User.prisma().count(where={'NOT': [{'name': 'Robert'}]})
assert total == 0
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_delete_many() -> None:
"""Deleting many records using model-based access"""
_ = [
await User.prisma().create({'name': 'Robert'}),
await User.prisma().create({'name': 'Robert 2'}),
await User.prisma().create({'name': 'Tegan'}),
]
total = await User.prisma().delete_many(
where={
'name': 'Tegan',
},
)
assert total == 1
assert await User.prisma().count() == 2
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_query_raw(raw_queries: RawQueries) -> None:
"""Ensure results are transformed to the expected BaseModel"""
users = [
await User.prisma().create({'name': 'Robert'}),
await User.prisma().create({'name': 'Tegan'}),
]
results = await User.prisma().query_raw(
raw_queries.find_user_by_id,
users[1].id,
)
assert len(results) == 1
assert results[0].name == 'Tegan'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_query_first(raw_queries: RawQueries) -> None:
"""Ensure results are transformed to the expected BaseModel"""
users = [
await User.prisma().create({'name': 'Robert'}),
await User.prisma().create({'name': 'Tegan'}),
]
user = await User.prisma().query_first(raw_queries.find_user_by_id_limit_1, users[1].id)
assert user is not None
assert user.name == 'Tegan'
class MyUser(User):
@property
def info(self) -> str:
return f'{self.id}: {self.name}'
@pytest.mark.prisma
@pytest.mark.asyncio
async def test_custom_model() -> None:
"""Subclassed prisma model is returned by actions"""
user = await MyUser.prisma().create(
data={
'name': 'Robert',
},
)
assert user.info == f'{user.id}: Robert'
assert isinstance(user, MyUser)