diff --git a/applications/visualizer/backend/tests/test_connections.py b/applications/visualizer/backend/tests/test_connections.py new file mode 100644 index 00000000..3b2ac1ac --- /dev/null +++ b/applications/visualizer/backend/tests/test_connections.py @@ -0,0 +1,29 @@ +from ninja.testing import TestAsyncClient +import pytest +from model_bakery import baker + + +from api.api import api as celegans_api +from api.models import Connection +from .utils import flat_dict, generate_instance + + +# Some test data +connections = [ + {"id": "1", "dataset": {"id": "ds3", "name": "Dr. Seuss"}}, +] + + +@pytest.fixture(scope="module") +def django_db_setup(django_db_setup, django_db_blocker): + with django_db_blocker.unblock(): + generate_instance(Connection, connections) + + +@pytest.mark.django_db +def test__fk_creation(): + o = Connection.objects.get(id=1) + + assert o.dataset + assert o.dataset.id == "ds3" + assert o.dataset.name == "Dr. Seuss" diff --git a/applications/visualizer/backend/tests/test_datasets.py b/applications/visualizer/backend/tests/test_datasets.py index 90780d49..65adb162 100644 --- a/applications/visualizer/backend/tests/test_datasets.py +++ b/applications/visualizer/backend/tests/test_datasets.py @@ -1,10 +1,10 @@ from ninja.testing import TestAsyncClient import pytest -from model_bakery import baker from api.api import api as celegans_api from api.models import Dataset +from .utils import generate_instance # Some test data @@ -24,14 +24,6 @@ ] -# Setup the db for this module with some data -# @pytest.fixture(scope="module") -# def django_db_setup(django_db_setup, django_db_blocker): -# with django_db_blocker.unblock(): -# for dataset in datasets: -# Dataset.objects.create(**dataset) - - # Setup the db for this module with some data # Data are baked with "baker", it allows to create dummy values automatically # and also to specify some fields. It is used here to "fill" the fields which are @@ -39,8 +31,7 @@ @pytest.fixture(scope="module") def django_db_setup(django_db_setup, django_db_blocker): with django_db_blocker.unblock(): - for dataset in datasets: - baker.make(Dataset, **dataset) + generate_instance(Dataset, datasets) # Fixture to access the test client in all test functions diff --git a/applications/visualizer/backend/tests/utils.py b/applications/visualizer/backend/tests/utils.py new file mode 100644 index 00000000..6363e9ce --- /dev/null +++ b/applications/visualizer/backend/tests/utils.py @@ -0,0 +1,18 @@ +from model_bakery import baker + + +def flat_dict(d): + res = {} + for key, value in d.items(): + if isinstance(value, dict): + subdict = flat_dict(value) + for subk, subv in subdict.items(): + res[f"{key}__{subk}"] = subv + else: + res[key] = value + return res + + +def generate_instance(cls, instance_list): + for instance in instance_list: + baker.make(cls, **flat_dict(instance))