Skip to content

Commit

Permalink
Load an actual pipeline in tests rather than mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
rossgray committed Jul 26, 2024
1 parent 4d2246a commit 499ed2d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
Empty file.
19 changes: 19 additions & 0 deletions tests/container/fixtures/adder_pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging

from pipeline import Pipeline, Variable, pipe

logger = logging.getLogger(__name__)


@pipe
def add(first: int, second: int) -> int:
return first + second


with Pipeline() as builder:
first = Variable(int)
second = Variable(int)
result = add(first, second)
builder.output(result)

my_pipeline = builder.get_pipeline()
27 changes: 1 addition & 26 deletions tests/container/routes/conftest.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

from pipeline.cloud.schemas import pipelines as pipeline_schemas
from pipeline.cloud.schemas import runs as run_schemas
from pipeline.container.startup import create_app


class DummyManager:
def __init__(self, pipeline_path):
self.pipeline_state = pipeline_schemas.PipelineState.not_loaded

def startup(self):
self.pipeline_state = pipeline_schemas.PipelineState.loaded

def run(self, run_id: str | None, input_data: list[run_schemas.RunInput] | None):
# sum the inputs
result = 0
for input in input_data or []:
result += input.value
return [result]


@pytest.fixture
async def mock_manager():
with patch("pipeline.container.startup.Manager", DummyManager) as mock:
yield mock


@pytest.fixture
async def app(mock_manager):
async def app():
app = create_app()
return app

Expand Down
18 changes: 16 additions & 2 deletions tests/container/routes/test_runs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import pytest
from fastapi import status
from fastapi.testclient import TestClient

from pipeline.cloud.schemas import runs as run_schemas


@pytest.fixture
async def client(app, monkeypatch):
# loads a pipeline which sums 2 inputs
monkeypatch.setenv(
"PIPELINE_PATH", "tests.container.fixtures.adder_pipeline:my_pipeline"
)
with TestClient(app) as client:
yield client


async def test_create_run(client):

payload = run_schemas.ContainerRunCreate(
run_id="run_123",
inputs=[
Expand All @@ -15,12 +28,12 @@ async def test_create_run(client):

assert response.status_code == status.HTTP_200_OK
result = run_schemas.ContainerRunResult.parse_obj(response.json())
# we have mocked the run manager to sum the inputs
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_123",
run_id="run_124",
inputs=[
run_schemas.RunInput(type="integer", value=5),
run_schemas.RunInput(type="integer", value=10),
Expand All @@ -30,4 +43,5 @@ async def test_create_run(client):

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)]

0 comments on commit 499ed2d

Please sign in to comment.