-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_batch.py
281 lines (216 loc) · 9.04 KB
/
test_batch.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
import pytest
import prisma
from prisma import Prisma
from ..utils import RawQueries, DatabaseConfig
@pytest.mark.asyncio
async def test_base_usage(client: Prisma) -> None:
"""Basic non context manager usage"""
batcher = client.batch_()
batcher.user.create({'name': 'Robert'})
batcher.user.create({'name': 'Tegan'})
await batcher.commit()
user = await client.user.find_first(where={'name': 'Robert'})
assert user is not None
assert user.name == 'Robert'
user = await client.user.find_first(where={'name': 'Tegan'})
assert user is not None
assert user.name == 'Tegan'
@pytest.mark.asyncio
async def test_context_manager(client: Prisma) -> None:
"""Basic usage with a context manager"""
async with client.batch_() as batcher:
batcher.user.create({'name': 'Robert'})
batcher.user.create({'name': 'Tegan'})
user = await client.user.find_first(where={'name': 'Robert'})
assert user is not None
assert user.name == 'Robert'
user = await client.user.find_first(where={'name': 'Tegan'})
assert user is not None
assert user.name == 'Tegan'
@pytest.mark.asyncio
async def test_batch_error(client: Prisma) -> None:
"""Error while committing does not commit any records"""
with pytest.raises(prisma.errors.UniqueViolationError) as exc:
batcher = client.batch_()
batcher.user.create({'id': 'abc', 'name': 'Robert'})
batcher.user.create({'id': 'abc', 'name': 'Robert 2'})
await batcher.commit()
assert exc.match(r'Unique constraint failed')
assert await client.user.count() == 0
@pytest.mark.asyncio
async def test_context_manager_error(client: Prisma) -> None:
"""Error exiting context manager does not commit any records"""
with pytest.raises(prisma.errors.UniqueViolationError) as exc:
async with client.batch_() as batcher:
batcher.user.create({'id': 'abc', 'name': 'Robert'})
batcher.user.create({'id': 'abc', 'name': 'Robert 2'})
assert exc.match(r'Unique constraint failed')
assert await client.user.count() == 0
@pytest.mark.asyncio
async def test_context_manager_throws_error(client: Prisma) -> None:
"""Context manager respects errors"""
with pytest.raises(RuntimeError) as exc:
async with client.batch_() as batcher:
batcher.user.create({'name': 'Robert'})
raise RuntimeError('Example error')
assert exc.match('Example error')
assert await client.user.count() == 0
@pytest.mark.asyncio
async def test_mixing_models(client: Prisma) -> None:
"""Batching queries to multiple models works as intended"""
async with client.batch_() as batcher:
# NOTE: this is just to test functionality, the better method
# for acheiving this is to use nested writes with user.create
# client.user.create({'name': 'Robert', 'profile': {'create': {'bio': 'Robert\'s profile'}}})
batcher.user.create({'id': 'abc', 'name': 'Robert'})
batcher.profile.create(
{
'user': {'connect': {'id': 'abc'}},
'description': "Robert's profile",
'country': 'Scotland',
}
)
user = await client.user.find_first(where={'name': 'Robert'}, include={'profile': True})
assert user is not None
assert user.name == 'Robert'
assert user.profile is not None
assert user.profile.description == "Robert's profile"
assert await client.user.count() == 1
assert await client.profile.count() == 1
@pytest.mark.asyncio
async def test_mixing_actions(client: Prisma) -> None:
"""Batching queries to different operations works as intended"""
async with client.batch_() as batcher:
batcher.user.create({'name': 'Robert'})
batcher.user.delete_many(where={'name': 'Robert'})
assert await client.user.count() == 0
@pytest.mark.asyncio
async def test_reusing_batcher(client: Prisma) -> None:
"""Reusing the same batcher does not commit the same query multiple times"""
batcher = client.batch_()
batcher.user.create({'name': 'Robert'})
await batcher.commit()
assert await client.user.count() == 1
batcher.user.create({'name': 'Robert 2'})
await batcher.commit()
assert await client.user.count() == 2
@pytest.mark.asyncio
async def test_large_query(client: Prisma) -> None:
"""Batching a lot of queries works"""
async with client.batch_() as batcher:
for i in range(1000):
batcher.user.create({'name': f'User {i}'})
assert await client.user.count() == 1000
@pytest.mark.asyncio
async def test_delete(client: Prisma) -> None:
"""delete action works as suggested"""
user = await client.user.create({'name': 'Robert'})
assert await client.user.find_first(where={'id': user.id}) is not None
async with client.batch_() as batcher:
batcher.user.delete(where={'id': user.id})
assert await client.user.find_first(where={'id': user.id}) is None
@pytest.mark.asyncio
async def test_update(client: Prisma) -> None:
"""update action works as suggested"""
user = await client.user.create({'name': 'Robert'})
assert await client.user.find_first(where={'id': user.id}) is not None
async with client.batch_() as batcher:
batcher.user.update(where={'id': user.id}, data={'name': 'Roberto'})
new = await client.user.find_first(where={'id': user.id})
assert new is not None
assert new.id == user.id
assert new.name == 'Roberto'
@pytest.mark.asyncio
async def test_upsert(client: Prisma) -> None:
"""upsert action works as suggested"""
user_id = 'abc123'
assert await client.user.find_unique(where={'id': user_id}) is None
async with client.batch_() as batcher:
batcher.user.upsert(
where={'id': user_id},
data={
'create': {'id': user_id, 'name': 'Robert'},
'update': {'name': 'Robert'},
},
)
user = await client.user.find_unique(where={'id': user_id})
assert user is not None
assert user.id == user_id
assert user.name == 'Robert'
async with client.batch_() as batcher:
batcher.user.upsert(
where={'id': user_id},
data={
'create': {'id': user_id, 'name': 'Robert'},
'update': {'name': 'Roberto'},
},
)
user = await client.user.find_unique(where={'id': user_id})
assert user is not None
assert user.id == user_id
assert user.name == 'Roberto'
assert await client.user.count() == 1
@pytest.mark.asyncio
async def test_update_many(client: Prisma) -> None:
"""update_many action works as suggested"""
await client.user.create({'name': 'Robert'})
await client.user.create({'name': 'Robert 2'})
async with client.batch_() as batcher:
batcher.user.update_many(where={'name': {'startswith': 'Robert'}}, data={'name': 'Robert'})
users = await client.user.find_many()
assert len(users) == 2
assert users[0].name == 'Robert'
assert users[1].name == 'Robert'
@pytest.mark.asyncio
async def test_delete_many(client: Prisma) -> None:
"""delete_many action works as suggested"""
await client.user.create({'name': 'Robert'})
await client.user.create({'name': 'Robert 2'})
assert await client.user.count() == 2
async with client.batch_() as batcher:
batcher.user.delete_many(where={'name': {'startswith': 'Robert'}})
assert await client.user.count() == 0
@pytest.mark.asyncio
async def test_execute_raw(client: Prisma, raw_queries: RawQueries) -> None:
"""execute_raw action can be used to execute raw SQL queries"""
post1 = await client.post.create(
{
'title': 'My first post!',
'published': False,
}
)
post2 = await client.post.create(
{
'title': 'My 2nd post.',
'published': False,
}
)
async with client.batch_() as batcher:
batcher.execute_raw(
raw_queries.update_unique_post_title,
post1.id,
)
batcher.execute_raw(
raw_queries.update_unique_post_new_title,
post2.id,
)
found = await client.post.find_unique(where={'id': post1.id})
assert found is not None
assert found.id == post1.id
assert found.title == 'My edited title'
found = await client.post.find_unique(where={'id': post2.id})
assert found is not None
assert found.id == post2.id
assert found.title == 'My new title'
@pytest.mark.asyncio
async def test_create_many_skip_duplicates_unsupported(
client: Prisma,
config: DatabaseConfig,
) -> None:
"""Cannot call create_many with skip_duplicates on databases that do not support it"""
if 'create_many_skip_duplicates' not in config.unsupported_features:
pytest.skip('The create_many skip_duplicates argument is supported by the current database')
with pytest.raises(prisma.errors.UnsupportedDatabaseError) as exc:
async with client.batch_() as batcher:
batcher.user.create_many([{'name': 'Robert'}], skip_duplicates=True)
assert exc.match(r'skip_duplicates is not supported')