Coverage for databases/tests/test_errors.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1import re 

2 

3import pytest 

4 

5import prisma 

6from prisma import Prisma 

7from prisma.errors import FieldNotFoundError, ForeignKeyViolationError 

8 

9 

10@pytest.mark.xfail( 

11 reason='This was broken in v5, we now raise a different error - nobody should be relying on this behaviour and its tricky to fix' 

12) 

13@pytest.mark.asyncio 

14async def test_field_not_found_error(client: Prisma) -> None: 

15 """The FieldNotFoundError is raised when an unknown field is passed to 

16 both queries and mutations. 

17 """ 

18 with pytest.raises(FieldNotFoundError, match='bad_field'): 

19 await client.post.find_first(where={'bad_field': 'foo'}) # type: ignore 

20 

21 with pytest.raises(FieldNotFoundError, match='data.foo'): 

22 await client.post.create( 

23 data={ 

24 'title': 'foo', 

25 'published': True, 

26 'foo': 'bar', # pyright: ignore 

27 } 

28 ) 

29 

30 with pytest.raises(FieldNotFoundError, match='where.author.is.foo'): 

31 await client.post.find_first( 

32 where={ 

33 'author': { 

34 'is': { # pyright: ignore 

35 'foo': 'bar', 

36 }, 

37 }, 

38 }, 

39 ) 

40 

41 

42@pytest.mark.asyncio 

43@pytest.mark.prisma 

44async def test_field_not_found_error_selection() -> None: 

45 """The FieldNotFoundError is raised when an unknown field is passed to selections.""" 

46 

47 class CustomPost(prisma.bases.BasePost): 

48 foo_field: str 

49 

50 with pytest.raises( 

51 FieldNotFoundError, 

52 match=r'Field \'foo_field\' not found in enclosing type \'Post\'', 

53 ): 

54 await CustomPost.prisma().find_first() 

55 

56 

57@pytest.mark.asyncio 

58async def test_foreign_key_violation_error(client: Prisma) -> None: 

59 """The ForeignKeyViolationError is raised when a foreign key is invalid.""" 

60 with pytest.raises( 

61 ForeignKeyViolationError, 

62 match=re.compile(r'foreign key constraint failed on the field', re.IGNORECASE), 

63 ): 

64 await client.post.create( 

65 data={ 

66 'title': 'foo', 

67 'published': True, 

68 'author_id': 'cjld2cjxh0000qzrmn831i7rn', 

69 } 

70 )