-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
test_errors.py
70 lines (58 loc) · 2.12 KB
/
test_errors.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
import re
import pytest
import prisma
from prisma import Prisma
from prisma.errors import FieldNotFoundError, ForeignKeyViolationError
@pytest.mark.xfail(
reason='This was broken in v5, we now raise a different error - nobody should be relying on this behaviour and its tricky to fix'
)
@pytest.mark.asyncio
async def test_field_not_found_error(client: Prisma) -> None:
"""The FieldNotFoundError is raised when an unknown field is passed to
both queries and mutations.
"""
with pytest.raises(FieldNotFoundError, match='bad_field'):
await client.post.find_first(where={'bad_field': 'foo'}) # type: ignore
with pytest.raises(FieldNotFoundError, match='data.foo'):
await client.post.create(
data={
'title': 'foo',
'published': True,
'foo': 'bar', # pyright: ignore
}
)
with pytest.raises(FieldNotFoundError, match='where.author.is.foo'):
await client.post.find_first(
where={
'author': {
'is': { # pyright: ignore
'foo': 'bar',
},
},
},
)
@pytest.mark.asyncio
@pytest.mark.prisma
async def test_field_not_found_error_selection() -> None:
"""The FieldNotFoundError is raised when an unknown field is passed to selections."""
class CustomPost(prisma.bases.BasePost):
foo_field: str
with pytest.raises(
FieldNotFoundError,
match=r'Field \'foo_field\' not found in enclosing type \'Post\'',
):
await CustomPost.prisma().find_first()
@pytest.mark.asyncio
async def test_foreign_key_violation_error(client: Prisma) -> None:
"""The ForeignKeyViolationError is raised when a foreign key is invalid."""
with pytest.raises(
ForeignKeyViolationError,
match=re.compile(r'foreign key constraint failed on the field', re.IGNORECASE),
):
await client.post.create(
data={
'title': 'foo',
'published': True,
'author_id': 'cjld2cjxh0000qzrmn831i7rn',
}
)