Skip to content

Commit

Permalink
Add filters to rerun test executions of an artefact
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed May 7, 2024
1 parent bfbc7b3 commit 5b66de7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
23 changes: 19 additions & 4 deletions backend/test_observer/controllers/artefacts/artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
are_all_test_executions_approved,
is_there_a_rejected_test_execution,
)
from .models import ArtefactBuildDTO, ArtefactDTO, ArtefactPatch
from .models import (
ArtefactBuildDTO,
ArtefactDTO,
ArtefactPatch,
RerunArtefactTestExecutionsRequest,
)

router = APIRouter(tags=["artefacts"])

Expand Down Expand Up @@ -145,12 +150,22 @@ def get_artefact_builds(artefact_id: int, db: Session = Depends(get_db)):

@router.post("/{artefact_id}/reruns")
def rerun_artefact_test_executions(
request: RerunArtefactTestExecutionsRequest | None = None,
artefact: Artefact = Depends(_get_artefact_from_db),
db: Session = Depends(get_db),
):
latest_builds = db.scalars(
queries.latest_artefact_builds.where(ArtefactBuild.artefact_id == artefact.id)
)
for ab in latest_builds:
for te in ab.test_executions:
get_or_create(db, TestExecutionRerunRequest, {"test_execution_id": te.id})
test_executions = (te for ab in latest_builds for te in ab.test_executions)

if request:
if status := request.test_execution_status:
test_executions = (te for te in test_executions if te.status == status)
if (decision := request.test_execution_review_decision) is not None:
test_executions = (
te for te in test_executions if te.review_decision == decision
)

for te in test_executions:
get_or_create(db, TestExecutionRerunRequest, {"test_execution_id": te.id})
5 changes: 5 additions & 0 deletions backend/test_observer/controllers/artefacts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,8 @@ class ArtefactBuildDTO(BaseModel):

class ArtefactPatch(BaseModel):
status: ArtefactStatus


class RerunArtefactTestExecutionsRequest(BaseModel):
test_execution_status: TestExecutionStatus | None = None
test_execution_review_decision: list[TestExecutionReviewDecision] | None = None
43 changes: 43 additions & 0 deletions backend/tests/controllers/artefacts/test_artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from test_observer.data_access.models_enums import (
ArtefactStatus,
TestExecutionReviewDecision,
TestExecutionStatus,
)
from tests.data_generator import DataGenerator

Expand Down Expand Up @@ -370,3 +371,45 @@ def test_rerun_skips_test_executions_of_old_builds(
assert response.status_code == 200
assert te1.rerun_request is None
assert te2.rerun_request


def test_rerun_failed_artefact_test_executions(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("candidate")
ab = generator.gen_artefact_build(a)
e1 = generator.gen_environment(name="laptop")
e2 = generator.gen_environment(name="server")
te1 = generator.gen_test_execution(ab, e1)
te2 = generator.gen_test_execution(ab, e2, status=TestExecutionStatus.FAILED)

response = test_client.post(
f"/v1/artefacts/{a.id}/reruns",
json={"test_execution_status": TestExecutionStatus.FAILED},
)

assert response.status_code == 200
assert te1.rerun_request is None
assert te2.rerun_request


def test_rerun_undecided_artefact_test_executions(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("candidate")
ab = generator.gen_artefact_build(a)
e1 = generator.gen_environment(name="laptop")
e2 = generator.gen_environment(name="server")
te1 = generator.gen_test_execution(
ab, e1, review_decision=[TestExecutionReviewDecision.APPROVED_ALL_TESTS_PASS]
)
te2 = generator.gen_test_execution(ab, e2, review_decision=[])

response = test_client.post(
f"/v1/artefacts/{a.id}/reruns",
json={"test_execution_review_decision": []},
)

assert response.status_code == 200
assert te1.rerun_request is None
assert te2.rerun_request

0 comments on commit 5b66de7

Please sign in to comment.