Coverage for databases/tests/test_create_many.py: 100%
33 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-28 15:17 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-28 15:17 +0000
1import pytest
3import prisma
4from prisma import Prisma
7@pytest.mark.asyncio
8async def test_create_many(client: Prisma) -> None:
9 """Standard usage"""
10 total = await client.user.create_many([{'name': 'Robert'}, {'name': 'Tegan'}])
11 assert total == 2
13 user = await client.user.find_first(where={'name': 'Robert'})
14 assert user is not None
15 assert user.name == 'Robert'
17 assert await client.user.count() == 2
20@pytest.mark.asyncio
21async def test_skip_duplicates(client: Prisma) -> None:
22 """Skipping duplcates ignores unique constraint errors"""
23 user = await client.user.create({'name': 'Robert'})
25 with pytest.raises(prisma.errors.UniqueViolationError) as exc:
26 await client.user.create_many([{'id': user.id, 'name': 'Robert 2'}])
28 assert exc.match(r'Unique constraint failed')
30 count = await client.user.create_many(
31 [{'id': user.id, 'name': 'Robert 2'}, {'name': 'Tegan'}],
32 skip_duplicates=True,
33 )
34 assert count == 1
37@pytest.mark.asyncio
38async def test_required_relation_key_field(client: Prisma) -> None:
39 """Explicitly passing a field used as a foreign key connects the relations"""
40 user = await client.user.create(
41 data={
42 'name': 'Robert',
43 },
44 )
45 user2 = await client.user.create(
46 data={
47 'name': 'Robert',
48 },
49 )
50 count = await client.profile.create_many(
51 data=[
52 {'user_id': user.id, 'description': 'Foo', 'country': 'Scotland'},
53 {
54 'user_id': user2.id,
55 'description': 'Foo 2',
56 'country': 'Scotland',
57 },
58 ],
59 )
60 assert count == 2
62 found = await client.user.find_unique(
63 where={
64 'id': user.id,
65 },
66 include={
67 'profile': True,
68 },
69 )
70 assert found is not None
71 assert found.profile is not None
72 assert found.profile.description == 'Foo'
74 found = await client.user.find_unique(
75 where={
76 'id': user2.id,
77 },
78 include={
79 'profile': True,
80 },
81 )
82 assert found is not None
83 assert found.profile is not None
84 assert found.profile.description == 'Foo 2'