Skip to content

Commit b26b1b5

Browse files
committed
fix(db): submission datetime
1 parent 73b7406 commit b26b1b5

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

+2-2
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

+2-1
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

+1-1
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

+239
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

@@ -255,6 +256,244 @@ def test_insert_malformed_jdl(normal_user_client):
255256
assert r.status_code == 400, r.json()
256257

257258

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

0 commit comments

Comments
 (0)