-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CELE-18 Add new way of creating data
- Loading branch information
Showing
3 changed files
with
49 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |