Skip to content

Commit

Permalink
Lint and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <[email protected]>
  • Loading branch information
aaronchongth committed May 27, 2024
1 parent 93467ca commit c4a76d8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 38 deletions.
1 change: 0 additions & 1 deletion packages/api-server/api_server/models/alerts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from enum import Enum
from typing import List, Optional

Expand Down
1 change: 0 additions & 1 deletion packages/api-server/api_server/routes/alerts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
from datetime import datetime
from typing import List

Expand Down
2 changes: 1 addition & 1 deletion packages/api-server/api_server/routes/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from api_server.app_config import app_config
from api_server.logging import LoggerAdapter, get_logger
from api_server.repositories import FleetRepository, TaskRepository
from api_server.rmf_io import alert_events, fleet_events, task_events
from api_server.rmf_io import fleet_events, task_events

router = APIRouter(tags=["_internal"])
user: mdl.User = mdl.User(username="__rmf_internal__", is_admin=True)
Expand Down
76 changes: 43 additions & 33 deletions packages/api-server/api_server/routes/test_alerts.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from unittest.mock import patch
from urllib.parse import urlencode
from uuid import uuid4

from api_server import models as mdl
from api_server.rmf_io import tasks_service
from api_server.test import AppFixture, make_alert_request


Expand All @@ -13,20 +11,20 @@ def setUpClass(cls):
super().setUpClass()

def test_create_new_alert(self):
id = str(uuid4())
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert_id = str(uuid4())
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
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)

# repeated creation with same ID will fail
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
resp = self.client.post("/alerts/request", data=alert.json(exclude_none=True))
self.assertEqual(409, resp.status_code, resp.content)

def respond_to_alert(self):
id = str(uuid4())
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert_id = str(uuid4())
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
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)
Expand All @@ -40,61 +38,65 @@ def respond_to_alert(self):

# response that is unavailable
params = {"response": "wrong"}
resp = self.client.post(f"/alerts/{id}/respond?{urlencode(params)}")
resp = self.client.post(f"/alerts/{alert_id}/respond?{urlencode(params)}")
self.assertEqual(422, resp.status_code, resp.content)

# respond correctly
response = "resume"
params = {"response": response}
resp = self.client.post(f"/alerts/request/{id}/respond?{urlencode(params)}")
resp = self.client.post(
f"/alerts/request/{alert_id}/respond?{urlencode(params)}"
)
self.assertEqual(201, resp.status_code, resp.content)
self.assertEqual(id, resp.json()["id"], resp.content)
self.assertEqual(response, resp.json()["response"], resp.content)

def test_get_alert(self):
id = str(uuid4())
alert_id = str(uuid4())

# alert does not exist
resp = self.client.get(f"/alerts/request/{id}")
resp = self.client.get(f"/alerts/request/{alert_id}")
self.assertEqual(404, resp.status_code, resp.content)

# create alert
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
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)

# alert exists now
resp = self.client.get(f"/alerts/request/{id}")
resp = self.client.get(f"/alerts/request/{alert_id}")
self.assertEqual(200, resp.status_code, resp.content)
self.assertEqual(alert, resp.json(), resp.content)

def test_get_alert_response(self):
id = str(uuid4())
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert_id = str(uuid4())
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
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)

# respond
response = "resume"
params = {"response": response}
resp = self.client.post(f"/alerts/request/{id}/respond?{urlencode(params)}")
resp = self.client.post(
f"/alerts/request/{alert_id}/respond?{urlencode(params)}"
)
self.assertEqual(201, resp.status_code, resp.content)
self.assertEqual(id, resp.json()["id"], resp.content)
self.assertEqual(alert_id, resp.json()["id"], resp.content)
self.assertEqual(response, resp.json()["response"], resp.content)

# response exists
resp = self.client.get(f"/alerts/request/{id}/response")
resp = self.client.get(f"/alerts/request/{alert_id}/response")
self.assertEqual(200, resp.status_code, resp.content)
self.assertEqual(id, resp.json()["id"], resp.content)
self.assertEqual(alert_id, resp.json()["id"], resp.content)
self.assertEqual(response, resp.json()["response"], resp.content)

def test_sub_alert(self):
gen = self.subscribe_sio("/alerts/requests")

id = str(uuid4())
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert_id = str(uuid4())
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
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)
Expand All @@ -106,27 +108,29 @@ def test_sub_alert(self):
def test_sub_alert_response(self):
gen = self.subscribe_sio("/alerts/responses")

id = str(uuid4())
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert_id = str(uuid4())
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
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)

# respond
response = "resume"
params = {"response": response}
resp = self.client.post(f"/alerts/request/{id}/respond?{urlencode(params)}")
resp = self.client.post(
f"/alerts/request/{alert_id}/respond?{urlencode(params)}"
)
self.assertEqual(201, resp.status_code, resp.content)
self.assertEqual(id, resp.json()["id"], resp.content)
self.assertEqual(alert_id, resp.json()["id"], resp.content)
self.assertEqual(response, resp.json()["response"], resp.content)

# check subscribed alert response
subbed_alert_response = next(gen)
self.assertEqual(subbed_alert_response, resp.json(), subbed_alert_response)

def test_get_alerts_of_task(self):
id = str(uuid4())
alert = make_alert_request(id=id, responses=["resume", "cancel"])
alert_id = str(uuid4())
alert = make_alert_request(alert_id=alert_id, responses=["resume", "cancel"])
alert.task_id = "test_task_id"
resp = self.client.post("/alerts/request", data=alert.json(exclude_none=True))
self.assertEqual(201, resp.status_code, resp.content)
Expand All @@ -146,9 +150,11 @@ def test_get_alerts_of_task(self):
# respond to alert
response = "resume"
params = {"response": response}
resp = self.client.post(f"/alerts/request/{id}/respond?{urlencode(params)}")
resp = self.client.post(
f"/alerts/request/{alert_id}/respond?{urlencode(params)}"
)
self.assertEqual(201, resp.status_code, resp.content)
self.assertEqual(id, resp.json()["id"], resp.content)
self.assertEqual(alert_id, resp.json()["id"], resp.content)
self.assertEqual(response, resp.json()["response"], resp.content)

# check for alert of correct task ID again (will only return
Expand All @@ -168,15 +174,19 @@ def test_get_alerts_of_task(self):

def test_get_unresponded_alert_ids(self):
first_id = str(uuid4())
first_alert = make_alert_request(id=first_id, responses=["resume", "cancel"])
first_alert = make_alert_request(
alert_id=first_id, responses=["resume", "cancel"]
)
resp = self.client.post(
"/alerts/request", data=first_alert.json(exclude_none=True)
)
self.assertEqual(201, resp.status_code, resp.content)
self.assertEqual(first_alert, resp.json(), resp.content)

second_id = str(uuid4())
second_alert = make_alert_request(id=second_id, responses=["resume", "cancel"])
second_alert = make_alert_request(
alert_id=second_id, responses=["resume", "cancel"]
)
resp = self.client.post(
"/alerts/request", data=second_alert.json(exclude_none=True)
)
Expand Down Expand Up @@ -215,10 +225,10 @@ def test_get_unresponded_alert_ids(self):
self.assertTrue(second_id in returned_alert_ids)

def test_task_location_complete(self):
id = str(uuid4())
alert_id = str(uuid4())
task_id = "test_task_id"
location_name = "test_location"
alert = make_alert_request(id=id, responses=["success", "fail"])
alert = make_alert_request(alert_id=alert_id, responses=["success", "fail"])
alert.alert_parameters = [
mdl.AlertParameter(name="type", value="location_result"),
mdl.AlertParameter(name="location_name", value=location_name),
Expand Down
4 changes: 2 additions & 2 deletions packages/api-server/api_server/test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,9 @@ def make_task_log(task_id: str) -> TaskEventLog:
return sample


def make_alert_request(id: str, responses: List[str]) -> AlertRequest:
def make_alert_request(alert_id: str, responses: List[str]) -> AlertRequest:
return AlertRequest(
id=id,
id=alert_id,
unix_millis_alert_time=0,
title="test_title",
subtitle="test_subtitle",
Expand Down

0 comments on commit c4a76d8

Please sign in to comment.