Skip to content

Commit

Permalink
Add GET /v1/artefacts/{artefact_id}/builds endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Jun 16, 2023
1 parent 87595d0 commit ddf3745
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
Empty file.
16 changes: 16 additions & 0 deletions backend/test_observer/controllers/artefacts/artefacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session
from test_observer.data_access.models import ArtefactBuild
from test_observer.data_access.setup import get_db

from .models import ArtefactBuildDTO


router = APIRouter()


@router.get("/{artefact_id}/builds", response_model=list[ArtefactBuildDTO])
def get_artefact_builds(artefact_id: int, db: Session = Depends(get_db)):
return (
db.query(ArtefactBuild).filter(ArtefactBuild.artefact_id == artefact_id).all()
)
29 changes: 29 additions & 0 deletions backend/test_observer/controllers/artefacts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from pydantic import BaseModel


class EnvironmentDTO(BaseModel):
id: int
name: str
architecture: str

class Config:
orm_mode = True


class TestExecutionDTO(BaseModel):
id: int
jenkins_link: str | None
c3_link: str | None
environment: EnvironmentDTO

class Config:
orm_mode = True


class ArtefactBuildDTO(BaseModel):
id: int
revision: int | None
test_executions: list[TestExecutionDTO]

class Config:
orm_mode = True
5 changes: 4 additions & 1 deletion backend/test_observer/controllers/router.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from fastapi import APIRouter, Depends
from sqlalchemy import text
from sqlalchemy.orm import Session

from test_observer.data_access.setup import get_db

from .application import version
from .promoter import promoter
from .artefacts import artefacts
from .families import families
from .promoter import promoter
from .test_execution import test_execution
from .test_executions import test_executions

Expand All @@ -15,6 +17,7 @@
router.include_router(families.router, prefix="/v1/families")
router.include_router(version.router, prefix="/v1/version")
router.include_router(test_executions.router, prefix="/v1/test-executions")
router.include_router(artefacts.router, prefix="/v1/artefacts")


@router.get("/")
Expand Down
40 changes: 40 additions & 0 deletions backend/tests/controllers/artefacts/test_artefacts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from test_observer.data_access.models import Environment, TestExecution

from tests.helpers import create_artefact, create_artefact_builds


def test_get_artefact(db_session: Session, test_client: TestClient):
artefact = create_artefact(db_session, "beta")
artefact_build = create_artefact_builds(db_session, artefact, 1)[0]
environment = Environment(
name="some-environment", architecture=artefact_build.architecture
)
test_execution = TestExecution(
artefact_build=artefact_build, environment=environment
)
db_session.add_all([environment, test_execution])
db_session.commit()

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

assert response.status_code == 200
assert response.json() == [
{
"id": artefact_build.id,
"revision": artefact_build.revision,
"test_executions": [
{
"id": test_execution.id,
"jenkins_link": test_execution.jenkins_link,
"c3_link": test_execution.c3_link,
"environment": {
"id": environment.id,
"name": environment.name,
"architecture": environment.architecture,
},
}
],
}
]
2 changes: 1 addition & 1 deletion backend/tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_artefact(db_session: Session, stage_name: str, **kwargs):

def create_artefact_builds(
db_session: Session, artefact: Artefact, num_builds: int = 5
):
) -> list[ArtefactBuild]:
"""
Create a number of ArtefactBuild instances for an Artefact and add them
to the database
Expand Down

0 comments on commit ddf3745

Please sign in to comment.