-
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.
- Loading branch information
Showing
10 changed files
with
165 additions
and
2 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
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 was deleted.
Oops, something went wrong.
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,3 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = visualizer.settings | ||
django_debug_mode = keep |
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 |
---|---|---|
|
@@ -3,4 +3,6 @@ django_extensions | |
ipdb | ||
pytest | ||
pytest-django | ||
pytest-asyncio | ||
model-bakery | ||
black |
Empty file.
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,25 @@ | ||
import pytest | ||
|
||
from api.models import Connection | ||
from .utils import 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from ninja.testing import TestAsyncClient | ||
import pytest | ||
|
||
|
||
from api.api import api as celegans_api | ||
from api.models import Dataset | ||
from .utils import generate_instance | ||
|
||
|
||
# Some test data | ||
datasets = [ | ||
{ | ||
"id": "ds1", | ||
"name": "Dataset 1", | ||
}, | ||
{ | ||
"id": "gg", | ||
"name": "Gamma Goblin", | ||
}, | ||
{ | ||
"id": "ds3", | ||
"name": "Dr. Seuss", | ||
}, | ||
] | ||
|
||
|
||
# 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(): | ||
generate_instance(Dataset, datasets) | ||
|
||
|
||
# Fixture to access the test client in all test functions | ||
@pytest.fixture | ||
def api_client(): | ||
client = TestAsyncClient(celegans_api.default_router) | ||
return client | ||
|
||
|
||
# Test function | ||
@pytest.mark.django_db # required to access the DB | ||
@pytest.mark.asyncio # required as we access a async function | ||
async def test__get_nonexisting_dataset(api_client): | ||
response = await api_client.get("/datasets/nonexisting") | ||
assert response.status_code == 404 | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.asyncio | ||
async def test__get_existing_dataset(api_client): | ||
response = await api_client.get("/datasets/ds3") | ||
assert response.status_code == 200 | ||
|
||
dataset = response.json() | ||
assert dataset["id"] == "ds3" | ||
assert dataset["name"] == "Dr. Seuss" | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.asyncio | ||
async def test__get_all_datasets(api_client): | ||
response = await api_client.get("/datasets") | ||
assert response.status_code == 200 | ||
|
||
datasets = response.json() | ||
assert len(datasets) == 3 | ||
|
||
datasets_id = [dataset["id"] for dataset in datasets] | ||
assert "ds1" in datasets_id | ||
assert "ds3" in datasets_id | ||
assert "gg" in datasets_id |
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)) |
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