Coverage for databases/sync_tests/test_create.py: 100%
58 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import pytest
3from prisma import Prisma, errors
4from lib.testing import assert_time_like_now
7def test_create(client: Prisma) -> None:
8 """Basic record creation"""
9 post = client.post.create(
10 {
11 'title': 'Hi from Prisma!',
12 'published': True,
13 'description': 'Prisma is a database toolkit that makes databases easy.',
14 }
15 )
16 assert isinstance(post.id, str)
17 assert post.title == 'Hi from Prisma!'
18 assert post.description == 'Prisma is a database toolkit that makes databases easy.'
19 assert post.published is True
20 assert_time_like_now(post.created_at)
21 assert_time_like_now(post.updated_at)
23 user = client.user.create(
24 {
25 'name': 'Robert',
26 }
27 )
28 assert user.name == 'Robert'
29 assert isinstance(user.id, str)
32def test_create_with_relationship(client: Prisma) -> None:
33 """Creating a record with a nested relationship record creation"""
34 post = client.post.create(
35 {
36 'published': False,
37 'title': 'Post 1',
38 'author': {'create': {'name': 'Bob'}},
39 },
40 include={'author': True},
41 )
42 assert post.author is not None
43 assert post.author.name == 'Bob'
45 found = client.user.find_unique(where={'id': post.author.id})
46 assert found is not None
47 assert found.name == 'Bob'
50def test_create_missing_required_args(client: Prisma) -> None:
51 """Trying to create a record with a missing required field raises an error"""
52 with pytest.raises(TypeError):
53 client.post.create() # type: ignore[call-arg]
55 with pytest.raises(errors.MissingRequiredValueError):
56 client.post.create(
57 { # type: ignore[typeddict-item]
58 'published': False,
59 }
60 )
63def test_create_unique_violation(client: Prisma) -> None:
64 """Creating the same record twice raises an error"""
65 user = client.user.create({'name': 'Robert', 'id': 'user-1'})
66 assert user.id == 'user-1'
67 assert user.name == 'Robert'
69 with pytest.raises(errors.UniqueViolationError):
70 client.user.create({'name': 'Robert', 'id': 'user-1'})
73def test_setting_field_to_null(client: Prisma) -> None:
74 """Creating a field with a None value sets the database record to None"""
75 post = client.post.create(
76 data={
77 'title': 'Post',
78 'published': False,
79 'description': None,
80 },
81 )
82 assert post.description is None
85def test_setting_non_nullable_field_to_null(client: Prisma) -> None:
86 """Attempting to create a record with a non-nullable field set to null raises an error"""
87 with pytest.raises(errors.MissingRequiredValueError) as exc:
88 client.post.create(
89 data={
90 'title': 'Post',
91 'published': None, # type: ignore
92 },
93 )
95 assert exc.match(r'published')
98def test_nullable_relational_field(client: Prisma) -> None:
99 """Relational fields cannot be set to None"""
100 with pytest.raises(errors.MissingRequiredValueError) as exc:
101 client.post.create(
102 data={'title': 'Post', 'published': False, 'author': None} # type: ignore
103 )
105 assert exc.match(r'author')
108def test_required_relation_key_field(client: Prisma) -> None:
109 """Explicitly passing a field used as a foreign key connects the relation"""
110 user = client.user.create(
111 data={
112 'name': 'Robert',
113 },
114 )
115 profile = client.profile.create(
116 data={
117 'description': 'My bio!',
118 'country': 'Scotland',
119 'user_id': user.id,
120 },
121 include={
122 'user': True,
123 },
124 )
125 assert profile.user is not None
126 assert profile.user.id == user.id
127 assert profile.user.name == 'Robert'
130def test_connect_or_create(client: Prisma) -> None:
131 """Connect or create a relation"""
132 user = client.user.create(
133 data={
134 'name': 'Robert',
135 },
136 )
138 post = client.post.create(
139 data={
140 'title': 'Post 1',
141 'published': True,
142 'author': {
143 'connect_or_create': {
144 'where': {
145 'id': user.id,
146 },
147 'create': {
148 'name': 'Robert',
149 },
150 },
151 },
152 },
153 include={
154 'author': True,
155 },
156 )
158 assert post.author is not None
159 assert post.author.id == user.id
161 post2 = client.post.create(
162 data={
163 'title': 'Post 2',
164 'published': False,
165 'author': {
166 'connect_or_create': {
167 'where': {
168 'id': 'non-existent',
169 },
170 'create': {
171 'name': 'Bobert',
172 },
173 },
174 },
175 },
176 include={
177 'author': True,
178 },
179 )
181 assert post2.author is not None
182 assert post2.author.id != user.id
183 assert post2.author.name == 'Bobert'