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
« prev ^ index » next coverage.py v7.2.7, created at 2024-04-28 15:17 +0000
1import pytest
3import prisma
4from prisma import Prisma
7def test_create_many(client: Prisma) -> None:
8 """Standard usage"""
9 total = client.user.create_many([{'name': 'Robert'}, {'name': 'Tegan'}])
10 assert total == 2
12 user = client.user.find_first(where={'name': 'Robert'})
13 assert user is not None
14 assert user.name == 'Robert'
16 assert client.user.count() == 2
19def test_skip_duplicates(client: Prisma) -> None:
20 """Skipping duplcates ignores unique constraint errors"""
21 user = client.user.create({'name': 'Robert'})
23 with pytest.raises(prisma.errors.UniqueViolationError) as exc:
24 client.user.create_many([{'id': user.id, 'name': 'Robert 2'}])
26 assert exc.match(r'Unique constraint failed')
28 count = client.user.create_many(
29 [{'id': user.id, 'name': 'Robert 2'}, {'name': 'Tegan'}],
30 skip_duplicates=True,
31 )
32 assert count == 1
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
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'
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'