From bcb3538c3b3728dbfb497c44eab022e12922ea06 Mon Sep 17 00:00:00 2001 From: Dimitris Poulopoulos Date: Fri, 10 Jan 2025 11:10:32 +0200 Subject: [PATCH] tests: Add unit tests for the experiment repo (#570) Closes #570 Signed-off-by: Dimitris Poulopoulos --- .../unit/repositories/test_experiments.py | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lumigator/python/mzai/backend/backend/tests/unit/repositories/test_experiments.py diff --git a/lumigator/python/mzai/backend/backend/tests/unit/repositories/test_experiments.py b/lumigator/python/mzai/backend/backend/tests/unit/repositories/test_experiments.py new file mode 100644 index 00000000..ee11d5f0 --- /dev/null +++ b/lumigator/python/mzai/backend/backend/tests/unit/repositories/test_experiments.py @@ -0,0 +1,39 @@ +import uuid + +import pytest +from lumigator_schemas.jobs import JobStatus +from sqlalchemy.exc import IntegrityError + +from backend.repositories.experiments import ExperimentRepository +from backend.repositories.jobs import JobRepository + + +@pytest.fixture +def experiment_repository(db_session): + return ExperimentRepository(db_session) + + +@pytest.fixture +def job_repository(db_session): + return JobRepository(db_session) + + +def test_create_and_get_experiment(experiment_repository): + created_experiment = experiment_repository.create(name="test", description="") + retrieved_experiment = experiment_repository.get(created_experiment.id) + assert created_experiment.id == retrieved_experiment.id + assert created_experiment.name == retrieved_experiment.name + assert created_experiment.status == JobStatus.CREATED + + +def test_experiment_foreign_key(job_repository): + random_id = uuid.uuid4() + with pytest.raises(IntegrityError): + job_repository.create(name="test", description="", experiment_id=random_id) + + +def test_duplicate_results_error(experiment_repository, job_repository): + experiment = experiment_repository.create(name="test", description="") + job_repository.create(name="test", description="", experiment_id=experiment.id) + with pytest.raises(IntegrityError): + job_repository.create(name="test", description="", experiment_id=experiment.id)