Skip to content

Commit

Permalink
Fix bug and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-selo committed Jun 22, 2023
1 parent d147942 commit 2fadd83
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
9 changes: 5 additions & 4 deletions backend/scripts/seed_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import requests
from fastapi.testclient import TestClient

from test_observer.controllers.test_executions.models import StartTestExecutionRequest

BASE_URL = "http://localhost:30000/v1"
START_TEST_EXECUTION_URL = f"{BASE_URL}/test-execution/start"
START_TEST_EXECUTION_URL = f"{BASE_URL}/test-executions/start-test"

REQUESTS = [
StartTestExecutionRequest(
Expand Down Expand Up @@ -97,10 +98,10 @@
]


def seed_data():
def seed_data(client: TestClient | requests.Session):
for request in REQUESTS:
requests.put(START_TEST_EXECUTION_URL, json=request.dict())
client.put(START_TEST_EXECUTION_URL, json=request.dict()).raise_for_status()


if __name__ == "__main__":
seed_data()
seed_data(requests.Session())
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,40 @@ def start_test_execution(
artefact = get_or_create(
db,
Artefact,
name=request.name,
version=request.version,
source=request.source,
stage_id=stage.id,
kwargs={
"name": request.name,
"version": request.version,
"source": request.source,
},
extra_create_kwargs={"stage_id": stage.id},
)

environment = get_or_create(
db,
Environment,
name=request.environment,
architecture=request.arch,
kwargs={"name": request.environment, "architecture": request.arch},
)

artefact_build = get_or_create(
db,
ArtefactBuild,
architecture=request.arch,
revision=request.revision,
artefact_id=artefact.id,
kwargs={
"architecture": request.arch,
"revision": request.revision,
"artefact_id": artefact.id,
},
)

test_execution = get_or_create(
db,
TestExecution,
environment_id=environment.id,
artefact_build_id=artefact_build.id,
status=TestExecutionStatus.IN_PROGRESS,
kwargs={
"environment_id": environment.id,
"artefact_build_id": artefact_build.id,
},
extra_create_kwargs={
"status": TestExecutionStatus.IN_PROGRESS,
},
)
return {"id": test_execution.id}

Expand Down
18 changes: 15 additions & 3 deletions backend/test_observer/data_access/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,28 @@ def get_artefacts_by_family_name(
return artefacts


def get_or_create(db: Session, model: type[DataModel], **kwargs) -> DataModel:
def get_or_create(
db: Session,
model: type[DataModel],
kwargs: dict,
extra_create_kwargs: dict | None = None,
) -> DataModel:
"""
Creates an object if it doesn't exist, otherwise returns the existing one
:db: DB session
:model: model to create e.g. Stage, Family, Artefact
:kwargs: keyword arguments to pass to the model
:kwargs: arguments to pass to the model when querying and creating
:extra_create_kwargs: extra arguments to pass to the model when creating only
"""
# Try to create first to avoid race conditions
stmt = insert(model).values([kwargs]).on_conflict_do_nothing().returning(model)
extra_create_kwargs = extra_create_kwargs or {}
stmt = (
insert(model)
.values([{**kwargs, **extra_create_kwargs}])
.on_conflict_do_nothing()
.returning(model)
)

result = db.execute(stmt).scalar_one_or_none()
db.commit()
Expand Down
7 changes: 7 additions & 0 deletions backend/tests/scripts/test_seed_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from fastapi.testclient import TestClient

from scripts.seed_data import seed_data


def test_seed_data_no_failure(test_client: TestClient):
seed_data(test_client)

0 comments on commit 2fadd83

Please sign in to comment.