Coverage for databases/tests/types/test_bigint.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
4from prisma.errors import DataError
6from ...utils import CURRENT_DATABASE
9@pytest.mark.asyncio
10async def test_filtering(client: Prisma) -> None:
11 """Finding records by a BigInt value"""
12 async with client.batch_() as batcher:
13 for i in range(10):
14 batcher.types.create({'bigint': i + 1})
16 total = await client.types.count(where={'bigint': {'gte': 5}})
17 assert total == 6
19 found = await client.types.find_first(
20 where={
21 'bigint': {
22 'equals': 2,
23 },
24 },
25 )
26 assert found is not None
27 assert found.bigint == 2
29 results = await client.types.find_many(
30 where={
31 'bigint': {
32 'in': [1, 5, 7],
33 },
34 },
35 order={
36 'bigint': 'asc',
37 },
38 )
39 assert len(results) == 3
40 assert results[0].bigint == 1
41 assert results[1].bigint == 5
42 assert results[2].bigint == 7
44 results = await client.types.find_many(
45 where={
46 'bigint': {
47 'not_in': [1, 2, 3, 4, 6, 7, 8, 9],
48 },
49 },
50 order={
51 'bigint': 'asc',
52 },
53 )
54 assert len(results) == 2
55 assert results[0].bigint == 5
56 assert results[1].bigint == 10
58 found = await client.types.find_first(
59 where={
60 'bigint': {
61 'lt': 5,
62 },
63 },
64 order={
65 'bigint': 'desc',
66 },
67 )
68 assert found is not None
69 assert found.bigint == 4
71 found = await client.types.find_first(
72 where={
73 'bigint': {
74 'lte': 5,
75 },
76 },
77 order={
78 'bigint': 'desc',
79 },
80 )
81 assert found is not None
82 assert found.bigint == 5
84 found = await client.types.find_first(
85 where={
86 'bigint': {
87 'gt': 5,
88 },
89 },
90 order={
91 'bigint': 'asc',
92 },
93 )
94 assert found is not None
95 assert found.bigint == 6
97 found = await client.types.find_first(
98 where={
99 'bigint': {
100 'gte': 6,
101 },
102 },
103 order={
104 'bigint': 'asc',
105 },
106 )
107 assert found is not None
108 assert found.bigint == 6
110 found = await client.types.find_first(
111 where={
112 'bigint': {
113 'not': 1,
114 },
115 },
116 order={'bigint': 'asc'},
117 )
118 assert found is not None
119 assert found.bigint == 2
122@pytest.mark.asyncio
123async def test_atomic_update(client: Prisma) -> None:
124 """Atomically updating a BigInt value"""
125 model = await client.types.create({'id': 1, 'bigint': 1})
126 assert model.bigint == 1
128 updated = await client.types.update(
129 where={
130 'id': 1,
131 },
132 data={
133 'bigint': {'increment': 5},
134 },
135 )
136 assert updated is not None
137 assert updated.bigint == 6
139 updated = await client.types.update(
140 where={
141 'id': 1,
142 },
143 data={
144 'bigint': {
145 'set': 20,
146 },
147 },
148 )
149 assert updated is not None
150 assert updated.bigint == 20
152 updated = await client.types.update(
153 where={
154 'id': 1,
155 },
156 data={
157 'bigint': {
158 'decrement': 5,
159 },
160 },
161 )
162 assert updated is not None
163 assert updated.bigint == 15
165 updated = await client.types.update(
166 where={
167 'id': 1,
168 },
169 data={
170 'bigint': {
171 'multiply': 2,
172 },
173 },
174 )
175 assert updated is not None
176 assert updated.bigint == 30
179@pytest.mark.asyncio
180@pytest.mark.skipif(
181 CURRENT_DATABASE == 'cockroachdb',
182 reason='https://github.com/prisma/prisma/issues/16511',
183)
184async def test_atomic_update_divide(client: Prisma) -> None:
185 """Atomically dividing a `BigInt` value"""
186 model = await client.types.create({'id': 1, 'bigint': 30})
187 assert model.bigint == 30
189 updated = await client.types.update(
190 where={
191 'id': 1,
192 },
193 data={
194 'bigint': {
195 'divide': 3,
196 },
197 },
198 )
199 assert updated is not None
200 assert updated.bigint == 10
203@pytest.mark.asyncio
204async def test_atomic_update_invalid_input(client: Prisma) -> None:
205 """BigInt atomic update only allows one field to be passed"""
206 with pytest.raises(DataError) as exc:
207 await client.types.update(
208 where={
209 'id': 1,
210 },
211 data={
212 'bigint': { # type: ignore
213 'divide': 1,
214 'multiply': 2,
215 },
216 },
217 )
219 message = exc.value.args[0]
220 assert isinstance(message, str)
221 assert 'Expected exactly one field to be present, got 2' in message
224@pytest.mark.asyncio
225async def test_filtering_nulls(client: Prisma) -> None:
226 """None is a valid filter for nullable BigInt fields"""
227 await client.types.create(
228 {
229 'string': 'a',
230 'optional_bigint': None,
231 },
232 )
233 await client.types.create(
234 {
235 'string': 'b',
236 'optional_bigint': 12437823782382,
237 },
238 )
239 await client.types.create(
240 {
241 'string': 'c',
242 'optional_bigint': 8239829842494,
243 },
244 )
246 found = await client.types.find_first(
247 where={
248 'NOT': [
249 {
250 'optional_bigint': None,
251 },
252 ],
253 },
254 order={
255 'string': 'asc',
256 },
257 )
258 assert found is not None
259 assert found.string == 'b'
260 assert found.optional_bigint == 12437823782382
262 count = await client.types.count(
263 where={
264 'optional_bigint': None,
265 },
266 )
267 assert count == 1
269 count = await client.types.count(
270 where={
271 'NOT': [
272 {
273 'optional_bigint': None,
274 },
275 ],
276 },
277 )
278 assert count == 2