Skip to content

Commit

Permalink
Move artefact builds controllers out of artefacts module
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Oct 16, 2024
1 parent 776e5c8 commit 1491aa1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 159 deletions.
27 changes: 2 additions & 25 deletions backend/test_observer/controllers/artefacts/artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,25 @@
from test_observer.data_access.models import (
Artefact,
ArtefactBuild,
TestExecution,
)
from test_observer.data_access.models_enums import ArtefactStatus, FamilyName
from test_observer.data_access.repository import get_artefacts_by_family
from test_observer.data_access.setup import get_db

from . import environment_reviews
from . import builds, environment_reviews
from .logic import (
are_all_environments_approved,
is_there_a_rejected_environment,
)
from .models import (
ArtefactBuildDTO,
ArtefactDTO,
ArtefactPatch,
ArtefactVersionDTO,
)

router = APIRouter(tags=["artefacts"])
router.include_router(environment_reviews.router)
router.include_router(builds.router)


@router.get("", response_model=list[ArtefactDTO])
Expand Down Expand Up @@ -126,28 +125,6 @@ def _validate_artefact_status(
)


@router.get("/{artefact_id}/builds", response_model=list[ArtefactBuildDTO])
def get_artefact_builds(
artefact: Artefact = Depends(
ArtefactRetriever(
selectinload(Artefact.builds)
.selectinload(ArtefactBuild.test_executions)
.options(
selectinload(TestExecution.environment),
selectinload(TestExecution.rerun_request),
)
)
),
):
"""Get latest artefact builds of an artefact together with their test executions"""
for artefact_build in artefact.latest_builds:
artefact_build.test_executions.sort(
key=lambda test_execution: test_execution.environment.name
)

return artefact.latest_builds


@router.get("/{artefact_id}/versions", response_model=list[ArtefactVersionDTO])
def get_artefact_versions(
artefact: Artefact = Depends(ArtefactRetriever()), db: Session = Depends(get_db)
Expand Down
37 changes: 37 additions & 0 deletions backend/test_observer/controllers/artefacts/builds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import selectinload

from test_observer.controllers.artefacts.artefact_retriever import ArtefactRetriever
from test_observer.data_access.models import (
Artefact,
ArtefactBuild,
TestExecution,
)

from .models import (
ArtefactBuildDTO,
)

router = APIRouter(tags=["artefact-builds"])


@router.get("/{artefact_id}/builds", response_model=list[ArtefactBuildDTO])
def get_artefact_builds(
artefact: Artefact = Depends(
ArtefactRetriever(
selectinload(Artefact.builds)
.selectinload(ArtefactBuild.test_executions)
.options(
selectinload(TestExecution.environment),
selectinload(TestExecution.rerun_request),
)
)
),
):
"""Get latest artefact builds of an artefact together with their test executions"""
for artefact_build in artefact.latest_builds:
artefact_build.test_executions.sort(
key=lambda test_execution: test_execution.environment.name
)

return artefact.latest_builds
136 changes: 2 additions & 134 deletions backend/tests/controllers/artefacts/test_artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_get_artefact(test_client: TestClient, generator: DataGenerator):
}


def test_get_artefact_test_execution_counts_only_latest_build(
def test_get_artefact_environment_reviews_counts_only_latest_build(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("beta")
Expand All @@ -134,7 +134,7 @@ def test_get_artefact_test_execution_counts_only_latest_build(
assert response.json()["completed_environment_reviews_count"] == 1


def test_get_artefact_test_execution_counts(
def test_get_artefact_environment_reviews_counts(
test_client: TestClient,
generator: DataGenerator,
db_session: Session,
Expand Down Expand Up @@ -163,138 +163,6 @@ def test_get_artefact_test_execution_counts(
assert response.json()["completed_environment_reviews_count"] == 1


def test_get_artefact_builds(test_client: TestClient, generator: DataGenerator):
a = generator.gen_artefact("beta")
ab = generator.gen_artefact_build(a)
e = generator.gen_environment()
te = generator.gen_test_execution(ab, e)

response = test_client.get(f"/v1/artefacts/{a.id}/builds")

assert response.status_code == 200
assert response.json() == [
{
"id": ab.id,
"revision": ab.revision,
"architecture": ab.architecture,
"test_executions": [
{
"id": te.id,
"ci_link": te.ci_link,
"c3_link": te.c3_link,
"status": te.status.value,
"environment": {
"id": e.id,
"name": e.name,
"architecture": e.architecture,
},
"is_rerun_requested": False,
}
],
}
]


def test_get_artefact_builds_sorts_test_executions_by_environment_name(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("beta")
ab = generator.gen_artefact_build(a)
e2 = generator.gen_environment("e2")
e1 = generator.gen_environment("e1")
te2 = generator.gen_test_execution(ab, e2)
te1 = generator.gen_test_execution(ab, e1)

assert test_client.get(f"/v1/artefacts/{a.id}/builds").json() == [
{
"id": ab.id,
"revision": ab.revision,
"architecture": ab.architecture,
"test_executions": [
{
"id": te1.id,
"ci_link": te1.ci_link,
"c3_link": te1.c3_link,
"status": te1.status.value,
"environment": {
"id": e1.id,
"name": e1.name,
"architecture": e1.architecture,
},
"is_rerun_requested": False,
},
{
"id": te2.id,
"ci_link": te2.ci_link,
"c3_link": te2.c3_link,
"status": te2.status.value,
"environment": {
"id": e2.id,
"name": e2.name,
"architecture": e2.architecture,
},
"is_rerun_requested": False,
},
],
}
]


def test_get_artefact_builds_only_latest(
test_client: TestClient, generator: DataGenerator
):
artefact = generator.gen_artefact("beta")
generator.gen_artefact_build(artefact=artefact, revision=1)
artefact_build2 = generator.gen_artefact_build(artefact=artefact, revision=2)

response = test_client.get(f"/v1/artefacts/{artefact.id}/builds")

assert response.status_code == 200
assert response.json() == [
{
"id": artefact_build2.id,
"revision": artefact_build2.revision,
"architecture": artefact_build2.architecture,
"test_executions": [],
}
]


def test_get_artefact_builds_with_rerun_requested(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("beta")
ab = generator.gen_artefact_build(a)
e = generator.gen_environment()
te = generator.gen_test_execution(ab, e)
generator.gen_rerun_request(te)

response = test_client.get(f"/v1/artefacts/{a.id}/builds")

assert response.status_code == 200
assert response.json() == [
{
"id": ab.id,
"revision": ab.revision,
"architecture": ab.architecture,
"test_executions": [
{
"id": te.id,
"ci_link": te.ci_link,
"c3_link": te.c3_link,
"status": te.status.value,
"environment": {
"id": e.id,
"name": e.name,
"architecture": e.architecture,
},
"is_rerun_requested": True,
}
],
}
]


def test_artefact_signoff_approve(test_client: TestClient, generator: DataGenerator):
artefact = generator.gen_artefact("candidate")

Expand Down
135 changes: 135 additions & 0 deletions backend/tests/controllers/artefacts/test_builds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from fastapi.testclient import TestClient

from tests.data_generator import DataGenerator


def test_get_artefact_builds(test_client: TestClient, generator: DataGenerator):
a = generator.gen_artefact("beta")
ab = generator.gen_artefact_build(a)
e = generator.gen_environment()
te = generator.gen_test_execution(ab, e)

response = test_client.get(f"/v1/artefacts/{a.id}/builds")

assert response.status_code == 200
assert response.json() == [
{
"id": ab.id,
"revision": ab.revision,
"architecture": ab.architecture,
"test_executions": [
{
"id": te.id,
"ci_link": te.ci_link,
"c3_link": te.c3_link,
"status": te.status.value,
"environment": {
"id": e.id,
"name": e.name,
"architecture": e.architecture,
},
"is_rerun_requested": False,
}
],
}
]


def test_get_artefact_builds_sorts_test_executions_by_environment_name(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("beta")
ab = generator.gen_artefact_build(a)
e2 = generator.gen_environment("e2")
e1 = generator.gen_environment("e1")
te2 = generator.gen_test_execution(ab, e2)
te1 = generator.gen_test_execution(ab, e1)

assert test_client.get(f"/v1/artefacts/{a.id}/builds").json() == [
{
"id": ab.id,
"revision": ab.revision,
"architecture": ab.architecture,
"test_executions": [
{
"id": te1.id,
"ci_link": te1.ci_link,
"c3_link": te1.c3_link,
"status": te1.status.value,
"environment": {
"id": e1.id,
"name": e1.name,
"architecture": e1.architecture,
},
"is_rerun_requested": False,
},
{
"id": te2.id,
"ci_link": te2.ci_link,
"c3_link": te2.c3_link,
"status": te2.status.value,
"environment": {
"id": e2.id,
"name": e2.name,
"architecture": e2.architecture,
},
"is_rerun_requested": False,
},
],
}
]


def test_get_artefact_builds_only_latest(
test_client: TestClient, generator: DataGenerator
):
artefact = generator.gen_artefact("beta")
generator.gen_artefact_build(artefact=artefact, revision=1)
artefact_build2 = generator.gen_artefact_build(artefact=artefact, revision=2)

response = test_client.get(f"/v1/artefacts/{artefact.id}/builds")

assert response.status_code == 200
assert response.json() == [
{
"id": artefact_build2.id,
"revision": artefact_build2.revision,
"architecture": artefact_build2.architecture,
"test_executions": [],
}
]


def test_get_artefact_builds_with_rerun_requested(
test_client: TestClient, generator: DataGenerator
):
a = generator.gen_artefact("beta")
ab = generator.gen_artefact_build(a)
e = generator.gen_environment()
te = generator.gen_test_execution(ab, e)
generator.gen_rerun_request(te)

response = test_client.get(f"/v1/artefacts/{a.id}/builds")

assert response.status_code == 200
assert response.json() == [
{
"id": ab.id,
"revision": ab.revision,
"architecture": ab.architecture,
"test_executions": [
{
"id": te.id,
"ci_link": te.ci_link,
"c3_link": te.c3_link,
"status": te.status.value,
"environment": {
"id": e.id,
"name": e.name,
"architecture": e.architecture,
},
"is_rerun_requested": True,
}
],
}
]

0 comments on commit 1491aa1

Please sign in to comment.