From ff38d4871c55ce59857219662cc39d34614fcf84 Mon Sep 17 00:00:00 2001 From: Omar Abou Selo Date: Fri, 10 May 2024 14:13:30 +0300 Subject: [PATCH] Add delete reruns endpoint (#182) --- .../controllers/test_executions/models.py | 4 ++++ .../controllers/test_executions/reruns.py | 13 ++++++++++-- .../test_executions/test_reruns.py | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/backend/test_observer/controllers/test_executions/models.py b/backend/test_observer/controllers/test_executions/models.py index 91abbdfd..35610cca 100644 --- a/backend/test_observer/controllers/test_executions/models.py +++ b/backend/test_observer/controllers/test_executions/models.py @@ -168,3 +168,7 @@ class PendingRerun(BaseModel): "test_execution", "artefact_build", "artefact", "stage", "family", "name" ) ) + + +class DeleteReruns(BaseModel): + test_execution_ids: set[int] diff --git a/backend/test_observer/controllers/test_executions/reruns.py b/backend/test_observer/controllers/test_executions/reruns.py index 973524b4..08a78db9 100644 --- a/backend/test_observer/controllers/test_executions/reruns.py +++ b/backend/test_observer/controllers/test_executions/reruns.py @@ -1,5 +1,5 @@ from fastapi import APIRouter, Depends, HTTPException -from sqlalchemy import select +from sqlalchemy import delete, select from sqlalchemy.orm import Session, joinedload from test_observer.data_access.models import ( @@ -12,7 +12,7 @@ from test_observer.data_access.repository import get_or_create from test_observer.data_access.setup import get_db -from .models import PendingRerun, RerunRequest +from .models import DeleteReruns, PendingRerun, RerunRequest router = APIRouter() @@ -38,3 +38,12 @@ def get_rerun_requests(db: Session = Depends(get_db)): .joinedload(Stage.family) ) ) + + +@router.delete("/reruns") +def delete_rerun_requests(request: DeleteReruns, db: Session = Depends(get_db)): + return db.execute( + delete(TestExecutionRerunRequest).where( + TestExecutionRerunRequest.test_execution_id.in_(request.test_execution_ids) + ) + ) diff --git a/backend/tests/controllers/test_executions/test_reruns.py b/backend/tests/controllers/test_executions/test_reruns.py index 306f2f9e..0f985ef8 100644 --- a/backend/tests/controllers/test_executions/test_reruns.py +++ b/backend/tests/controllers/test_executions/test_reruns.py @@ -27,8 +27,17 @@ def get_helper() -> Response: return get_helper +@pytest.fixture +def delete(test_client: TestClient): + def delete_helper(data: Any) -> Response: # noqa: ANN401 + return test_client.request("DELETE", reruns_url, json=data) + + return delete_helper + + Post: TypeAlias = Callable[[Any], Response] Get: TypeAlias = Callable[[], Response] +Delete: TypeAlias = Callable[[Any], Response] def test_post_no_data_returns_422(post: Post): @@ -106,3 +115,14 @@ def test_get_after_two_different_posts( "family": te2.artefact_build.artefact.stage.family.name, }, ] + + +def test_post_delete_get( + get: Get, post: Post, delete: Delete, test_execution: TestExecution +): + test_execution.ci_link = "ci.link" + post({"test_execution_id": test_execution.id}) + response = delete({"test_execution_ids": [test_execution.id]}) + + assert response.status_code == 200 + assert get().json() == []