Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed data fix #32

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ jobs:
with:
poetry-version: "1.5.1"
- run: poetry install
- run: poetry run black --check test_observer tests migrations
- run: poetry run ruff test_observer tests migrations
- run: poetry run mypy test_observer tests migrations
- run: poetry run black --check test_observer tests migrations scripts
- run: poetry run ruff test_observer tests migrations scripts
- run: poetry run mypy --explicit-package-bases test_observer tests migrations scripts
- run: poetry run pytest
env:
TEST_DB_URL: postgresql+pg8000://postgres:password@localhost:5432/postgres
1 change: 1 addition & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var/
*.egg
.mypy_cache
.pytest_cache
.ruff_cache

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down
11 changes: 6 additions & 5 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_execution.models import StartTestExecutionRequest
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):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really dislike adding TestClient type here. But didn't find a way around it other than using Any which is worse

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,
filter_kwargs={
"name": request.name,
"version": request.version,
"source": request.source,
},
creation_kwargs={"stage_id": stage.id},
)

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

artefact_build = get_or_create(
db,
ArtefactBuild,
architecture=request.arch,
revision=request.revision,
artefact_id=artefact.id,
filter_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,
filter_kwargs={
"environment_id": environment.id,
"artefact_build_id": artefact_build.id,
},
creation_kwargs={
"status": TestExecutionStatus.IN_PROGRESS,
},
)
return {"id": test_execution.id}

Expand Down
20 changes: 16 additions & 4 deletions backend/test_observer/data_access/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,34 @@ 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],
filter_kwargs: dict,
creation_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
:filter_kwargs: arguments to pass to the model when querying and creating
:creation_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)
creation_kwargs = creation_kwargs or {}
stmt = (
insert(model)
.values([{**filter_kwargs, **creation_kwargs}])
.on_conflict_do_nothing()
.returning(model)
)

result = db.execute(stmt).scalar_one_or_none()
db.commit()

if result is None:
# If the object already existed, we need to query it
result = db.query(model).filter_by(**kwargs).one()
result = db.query(model).filter_by(**filter_kwargs).one()

return result
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)