Coverage for databases/sync_tests/types/test_datetime.py: 98%
76 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 datetime
3from dirty_equals import IsPartialDict
5from prisma import Prisma
6from lib.testing import assert_similar_time
7from prisma.models import Types
8from prisma._compat import PYDANTIC_V2, model_json_schema
11def test_filtering(client: Prisma) -> None:
12 """Finding records by a DateTime value"""
13 now = datetime.datetime.now(datetime.timezone.utc)
14 with client.batch_() as batcher:
15 for i in range(10):
16 batcher.types.create({'datetime_': now + datetime.timedelta(hours=i)})
18 total = client.types.count(where={'datetime_': {'gte': now + datetime.timedelta(hours=5)}})
19 assert total == 5
21 found = client.types.find_first(
22 where={
23 'datetime_': {
24 'equals': now,
25 },
26 },
27 )
28 assert found is not None
29 assert_similar_time(found.datetime_, now)
31 results = client.types.find_many(
32 where={
33 'datetime_': {
34 'in': [
35 now + datetime.timedelta(hours=1),
36 now + datetime.timedelta(hours=4),
37 now + datetime.timedelta(hours=10),
38 ],
39 },
40 },
41 order={
42 'datetime_': 'asc',
43 },
44 )
45 assert len(results) == 2
46 assert_similar_time(results[0].datetime_, now + datetime.timedelta(hours=1))
47 assert_similar_time(results[1].datetime_, now + datetime.timedelta(hours=4))
49 found = client.types.find_first(
50 where={
51 'datetime_': {
52 'not_in': [
53 now,
54 now + datetime.timedelta(hours=1),
55 now + datetime.timedelta(hours=2),
56 ],
57 },
58 },
59 order={
60 'datetime_': 'asc',
61 },
62 )
63 assert found is not None
64 assert_similar_time(found.datetime_, now + datetime.timedelta(hours=3))
66 found = client.types.find_first(
67 where={
68 'datetime_': {
69 'lt': now + datetime.timedelta(hours=1),
70 },
71 },
72 order={
73 'datetime_': 'desc',
74 },
75 )
76 assert found is not None
77 assert_similar_time(found.datetime_, now)
79 found = client.types.find_first(
80 where={
81 'datetime_': {
82 'lte': now + datetime.timedelta(hours=1),
83 },
84 },
85 order={
86 'datetime_': 'desc',
87 },
88 )
89 assert found is not None
90 assert_similar_time(found.datetime_, now + datetime.timedelta(hours=1))
92 found = client.types.find_first(
93 where={
94 'datetime_': {
95 'gt': now,
96 },
97 },
98 order={
99 'datetime_': 'asc',
100 },
101 )
102 assert found is not None
103 assert_similar_time(found.datetime_, now + datetime.timedelta(hours=1))
105 found = client.types.find_first(
106 where={
107 'datetime_': {
108 'gte': now,
109 },
110 },
111 order={
112 'datetime_': 'asc',
113 },
114 )
115 assert found is not None
116 assert_similar_time(found.datetime_, now)
118 found = client.types.find_first(
119 where={
120 'datetime_': {
121 'not': now,
122 },
123 },
124 order={
125 'datetime_': 'asc',
126 },
127 )
128 assert found is not None
129 assert_similar_time(found.datetime_, now + datetime.timedelta(hours=1))
131 found = client.types.find_first(
132 where={
133 'datetime_': {
134 'not': {
135 'equals': now,
136 },
137 },
138 },
139 order={
140 'datetime_': 'asc',
141 },
142 )
143 assert found is not None
144 assert_similar_time(now + datetime.timedelta(hours=1), found.datetime_)
147def test_finds(client: Prisma) -> None:
148 """Adding 1 second timedelta finds the record"""
149 record = client.types.create(data={})
150 found = client.types.find_first(
151 where={
152 'datetime_': {
153 'lt': record.datetime_ + datetime.timedelta(seconds=1),
154 },
155 },
156 )
157 assert found is not None
158 assert found.id == record.id
161def test_tz_aware(client: Prisma) -> None:
162 """Modifying timezone still finds the record"""
163 record = client.types.create(data={})
164 found = client.types.find_first(
165 where={'datetime_': {'lt': (record.datetime_ + datetime.timedelta(hours=1)).astimezone(datetime.timezone.max)}}
166 )
167 assert found is not None
168 assert found.id == record.id
171def test_filtering_nulls(client: Prisma) -> None:
172 """None is a valid filter for nullable DateTime fields"""
173 now = datetime.datetime.now(datetime.timezone.utc)
174 client.types.create(
175 {
176 'string': 'a',
177 'optional_datetime': None,
178 },
179 )
180 client.types.create(
181 {
182 'string': 'b',
183 'optional_datetime': now,
184 },
185 )
186 client.types.create(
187 {
188 'string': 'c',
189 'optional_datetime': now + datetime.timedelta(days=1),
190 },
191 )
193 found = client.types.find_first(
194 where={
195 'NOT': [
196 {
197 'optional_datetime': None,
198 },
199 ],
200 },
201 order={
202 'string': 'asc',
203 },
204 )
205 assert found is not None
206 assert found.string == 'b'
207 assert found.optional_datetime is not None
208 assert_similar_time(now, found.optional_datetime)
210 count = client.types.count(
211 where={
212 'optional_datetime': None,
213 },
214 )
215 assert count == 1
217 count = client.types.count(
218 where={
219 'NOT': [
220 {
221 'optional_datetime': None,
222 },
223 ],
224 },
225 )
226 assert count == 2
229def test_precision_loss(client: Prisma) -> None:
230 """https://github.com/RobertCraigie/prisma-client-py/issues/129"""
231 date = datetime.datetime.now(datetime.timezone.utc)
232 post = client.post.create(
233 data={
234 'title': 'My first post',
235 'published': False,
236 'created_at': date,
237 },
238 )
239 found = client.post.find_first(
240 where={
241 'created_at': date,
242 },
243 )
244 assert found is not None
246 found = client.post.find_first(
247 where={
248 'created_at': post.created_at,
249 },
250 )
251 assert found is not None
254def test_json_schema() -> None:
255 """Ensure a JSON Schema definition can be created"""
256 if PYDANTIC_V2: 256 ↛ 277line 256 didn't jump to line 277, because the condition on line 256 was never false
257 assert model_json_schema(Types) == IsPartialDict(
258 properties=IsPartialDict(
259 {
260 'datetime_': {
261 'title': 'Datetime ',
262 'type': 'string',
263 'format': 'date-time',
264 },
265 'optional_datetime': {
266 'title': 'Optional Datetime',
267 'anyOf': [
268 {'format': 'date-time', 'type': 'string'},
269 {'type': 'null'},
270 ],
271 'default': None,
272 },
273 }
274 )
275 )
276 else:
277 assert model_json_schema(Types) == IsPartialDict(
278 properties=IsPartialDict(
279 {
280 'datetime_': {
281 'title': 'Datetime ',
282 'type': 'string',
283 'format': 'date-time',
284 },
285 'optional_datetime': {
286 'title': 'Optional Datetime',
287 'type': 'string',
288 'format': 'date-time',
289 },
290 }
291 )
292 )