Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing tests for some endpoints before route refactoring #52

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/hayhooks/server/handlers/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
from hayhooks.server.pipelines import registry


@app.get("/status")
async def status():
@app.get("/status", tags=["status"])
async def status_all():
pipelines = registry.get_names()
return {"status": "Up!", "pipelines": pipelines}


@app.get("/status/{pipeline_name}", tags=["status"])
async def status(pipeline_name: str):
if pipeline_name not in registry.get_names():
raise HTTPException(status_code=404, detail=f"Pipeline '{pipeline_name}' not found")
return {"status": "Up!", "pipeline": pipeline_name}
3 changes: 3 additions & 0 deletions src/hayhooks/server/pipelines/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,8 @@ def get(self, name: str) -> Optional[Pipeline]:
def get_names(self) -> list[str]:
return list(self._pipelines.keys())

def clear(self):
self._pipelines.clear()


registry = _PipelineRegistry()
50 changes: 46 additions & 4 deletions tests/test_it_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,65 @@
from fastapi.testclient import TestClient
from hayhooks.server import app
from pathlib import Path
from hayhooks.server.pipelines.registry import registry

client = TestClient(app)


@pytest.fixture(autouse=True)
def clear_registry():
registry.clear()


# Load pipeline definitions from test_files
test_files = Path(__file__).parent / "test_files" / "working_pipelines"
pipeline_data = [{"name": file.stem, "source_code": file.read_text()} for file in test_files.glob("*.yml")]


@pytest.mark.parametrize("pipeline_data", pipeline_data)
def test_deploy_pipeline_def(pipeline_data: dict):
def deploy_pipeline(pipeline_data: dict):
deploy_response = client.post(
"/deploy", json={"name": pipeline_data["name"], "source_code": pipeline_data["source_code"]}
)

return deploy_response
anakin87 marked this conversation as resolved.
Show resolved Hide resolved


def undeploy_pipeline(pipeline_data: dict):
undeploy_response = client.post(f"/undeploy/{pipeline_data['name']}")
return undeploy_response


@pytest.mark.parametrize("pipeline_data", pipeline_data)
def test_deploy_pipeline_def(pipeline_data: dict):
deploy_response = deploy_pipeline(pipeline_data)
assert deploy_response.status_code == 200

status_response = client.get("/status")
assert pipeline_data["name"] in status_response.json()["pipelines"]
status_response = client.get(f"/status/{pipeline_data['name']}")
assert pipeline_data["name"] in status_response.json()["pipeline"]

docs_response = client.get("/docs")
assert docs_response.status_code == 200


def test_undeploy_pipeline_def():
pipeline_file = Path(__file__).parent / "test_files" / "working_pipelines/test_pipeline_01.yml"
pipeline_data = {"name": pipeline_file.stem, "source_code": pipeline_file.read_text()}

deploy_response = deploy_pipeline(pipeline_data)
assert deploy_response.status_code == 200

undeploy_response = undeploy_pipeline(pipeline_data)
assert undeploy_response.status_code == 200

status_response = client.get(f"/status/{pipeline_data['name']}")
assert status_response.status_code == 404


def test_undeploy_non_existent_pipeline():
undeploy_response = client.post("/undeploy/non_existent_pipeline")
assert undeploy_response.status_code == 404


def test_undeploy_no_pipelines():
undeploy_response = client.post("/undeploy")
assert undeploy_response.status_code == 404
25 changes: 25 additions & 0 deletions tests/test_it_draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from fastapi.testclient import TestClient
from hayhooks.server import app
from tests.test_it_deploy import deploy_pipeline
from pathlib import Path

client = TestClient(app)


def test_draw_pipeline():
pipeline_file = Path(__file__).parent / "test_files" / "working_pipelines/test_pipeline_01.yml"
pipeline_data = {"name": pipeline_file.stem, "source_code": pipeline_file.read_text()}

deploy_response = deploy_pipeline(pipeline_data)
assert deploy_response.status_code == 200

draw_response = client.get(f"/draw/{pipeline_data['name']}")
assert draw_response.status_code == 200

assert draw_response.headers["Content-Type"] == "image/png"
assert len(draw_response.content) > 0


def test_draw_non_existent_pipeline():
draw_response = client.get("/draw/non_existent_pipeline")
assert draw_response.status_code == 404
42 changes: 42 additions & 0 deletions tests/test_it_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
from fastapi.testclient import TestClient
from hayhooks.server import app
from hayhooks.server.pipelines import registry
from tests.test_it_deploy import deploy_pipeline
from pathlib import Path

client = TestClient(app)

@pytest.fixture(autouse=True)
def clear_registry():
registry.clear()


def test_status_all_pipelines():
status_response = client.get("/status")
assert status_response.status_code == 200
assert "pipelines" in status_response.json()


def test_status_single_pipeline():
pipeline_file = Path(__file__).parent / "test_files" / "working_pipelines/test_pipeline_01.yml"
pipeline_data = {"name": pipeline_file.stem, "source_code": pipeline_file.read_text()}

deploy_pipeline(pipeline_data)

status_response = client.get(f"/status/{pipeline_data['name']}")
assert status_response.status_code == 200
assert status_response.json()["pipeline"] == pipeline_data["name"]


def test_status_non_existent_pipeline():
status_response = client.get("/status/non_existent_pipeline")
assert status_response.status_code == 404
assert status_response.json()["detail"] == f"Pipeline 'non_existent_pipeline' not found"


def test_status_no_pipelines():
status_response = client.get("/status")
assert status_response.status_code == 200
assert "pipelines" in status_response.json()
assert len(status_response.json()["pipelines"]) == 0
Loading