Skip to content

Commit 52fcf90

Browse files
aldbrchrisburr
authored andcommitted
fix(db): submission datetime
1 parent 4db5094 commit 52fcf90

File tree

4 files changed

+244
-4
lines changed

4 files changed

+244
-4
lines changed

diracx-db/src/diracx/db/sql/utils/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def find_time_resolution(value):
232232
if isinstance(value, datetime):
233233
return None, value
234234
if match := re.fullmatch(
235-
r"\d{4}(-\d{2}(-\d{2}(([ T])\d{2}(:\d{2}(:\d{2}(\.\d{6}Z?)?)?)?)?)?)?", value
235+
r"\d{4}(-\d{2}(-\d{2}(([ T])\d{2}(:\d{2}(:\d{2}(\.\d{1,6}Z?)?)?)?)?)?)?", value
236236
):
237237
if match.group(6):
238238
precision, pattern = "SECOND", r"\1-\2-\3 \4:\5:\6"
@@ -249,7 +249,7 @@ def find_time_resolution(value):
249249
return (
250250
precision,
251251
re.sub(
252-
r"^(\d{4})-?(\d{2})?-?(\d{2})?[ T]?(\d{2})?:?(\d{2})?:?(\d{2})?\.?(\d{6})?Z?$",
252+
r"^(\d{4})-?(\d{2})?-?(\d{2})?[ T]?(\d{2})?:?(\d{2})?:?(\d{2})?\.?(\d{1,6})?Z?$",
253253
pattern,
254254
value,
255255
),

diracx-db/src/diracx/db/sql/utils/functions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class date_trunc(expression.FunctionElement): # noqa: N801
4747
"""
4848

4949
type = DateTime()
50-
inherit_cache = True
50+
# Cache does not work as intended with time resolution values, so we disable it
51+
inherit_cache = False
5152

5253
def __init__(self, *args, time_resolution, **kwargs) -> None:
5354
super().__init__(*args, **kwargs)

diracx-routers/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ dependencies = [
3535
dynamic = ["version"]
3636

3737
[project.optional-dependencies]
38-
testing = ["diracx-testing", "moto[server]", "pytest-httpx"]
38+
testing = ["diracx-testing", "moto[server]", "pytest-httpx", "freezegun",]
3939
types = [
4040
"boto3-stubs",
4141
"types-aiobotocore[essential]",

diracx-routers/tests/test_job_manager.py

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import pytest
77
from fastapi.testclient import TestClient
8+
from freezegun import freeze_time
89

910
from diracx.core.models import JobStatus
1011

@@ -243,6 +244,244 @@ def test_insert_and_search(normal_user_client):
243244
assert r.json() == []
244245

245246

247+
@freeze_time("2024-01-01T00:00:00.123456Z")
248+
def test_insert_and_search_by_datetime(normal_user_client):
249+
"""Test inserting a job and then searching for it.
250+
251+
Focus on the SubmissionTime parameter.
252+
"""
253+
# job_definitions = [TEST_JDL%(normal_user_client.dirac_token_payload)]
254+
job_definitions = [TEST_JDL]
255+
r = normal_user_client.post("/api/jobs/jdl", json=job_definitions)
256+
listed_jobs = r.json()
257+
assert r.status_code == 200, listed_jobs
258+
assert len(listed_jobs) == len(job_definitions)
259+
260+
# 1.1 Search for all jobs submitted in 2024
261+
r = normal_user_client.post(
262+
"/api/jobs/search",
263+
json={
264+
"search": [
265+
{
266+
"parameter": "SubmissionTime",
267+
"operator": "eq",
268+
"value": "2024",
269+
}
270+
]
271+
},
272+
)
273+
assert r.status_code == 200, r.json()
274+
assert len(r.json()) == 1
275+
276+
# 1.2 Search for all jobs submitted before 2024
277+
r = normal_user_client.post(
278+
"/api/jobs/search",
279+
json={
280+
"search": [
281+
{
282+
"parameter": "SubmissionTime",
283+
"operator": "lt",
284+
"value": "2024",
285+
}
286+
]
287+
},
288+
)
289+
assert r.status_code == 200, r.json()
290+
assert len(r.json()) == 0
291+
292+
# 2.1 Search for all jobs submitted after 2024-01
293+
r = normal_user_client.post(
294+
"/api/jobs/search",
295+
json={
296+
"search": [
297+
{
298+
"parameter": "SubmissionTime",
299+
"operator": "gt",
300+
"value": "2024-01",
301+
}
302+
]
303+
},
304+
)
305+
assert r.status_code == 200, r.json()
306+
assert len(r.json()) == 0
307+
308+
# 2.2 Search for all jobs submitted before 2024-02
309+
r = normal_user_client.post(
310+
"/api/jobs/search",
311+
json={
312+
"search": [
313+
{
314+
"parameter": "SubmissionTime",
315+
"operator": "lt",
316+
"value": "2024-02",
317+
}
318+
]
319+
},
320+
)
321+
assert r.status_code == 200, r.json()
322+
assert len(r.json()) == 1
323+
324+
# 3 Search for all jobs submitted during 2024-01-01
325+
r = normal_user_client.post(
326+
"/api/jobs/search",
327+
json={
328+
"search": [
329+
{
330+
"parameter": "SubmissionTime",
331+
"operator": "eq",
332+
"value": "2024-01-01",
333+
}
334+
]
335+
},
336+
)
337+
assert r.status_code == 200, r.json()
338+
assert len(r.json()) == 1
339+
340+
# 4.1 Search for all jobs submitted during 2024-01-01 00
341+
r = normal_user_client.post(
342+
"/api/jobs/search",
343+
json={
344+
"search": [
345+
{
346+
"parameter": "SubmissionTime",
347+
"operator": "eq",
348+
"value": "2024-01-01 00",
349+
}
350+
]
351+
},
352+
)
353+
assert r.status_code == 200, r.json()
354+
assert len(r.json()) == 1
355+
356+
# 4.2 Search for all jobs submitted during 2024-01-01T00 (with the 'T' separator)
357+
r = normal_user_client.post(
358+
"/api/jobs/search",
359+
json={
360+
"search": [
361+
{
362+
"parameter": "SubmissionTime",
363+
"operator": "eq",
364+
"value": "2024-01-01T00",
365+
}
366+
]
367+
},
368+
)
369+
assert r.status_code == 200, r.json()
370+
assert len(r.json()) == 1
371+
372+
# 4.3 Search for all jobs not submitted during 2024-01-01 01
373+
r = normal_user_client.post(
374+
"/api/jobs/search",
375+
json={
376+
"search": [
377+
{
378+
"parameter": "SubmissionTime",
379+
"operator": "neq",
380+
"value": "2024-01-01 01",
381+
}
382+
]
383+
},
384+
)
385+
assert r.status_code == 200, r.json()
386+
assert len(r.json()) == 1
387+
388+
# 5.1 Search for all jobs submitted after 2024-01-01 00:00:00
389+
r = normal_user_client.post(
390+
"/api/jobs/search",
391+
json={
392+
"search": [
393+
{
394+
"parameter": "SubmissionTime",
395+
"operator": "gt",
396+
"value": "2024-01-01 00:00:00",
397+
}
398+
]
399+
},
400+
)
401+
assert r.status_code == 200, r.json()
402+
assert len(r.json()) == 0
403+
404+
# 5.2 Search for all jobs not submitted on 2024-01-01 00:00:00
405+
r = normal_user_client.post(
406+
"/api/jobs/search",
407+
json={
408+
"search": [
409+
{
410+
"parameter": "SubmissionTime",
411+
"operator": "neq",
412+
"value": "2024-01-01 00:00:00",
413+
}
414+
]
415+
},
416+
)
417+
assert r.status_code == 200, r.json()
418+
assert len(r.json()) == 0
419+
420+
# 5.3 Search for all jobs submitted on 2024-01-01 00:00:00
421+
r = normal_user_client.post(
422+
"/api/jobs/search",
423+
json={
424+
"search": [
425+
{
426+
"parameter": "SubmissionTime",
427+
"operator": "eq",
428+
"value": "2024-01-01 00:00:00",
429+
}
430+
]
431+
},
432+
)
433+
assert r.status_code == 200, r.json()
434+
assert len(r.json()) == 1
435+
436+
# 6.1 Search for all jobs submitted on 2024-01-01 00:00:00.123456
437+
r = normal_user_client.post(
438+
"/api/jobs/search",
439+
json={
440+
"search": [
441+
{
442+
"parameter": "SubmissionTime",
443+
"operator": "eq",
444+
"value": "2024-01-01 00:00:00.123456",
445+
}
446+
]
447+
},
448+
)
449+
assert r.status_code == 200, r.json()
450+
assert len(r.json()) == 1
451+
452+
# 6.2 Search for all jobs submitted on 2024-01-01 00:00:00.123456Z
453+
r = normal_user_client.post(
454+
"/api/jobs/search",
455+
json={
456+
"search": [
457+
{
458+
"parameter": "SubmissionTime",
459+
"operator": "eq",
460+
"value": "2024-01-01 00:00:00.123456Z",
461+
}
462+
]
463+
},
464+
)
465+
assert r.status_code == 200, r.json()
466+
assert len(r.json()) == 1
467+
468+
# 6.3 Search for all jobs submitted on 2024-01-01 00:00:00.123Z
469+
r = normal_user_client.post(
470+
"/api/jobs/search",
471+
json={
472+
"search": [
473+
{
474+
"parameter": "SubmissionTime",
475+
"operator": "eq",
476+
"value": "2024-01-01 00:00:00.123Z",
477+
}
478+
]
479+
},
480+
)
481+
assert r.status_code == 200, r.json()
482+
assert len(r.json()) == 1
483+
484+
246485
def test_search_distinct(normal_user_client):
247486
"""Test that the distinct parameter works as expected."""
248487
job_definitions = [TEST_JDL, TEST_JDL, TEST_JDL]

0 commit comments

Comments
 (0)