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