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

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-28 15:17 +0000

1import pytest 

2 

3import prisma 

4from prisma import Prisma 

5 

6 

7def test_create_many(client: Prisma) -> None: 

8 """Standard usage""" 

9 total = client.user.create_many([{'name': 'Robert'}, {'name': 'Tegan'}]) 

10 assert total == 2 

11 

12 user = client.user.find_first(where={'name': 'Robert'}) 

13 assert user is not None 

14 assert user.name == 'Robert' 

15 

16 assert client.user.count() == 2 

17 

18 

19def test_skip_duplicates(client: Prisma) -> None: 

20 """Skipping duplcates ignores unique constraint errors""" 

21 user = client.user.create({'name': 'Robert'}) 

22 

23 with pytest.raises(prisma.errors.UniqueViolationError) as exc: 

24 client.user.create_many([{'id': user.id, 'name': 'Robert 2'}]) 

25 

26 assert exc.match(r'Unique constraint failed') 

27 

28 count = client.user.create_many( 

29 [{'id': user.id, 'name': 'Robert 2'}, {'name': 'Tegan'}], 

30 skip_duplicates=True, 

31 ) 

32 assert count == 1 

33 

34 

35def test_required_relation_key_field(client: Prisma) -> None: 

36 """Explicitly passing a field used as a foreign key connects the relations""" 

37 user = client.user.create( 

38 data={ 

39 'name': 'Robert', 

40 }, 

41 ) 

42 user2 = client.user.create( 

43 data={ 

44 'name': 'Robert', 

45 }, 

46 ) 

47 count = client.profile.create_many( 

48 data=[ 

49 {'user_id': user.id, 'description': 'Foo', 'country': 'Scotland'}, 

50 { 

51 'user_id': user2.id, 

52 'description': 'Foo 2', 

53 'country': 'Scotland', 

54 }, 

55 ], 

56 ) 

57 assert count == 2 

58 

59 found = client.user.find_unique( 

60 where={ 

61 'id': user.id, 

62 }, 

63 include={ 

64 'profile': True, 

65 }, 

66 ) 

67 assert found is not None 

68 assert found.profile is not None 

69 assert found.profile.description == 'Foo' 

70 

71 found = client.user.find_unique( 

72 where={ 

73 'id': user2.id, 

74 }, 

75 include={ 

76 'profile': True, 

77 }, 

78 ) 

79 assert found is not None 

80 assert found.profile is not None 

81 assert found.profile.description == 'Foo 2'