Coverage for databases/tests/test_find_unique.py: 100%
101 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
6@pytest.mark.asyncio
7async def test_find_unique_id_field(client: Prisma) -> None:
8 """Finding a record by an ID field"""
9 post = await client.post.create(
10 {
11 'title': 'My post title!',
12 'published': False,
13 }
14 )
15 assert isinstance(post.id, str)
17 found = await client.post.find_unique(where={'id': post.id})
18 assert found is not None
19 assert found == post
22@pytest.mark.asyncio
23async def test_find_unique_missing_required_args(client: Prisma) -> None:
24 """Missing field raises an error"""
25 with pytest.raises(TypeError):
26 await client.post.find_unique() # type: ignore[call-arg]
28 # TODO: more constrained error type
29 with pytest.raises(errors.DataError):
30 await client.post.find_unique(
31 {
32 'title': 'Hi from Prisma!', # pyright: ignore
33 }
34 )
37@pytest.mark.asyncio
38async def test_find_unique_no_match(client: Prisma) -> None:
39 """Looking for non-existent record does not error"""
40 found = await client.post.find_unique(where={'id': 'sjbsjahs'})
41 assert found is None
44@pytest.mark.asyncio
45async def test_multiple_fields_are_not_allowed(client: Prisma) -> None:
46 """Multiple fields cannot be passed at once"""
47 with pytest.raises(errors.DataError):
48 await client.user.find_unique(
49 where={
50 'id': 'foo',
51 'email': 'foo', # type: ignore
52 },
53 )
56@pytest.mark.asyncio
57async def test_unique1(client: Prisma) -> None:
58 """Standard combined unique constraint"""
59 model = await client.unique1.create(
60 data={
61 'name': 'Robert',
62 'surname': 'Craigie',
63 },
64 )
65 found = await client.unique1.find_unique(
66 where={
67 'name_surname': {
68 'name': 'Robert',
69 'surname': 'Craigie',
70 },
71 },
72 )
73 assert found is not None
74 assert found.name == model.name
75 assert found.surname == model.surname
78@pytest.mark.asyncio
79async def test_unique2(client: Prisma) -> None:
80 """Combined unique constraint with an aditional unique field"""
81 model = await client.unique2.create(
82 data={
83 'name': 'Robert',
84 'surname': 'Craigie',
85 },
86 )
88 found = await client.unique2.find_unique(
89 where={
90 'name_surname': {
91 'name': 'Robert',
92 'surname': 'Craigie',
93 },
94 },
95 )
96 assert found is not None
97 assert found.name == model.name
98 assert found.surname == model.surname
100 found = await client.unique2.find_unique(
101 where={
102 'surname': 'Craigie',
103 },
104 )
105 assert found is not None
106 assert found.name == model.name
107 assert found.surname == model.surname
110@pytest.mark.asyncio
111async def test_unique3(client: Prisma) -> None:
112 """Combined unique constraint with an ID field and a unique field"""
113 model = await client.unique3.create(
114 data={
115 'name': 'Robert',
116 'surname': 'Craigie',
117 },
118 )
120 found = await client.unique3.find_unique(
121 where={
122 'name_surname': {
123 'name': 'Robert',
124 'surname': 'Craigie',
125 },
126 },
127 )
128 assert found is not None
129 assert found.id == model.id
131 found = await client.unique3.find_unique(
132 where={
133 'surname': 'Craigie',
134 },
135 )
136 assert found is not None
137 assert found.id == model.id
139 found = await client.unique3.find_unique(
140 where={
141 'id': model.id,
142 },
143 )
144 assert found is not None
145 assert found.id == model.id
148@pytest.mark.asyncio
149async def test_unique4(client: Prisma) -> None:
150 """Explicitly named unique constraint"""
151 model = await client.unique4.create(
152 data={
153 'name': 'Robert',
154 'surname': 'Craigie',
155 },
156 )
158 found = await client.unique4.find_unique(
159 where={
160 'my_unique': {
161 'name': 'Robert',
162 'surname': 'Craigie',
163 },
164 },
165 )
166 assert found is not None
167 assert found.name == model.name
168 assert found.surname == model.surname
171@pytest.mark.asyncio
172async def test_unique5(client: Prisma) -> None:
173 """Combined unique constraint with 3 fields"""
174 model = await client.unique5.create(
175 data={
176 'name': 'Robert',
177 'middlename': 'Cosmo',
178 'surname': 'Craigie',
179 },
180 )
182 found = await client.unique5.find_unique(
183 where={
184 'name_middlename_surname': {
185 'name': 'Robert',
186 'middlename': 'Cosmo',
187 'surname': 'Craigie',
188 },
189 },
190 )
191 assert found is not None
192 assert found.name == model.name
193 assert found.middlename == model.middlename
194 assert found.surname == model.surname
197@pytest.mark.asyncio
198async def test_id1(client: Prisma) -> None:
199 """Standard combined ID constraint"""
200 model = await client.id1.create(
201 data={
202 'name': 'Robert',
203 'surname': 'Craigie',
204 },
205 )
206 found = await client.id1.find_unique(
207 where={
208 'name_surname': {
209 'name': 'Robert',
210 'surname': 'Craigie',
211 },
212 },
213 )
214 assert found is not None
215 assert found.name == model.name
216 assert found.surname == model.surname
219@pytest.mark.asyncio
220async def test_id2(client: Prisma) -> None:
221 """Combined ID constraint with a unique field"""
222 model = await client.id2.create(
223 data={
224 'name': 'Robert',
225 'surname': 'Craigie',
226 },
227 )
229 found = await client.id2.find_unique(
230 where={
231 'name_surname': {
232 'name': 'Robert',
233 'surname': 'Craigie',
234 },
235 },
236 )
237 assert found is not None
238 assert found.name == model.name
239 assert found.surname == model.surname
241 found = await client.id2.find_unique(
242 where={
243 'surname': 'Craigie',
244 },
245 )
246 assert found is not None
247 assert found.name == model.name
248 assert found.surname == model.surname
251@pytest.mark.asyncio
252async def test_id3(client: Prisma) -> None:
253 """Explicitly named combined ID constraint"""
254 model = await client.id3.create(
255 data={
256 'name': 'Robert',
257 'surname': 'Craigie',
258 },
259 )
261 found = await client.id3.find_unique(
262 where={
263 'my_id': {
264 'name': 'Robert',
265 'surname': 'Craigie',
266 },
267 },
268 )
269 assert found is not None
270 assert found.name == model.name
271 assert found.surname == model.surname
274@pytest.mark.asyncio
275async def test_id4(client: Prisma) -> None:
276 """Combined ID constraint with 3 fields"""
277 model = await client.id4.create(
278 data={
279 'name': 'Robert',
280 'middlename': 'Cosmo',
281 'surname': 'Craigie',
282 },
283 )
285 found = await client.id4.find_unique(
286 where={
287 'name_middlename_surname': {
288 'name': 'Robert',
289 'middlename': 'Cosmo',
290 'surname': 'Craigie',
291 },
292 },
293 )
294 assert found is not None
295 assert found.name == model.name
296 assert found.middlename == model.middlename
297 assert found.surname == model.surname