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

15 statements  

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

1import re 

2 

3import pytest 

4 

5from prisma import Prisma 

6from prisma.errors import FieldNotFoundError, ForeignKeyViolationError 

7 

8 

9@pytest.mark.xfail( 

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

11) 

12def test_field_not_found_error(client: Prisma) -> None: 

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

14 both queries and mutations. 

15 """ 

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

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

18 

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

20 client.post.create( 

21 data={ 

22 'title': 'foo', 

23 'published': True, 

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

25 } 

26 ) 

27 

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

29 client.post.find_first( 

30 where={ 

31 'author': { 

32 'is': { # pyright: ignore 

33 'foo': 'bar', 

34 }, 

35 }, 

36 }, 

37 ) 

38 

39 

40def test_foreign_key_violation_error(client: Prisma) -> None: 

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

42 with pytest.raises( 

43 ForeignKeyViolationError, 

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

45 ): 

46 client.post.create( 

47 data={ 

48 'title': 'foo', 

49 'published': True, 

50 'author_id': 'cjld2cjxh0000qzrmn831i7rn', 

51 } 

52 )