Skip to content

Commit

Permalink
CELE-18 Add new way of creating data
Browse files Browse the repository at this point in the history
  • Loading branch information
aranega committed Apr 24, 2024
1 parent 6b950a4 commit 5bf7b16
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
29 changes: 29 additions & 0 deletions applications/visualizer/backend/tests/test_connections.py
Original file line number Diff line number Diff line change
@@ -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"
13 changes: 2 additions & 11 deletions applications/visualizer/backend/tests/test_datasets.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,23 +24,14 @@
]


# 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
# marked as "non-null" in the model which we don't want to manually fill.
@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
Expand Down
18 changes: 18 additions & 0 deletions applications/visualizer/backend/tests/utils.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 5bf7b16

Please sign in to comment.