From 9468f49d3482efb8dace99196bff749d43610c78 Mon Sep 17 00:00:00 2001 From: Ross Gray Date: Fri, 26 Jul 2024 10:51:19 +0000 Subject: [PATCH] Add more tests --- tests/container/fixtures/adder_pipeline.py | 2 + tests/container/routes/test_runs.py | 127 ++++++++++++++++----- 2 files changed, 100 insertions(+), 29 deletions(-) diff --git a/tests/container/fixtures/adder_pipeline.py b/tests/container/fixtures/adder_pipeline.py index 5ee0d170..9af7c514 100644 --- a/tests/container/fixtures/adder_pipeline.py +++ b/tests/container/fixtures/adder_pipeline.py @@ -7,6 +7,8 @@ @pipe def add(first: int, second: int) -> int: + if first < 0 or second < 0: + raise ValueError("I can only sum positive integers") return first + second diff --git a/tests/container/routes/test_runs.py b/tests/container/routes/test_runs.py index a05751f4..26be1d1a 100644 --- a/tests/container/routes/test_runs.py +++ b/tests/container/routes/test_runs.py @@ -15,33 +15,102 @@ async def client(app, monkeypatch): yield client -async def test_create_run(client): - - payload = run_schemas.ContainerRunCreate( - run_id="run_123", - inputs=[ - run_schemas.RunInput(type="integer", value=5), - run_schemas.RunInput(type="integer", value=4), - ], - ) - response = client.post("/v4/runs", json=payload.dict()) - - assert response.status_code == status.HTTP_200_OK - result = run_schemas.ContainerRunResult.parse_obj(response.json()) - assert result.error is None - assert result.outputs == [run_schemas.RunOutput(type="integer", value=9)] - - # make another run to ensure execution handler is still working as expected - payload = run_schemas.ContainerRunCreate( - run_id="run_124", - inputs=[ - run_schemas.RunInput(type="integer", value=5), - run_schemas.RunInput(type="integer", value=10), - ], - ) - response = client.post("/v4/runs", json=payload.dict()) +class TestCreateRun: + + def test_success(self, client): + + payload = run_schemas.ContainerRunCreate( + run_id="run_123", + inputs=[ + run_schemas.RunInput(type="integer", value=5), + run_schemas.RunInput(type="integer", value=4), + ], + ) + response = client.post("/v4/runs", json=payload.dict()) + + assert response.status_code == status.HTTP_200_OK + result = run_schemas.ContainerRunResult.parse_obj(response.json()) + assert result.error is None + assert result.outputs == [run_schemas.RunOutput(type="integer", value=9)] + + # make another run to ensure execution handler is still working as expected + payload = run_schemas.ContainerRunCreate( + run_id="run_124", + inputs=[ + run_schemas.RunInput(type="integer", value=5), + run_schemas.RunInput(type="integer", value=10), + ], + ) + response = client.post("/v4/runs", json=payload.dict()) + + assert response.status_code == status.HTTP_200_OK + result = run_schemas.ContainerRunResult.parse_obj(response.json()) + assert result.error is None + assert result.outputs == [run_schemas.RunOutput(type="integer", value=15)] + + def test_when_pipeline_failed_to_load(self, app, monkeypatch): + # use invalid path to simulate pipeline failed error + monkeypatch.setenv("PIPELINE_PATH", "tests.container.fixtures.oops:my_pipeline") + with TestClient(app) as client: + + payload = run_schemas.ContainerRunCreate( + run_id="run_123", + inputs=[ + run_schemas.RunInput(type="integer", value=5), + run_schemas.RunInput(type="integer", value=4), + ], + ) + response = client.post("/v4/runs", json=payload.dict()) + + assert response.status_code == status.HTTP_200_OK + result = run_schemas.ContainerRunResult.parse_obj(response.json()) + assert result.outputs is None + assert result.error is not None + error = run_schemas.ContainerRunError.parse_obj(result.error) + assert error.type == run_schemas.ContainerRunErrorType.startup_error + assert error.message == "Pipeline failed to load" + assert error.traceback is not None + + def test_when_invalid_inputs(self, client): + payload = run_schemas.ContainerRunCreate( + run_id="run_123", + # one input is missing + inputs=[ + run_schemas.RunInput(type="integer", value=5), + ], + ) + response = client.post("/v4/runs", json=payload.dict()) + + assert response.status_code == status.HTTP_400_BAD_REQUEST + result = run_schemas.ContainerRunResult.parse_obj(response.json()) + assert result.outputs is None + assert result.error is not None + error = run_schemas.ContainerRunError.parse_obj(result.error) + assert error.type == run_schemas.ContainerRunErrorType.input_error + assert error.message == "Inputs do not match graph inputs" + + def test_when_pipeline_raises_an_exception(self, client): + """We've set up the fixture pipeline to only accept positive integers, + so providing negative ones should result in a RunnableError. + + (Note: in reality we could add options to our inputs to handle this and + return an input_error) + """ + payload = run_schemas.ContainerRunCreate( + run_id="run_123", + inputs=[ + run_schemas.RunInput(type="integer", value=-5), + run_schemas.RunInput(type="integer", value=5), + ], + ) + response = client.post("/v4/runs", json=payload.dict()) - assert response.status_code == status.HTTP_200_OK - result = run_schemas.ContainerRunResult.parse_obj(response.json()) - assert result.error is None - assert result.outputs == [run_schemas.RunOutput(type="integer", value=15)] + assert response.status_code == status.HTTP_200_OK + result = run_schemas.ContainerRunResult.parse_obj(response.json()) + assert result.outputs is None + assert result.error is not None + error = run_schemas.ContainerRunError.parse_obj(result.error) + assert error.type == run_schemas.ContainerRunErrorType.pipeline_error + assert error.message == "ValueError('I can only sum positive integers')" + assert error.traceback is not None + assert error.traceback.startswith("Traceback (most recent call last):")