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

24 statements  

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

1import pytest 

2 

3from prisma import Prisma 

4 

5 

6@pytest.mark.asyncio 

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

8 """Standard usage""" 

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

10 assert total == 2 

11 

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

13 assert user is not None 

14 assert user.name == 'Robert' 

15 

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

17 

18 

19@pytest.mark.asyncio 

20async def test_required_relation_key_field(client: Prisma) -> None: 

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

22 user = await client.user.create( 

23 data={ 

24 'name': 'Robert', 

25 }, 

26 ) 

27 user2 = await client.user.create( 

28 data={ 

29 'name': 'Robert', 

30 }, 

31 ) 

32 count = await client.profile.create_many( 

33 data=[ 

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

35 { 

36 'user_id': user2.id, 

37 'description': 'Foo 2', 

38 'country': 'Scotland', 

39 }, 

40 ], 

41 ) 

42 assert count == 2 

43 

44 found = await client.user.find_unique( 

45 where={ 

46 'id': user.id, 

47 }, 

48 include={ 

49 'profile': True, 

50 }, 

51 ) 

52 assert found is not None 

53 assert found.profile is not None 

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

55 

56 found = await client.user.find_unique( 

57 where={ 

58 'id': user2.id, 

59 }, 

60 include={ 

61 'profile': True, 

62 }, 

63 ) 

64 assert found is not None 

65 assert found.profile is not None 

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