Skip to content

Commit

Permalink
Add tests for known bad requests
Browse files Browse the repository at this point in the history
Use status enum for ease of read

Do not cache handler and test client
  • Loading branch information
DiamondJoseph committed May 25, 2023
1 parent 51a4cf2 commit 1b1683f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
7 changes: 5 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ def client(self) -> TestClient:
return TestClient(app)


@pytest.fixture(scope="session")
@pytest.fixture
def handler() -> Handler:
context: BlueskyContext = Mock()
context.plans = {}
context.devices = {}
context.plan_functions = {}
context.run_engine.state = RunEngineStateMachine.States.IDLE
handler = Handler(context=context)

Expand All @@ -55,6 +58,6 @@ def no_op():
return handler


@pytest.fixture(scope="session")
@pytest.fixture
def client(handler: Handler) -> TestClient:
return Client(handler).client
41 changes: 35 additions & 6 deletions tests/service/test_rest_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass

from bluesky.run_engine import RunEngineStateMachine
from fastapi import status
from fastapi.testclient import TestClient
from pydantic import BaseModel

Expand All @@ -9,8 +10,6 @@
from blueapi.worker.task import RunPlan
from src.blueapi.worker import WorkerState

_TASK = RunPlan(name="count", params={"detectors": ["x"]})


def test_get_plans(handler: Handler, client: TestClient) -> None:
class MyModel(BaseModel):
Expand All @@ -21,7 +20,7 @@ class MyModel(BaseModel):
handler.context.plans = {"my-plan": plan}
response = client.get("/plans")

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"plans": [{"name": "my-plan"}]}


Expand All @@ -34,7 +33,7 @@ class MyModel(BaseModel):
handler.context.plans = {"my-plan": plan}
response = client.get("/plans/my-plan")

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
assert response.json() == {"name": "my-plan"}


Expand All @@ -48,7 +47,7 @@ class MyDevice:
handler.context.devices = {"my-device": device}
response = client.get("/devices")

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"devices": [
{
Expand All @@ -69,13 +68,21 @@ class MyDevice:
handler.context.devices = {"my-device": device}
response = client.get("/devices/my-device")

assert response.status_code == 200
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"name": "my-device",
"protocols": ["HasName"],
}


def test_get_non_existant_device_by_name(handler: Handler, client: TestClient) -> None:
response = client.get("/devices/my-device")

assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.json() == {'detail': 'Item not found'}



def test_create_task(handler: Handler, client: TestClient) -> None:
response = client.post("/tasks", json=_TASK.dict())
task_id = response.json()["task_id"]
Expand All @@ -95,6 +102,28 @@ def test_put_plan_begins_task(handler: Handler, client: TestClient) -> None:
assert handler.worker._task_channel.get().task_id == task_id # type: ignore


def test_put_plan_with_unknown_plan_name_fails(handler: Handler, client: TestClient) -> None:
task_name = "foo"
task_params = {"detectors": ["x"]}
task_json = {"name": task_name, "params": task_params}

response = client.post("/tasks", json=task_json)

assert not handler.worker.get_pending_tasks()
assert response.status_code == status.HTTP_400_BAD_REQUEST


def test_put_plan_with_bad_params_fails(handler: Handler, client: TestClient) -> None:
task_name = "count"
task_params = {"motors": ["x"]}
task_json = {"name": task_name, "params": task_params}

response = client.post("/tasks", json=task_json)

assert not handler.worker.get_pending_tasks()
assert response.status_code == status.HTTP_400_BAD_REQUEST


def test_get_state_updates(handler: Handler, client: TestClient) -> None:
assert client.get("/worker/state").text == f'"{WorkerState.IDLE.name}"'
handler.worker._on_state_change( # type: ignore
Expand Down

0 comments on commit 1b1683f

Please sign in to comment.