Coverage for databases/sync_tests/test_find_unique_or_raise.py: 100%
74 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
6def test_id_field(client: Prisma) -> None:
7 """Finding a record by an ID field"""
8 post = client.post.create(
9 {
10 'title': 'My post title!',
11 'published': False,
12 }
13 )
14 assert isinstance(post.id, str)
16 found = client.post.find_unique_or_raise(where={'id': post.id})
17 assert found == post
20def test_missing_required_args(client: Prisma) -> None:
21 """Missing field raises an error"""
22 with pytest.raises(TypeError):
23 client.post.find_unique_or_raise() # type: ignore[call-arg]
25 # TODO: more constrained error type
26 with pytest.raises(errors.DataError):
27 client.post.find_unique_or_raise(
28 {
29 'title': 'Hi from Prisma!', # pyright: ignore
30 }
31 )
34def test_no_match(client: Prisma) -> None:
35 """Looking for non-existent record raises an error"""
36 with pytest.raises(
37 errors.RecordNotFoundError,
38 match=r'depends on one or more records that were required but not found',
39 ):
40 client.post.find_unique_or_raise(where={'id': 'sjbsjahs'})
43def test_multiple_fields_are_not_allowed(client: Prisma) -> None:
44 """Multiple fields cannot be passed at once"""
45 with pytest.raises(errors.DataError):
46 client.user.find_unique_or_raise(
47 where={
48 'id': 'foo',
49 'email': 'foo', # type: ignore
50 },
51 )
54def test_unique1(client: Prisma) -> None:
55 """Standard combined unique constraint"""
56 model = client.unique1.create(
57 data={
58 'name': 'Robert',
59 'surname': 'Craigie',
60 },
61 )
62 found = client.unique1.find_unique_or_raise(
63 where={
64 'name_surname': {
65 'name': 'Robert',
66 'surname': 'Craigie',
67 },
68 },
69 )
70 assert found.name == model.name
71 assert found.surname == model.surname
74def test_unique2(client: Prisma) -> None:
75 """Combined unique constraint with an aditional unique field"""
76 model = client.unique2.create(
77 data={
78 'name': 'Robert',
79 'surname': 'Craigie',
80 },
81 )
83 found = client.unique2.find_unique_or_raise(
84 where={
85 'name_surname': {
86 'name': 'Robert',
87 'surname': 'Craigie',
88 },
89 },
90 )
91 assert found.name == model.name
92 assert found.surname == model.surname
94 found = client.unique2.find_unique_or_raise(
95 where={
96 'surname': 'Craigie',
97 },
98 )
99 assert found.name == model.name
100 assert found.surname == model.surname
103def test_unique3(client: Prisma) -> None:
104 """Combined unique constraint with an ID field and a unique field"""
105 model = client.unique3.create(
106 data={
107 'name': 'Robert',
108 'surname': 'Craigie',
109 },
110 )
112 found = client.unique3.find_unique_or_raise(
113 where={
114 'name_surname': {
115 'name': 'Robert',
116 'surname': 'Craigie',
117 },
118 },
119 )
120 assert found.id == model.id
122 found = client.unique3.find_unique_or_raise(
123 where={
124 'surname': 'Craigie',
125 },
126 )
127 assert found.id == model.id
129 found = client.unique3.find_unique_or_raise(
130 where={
131 'id': model.id,
132 },
133 )
134 assert found.id == model.id
137def test_unique4(client: Prisma) -> None:
138 """Explicitly named unique constraint"""
139 model = client.unique4.create(
140 data={
141 'name': 'Robert',
142 'surname': 'Craigie',
143 },
144 )
146 found = client.unique4.find_unique_or_raise(
147 where={
148 'my_unique': {
149 'name': 'Robert',
150 'surname': 'Craigie',
151 },
152 },
153 )
154 assert found.name == model.name
155 assert found.surname == model.surname
158def test_unique5(client: Prisma) -> None:
159 """Combined unique constraint with 3 fields"""
160 model = client.unique5.create(
161 data={
162 'name': 'Robert',
163 'middlename': 'Cosmo',
164 'surname': 'Craigie',
165 },
166 )
168 found = client.unique5.find_unique_or_raise(
169 where={
170 'name_middlename_surname': {
171 'name': 'Robert',
172 'middlename': 'Cosmo',
173 'surname': 'Craigie',
174 },
175 },
176 )
177 assert found.name == model.name
178 assert found.middlename == model.middlename
179 assert found.surname == model.surname
182def test_id1(client: Prisma) -> None:
183 """Standard combined ID constraint"""
184 model = client.id1.create(
185 data={
186 'name': 'Robert',
187 'surname': 'Craigie',
188 },
189 )
190 found = client.id1.find_unique_or_raise(
191 where={
192 'name_surname': {
193 'name': 'Robert',
194 'surname': 'Craigie',
195 },
196 },
197 )
198 assert found.name == model.name
199 assert found.surname == model.surname
202def test_id2(client: Prisma) -> None:
203 """Combined ID constraint with a unique field"""
204 model = client.id2.create(
205 data={
206 'name': 'Robert',
207 'surname': 'Craigie',
208 },
209 )
211 found = client.id2.find_unique_or_raise(
212 where={
213 'name_surname': {
214 'name': 'Robert',
215 'surname': 'Craigie',
216 },
217 },
218 )
219 assert found.name == model.name
220 assert found.surname == model.surname
222 found = client.id2.find_unique_or_raise(
223 where={
224 'surname': 'Craigie',
225 },
226 )
227 assert found.name == model.name
228 assert found.surname == model.surname
231def test_id3(client: Prisma) -> None:
232 """Explicitly named combined ID constraint"""
233 model = client.id3.create(
234 data={
235 'name': 'Robert',
236 'surname': 'Craigie',
237 },
238 )
240 found = client.id3.find_unique_or_raise(
241 where={
242 'my_id': {
243 'name': 'Robert',
244 'surname': 'Craigie',
245 },
246 },
247 )
248 assert found.name == model.name
249 assert found.surname == model.surname
252def test_id4(client: Prisma) -> None:
253 """Combined ID constraint with 3 fields"""
254 model = client.id4.create(
255 data={
256 'name': 'Robert',
257 'middlename': 'Cosmo',
258 'surname': 'Craigie',
259 },
260 )
262 found = client.id4.find_unique_or_raise(
263 where={
264 'name_middlename_surname': {
265 'name': 'Robert',
266 'middlename': 'Cosmo',
267 'surname': 'Craigie',
268 },
269 },
270 )
271 assert found.name == model.name
272 assert found.middlename == model.middlename
273 assert found.surname == model.surname