Skip to content

Commit

Permalink
Added first test and fixed some parts of patch_metadata
Browse files Browse the repository at this point in the history
Remove breakpoint

pre-commit
  • Loading branch information
ryuwd committed Feb 10, 2025
1 parent eaa355f commit 46559f7
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
5 changes: 4 additions & 1 deletion diracx-routers/src/diracx/routers/jobs/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ async def search(
if query_logging_info := ("LoggingInfo" in (body.parameters or [])):
if body.parameters:
body.parameters.remove("LoggingInfo")
body.parameters = ["JobID"] + (body.parameters or [])
if not body.parameters:
body.parameters = None
else:
body.parameters = ["JobID"] + (body.parameters or [])

# TODO: Apply all the job policy stuff properly using user_info
if not config.Operations["Defaults"].Services.JobMonitoring.GlobalJobsInfo:
Expand Down
17 changes: 9 additions & 8 deletions diracx-routers/src/diracx/routers/jobs/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async def patch_metadata(
):
await check_permissions(action=ActionType.MANAGE, job_db=job_db, job_ids=updates)
possible_attribute_columns = [
name.lower() for name in _get_columns(Jobs.__table__, None)
col.name.lower() for col in _get_columns(Jobs.__table__, None)
]

attr_updates = {}
Expand All @@ -163,17 +163,18 @@ async def patch_metadata(
if pname.lower() in possible_attribute_columns
}
# else set elastic parameters DB
param_updates[job_id] = [
(pname, pvalue)
param_updates[job_id] = {
pname: pvalue
for pname, pvalue in metadata.items()
if pname.lower() not in possible_attribute_columns
]
}
# bulk set job attributes
await job_db.set_job_attributes_bulk(attr_updates)

# TODO: can we upsert to multiple documents?
for job_id, p_updates_ in param_updates.items():
await job_parameters_db.upsert(
int(job_id),
p_updates_,
)
if p_updates_:
await job_parameters_db.upsert(
int(job_id),
p_updates_,
)
64 changes: 64 additions & 0 deletions diracx-routers/tests/test_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,67 @@ def test_set_single_job_properties_non_existing_job(
# for job_id in valid_job_ids:
# r = normal_user_client.get(f"/api/jobs/{job_id}/status")
# assert r.status_code == HTTPStatus.NOT_FOUND, r.json()


def test_patch_metadata(normal_user_client: TestClient, valid_job_id: int):
# Arrange
r = normal_user_client.post(
"/api/jobs/search",
json={
"search": [
{
"parameter": "JobID",
"operator": "eq",
"value": valid_job_id,
}
],
"parameters": ["LoggingInfo"],
},
)

assert r.status_code == 200, r.json()
for j in r.json():
assert j["JobID"] == valid_job_id
assert j["Status"] == JobStatus.RECEIVED.value
assert j["MinorStatus"] == "Job accepted"
assert j["ApplicationStatus"] == "Unknown"

# Act
hbt = str(datetime.now(timezone.utc))
r = normal_user_client.patch(
"/api/jobs/metadata",
json={
valid_job_id: {
"UserPriority": 2,
"HeartBeatTime": hbt,
# set a parameter
"JobType": "VerySpecialIndeed",
}
},
)

# Assert
assert (
r.status_code == 204
), "PATCH metadata should return 204 No Content on success"
r = normal_user_client.post(
"/api/jobs/search",
json={
"search": [
{
"parameter": "JobID",
"operator": "eq",
"value": valid_job_id,
}
],
"parameters": ["LoggingInfo"],
},
)
assert r.status_code == 200, r.json()

assert r.json()[0]["JobID"] == valid_job_id
assert r.json()[0]["JobType"] == "VerySpecialIndeed"
assert datetime.fromisoformat(
r.json()[0]["HeartBeatTime"]
) == datetime.fromisoformat(hbt)
assert r.json()[0]["UserPriority"] == 2

0 comments on commit 46559f7

Please sign in to comment.