Skip to content

Commit

Permalink
Test for location complete route
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth committed May 24, 2024
1 parent 3e4a4ef commit 93467ca
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/api-server/api_server/routes/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,30 +404,40 @@ async def location_complete(
alerts = await get_alerts_of_task(task_id=task_id, unresponded=True)
if len(alerts) == 0:
raise HTTPException(
404, f"There are no locations awaiting completion for task {task_id}"
404, f"There are no location alerts awaiting response for task {task_id}"
)

# TODO: not hardcode the expected responses from the fleet adapter
# TODO: not hardcode the alert parameter expected from the fleet adapter
TimeoutResponse = "timeout"
MoveOffResponse = "move_off"
ParameterName = "waiting_at"
# TODO: not hardcode all these expected values
SuccessResponse = "success"
FailResponse = "fail"
TypeParameterName = "type"
TypeParameterValue = "location_result"
LocationParameterName = "location_name"

for alert in alerts:
if (
len(alert.alert_parameters) == 0
or TimeoutResponse not in alert.responses_available
or MoveOffResponse not in alert.responses_available
len(alert.alert_parameters) < 2
or SuccessResponse not in alert.responses_available
or FailResponse not in alert.responses_available
):
continue

# Check type
alert_type = None
for param in alert.alert_parameters:
if param.name == TypeParameterName:
alert_type = param.value
break
if alert_type != TypeParameterValue:
continue

# TODO: make sure that there are no duplicated locations that have
# not been responded to yet
for param in alert.alert_parameters:
if param.name == ParameterName and param.value == location:
if param.name == LocationParameterName and param.value == location:
response = await respond_to_alert(
alert_id=alert.id,
response=TimeoutResponse if not success else MoveOffResponse,
response=SuccessResponse if success else FailResponse,
)
logger.info(response)
return
Expand Down
33 changes: 33 additions & 0 deletions packages/api-server/api_server/routes/test_alerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,36 @@ def test_get_unresponded_alert_ids(self):
returned_alert_ids = [a["id"] for a in returned_alerts]
self.assertTrue(first_id not in returned_alert_ids)
self.assertTrue(second_id in returned_alert_ids)

def test_task_location_complete(self):
id = str(uuid4())
task_id = "test_task_id"
location_name = "test_location"
alert = make_alert_request(id=id, responses=["success", "fail"])
alert.alert_parameters = [
mdl.AlertParameter(name="type", value="location_result"),
mdl.AlertParameter(name="location_name", value=location_name),
]
alert.task_id = task_id
resp = self.client.post("/alerts/request", data=alert.json(exclude_none=True))
self.assertEqual(201, resp.status_code, resp.content)
self.assertEqual(alert, resp.json(), resp.content)

# complete wrong task ID
params = {
"task_id": "wrong_task_id",
"location": location_name,
"success": True,
}
resp = self.client.post(f"/tasks/location_complete?{urlencode(params)}")
self.assertEqual(404, resp.status_code, resp.content)

# complete missing location
params = {"task_id": task_id, "location": "wrong_location", "success": True}
resp = self.client.post(f"/tasks/location_complete?{urlencode(params)}")
self.assertEqual(404, resp.status_code, resp.content)

# complete location
params = {"task_id": task_id, "location": location_name, "success": True}
resp = self.client.post(f"/tasks/location_complete?{urlencode(params)}")
self.assertEqual(200, resp.status_code, resp.content)

0 comments on commit 93467ca

Please sign in to comment.