Skip to content

Commit

Permalink
split api data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emaciupe committed Jul 1, 2022
1 parent 87671a3 commit b17e2e5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 30 deletions.
14 changes: 13 additions & 1 deletion tests/api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from drf_querystringfilter.filters import RexList
from rest_framework.test import APIClient
from test_utilities.factories import AnonUserFactory
from test_utilities.factories import AnonUserFactory, factories_registry

from unicef_rest_framework.models import Service

Expand Down Expand Up @@ -68,3 +68,15 @@ def django_assert_no_duplicate_queries(pytestconfig, ignored=None):
@pytest.fixture()
def anon_user(db):
return AnonUserFactory()


@pytest.fixture()
def data(db, request):
# TIPS: database access is forbidden in pytest_generate_tests
viewset = request.getfixturevalue('viewset')
factory = factories_registry[viewset.serializer_class.Meta.model]
data = (factory(schema_name='bolivia'),
factory(schema_name='chad'),
factory(schema_name='lebanon'))
yield
[r.delete() for r in data]
29 changes: 0 additions & 29 deletions tests/api/test_api_data.py → tests/api/test_api_data_list.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from django.utils import timezone

import pytest
from test_utilities.factories import factories_registry

from etools_datamart.api.urls import router
from etools_datamart.apps.etl.models import EtlTask

FORMATS = (
('', 'application/json'),
Expand All @@ -21,18 +17,6 @@
)


@pytest.fixture()
def data(db, request):
# TIPS: database access is forbidden in pytest_generate_tests
viewset = request.getfixturevalue('viewset')
factory = factories_registry[viewset.serializer_class.Meta.model]
data = (factory(schema_name='bolivia'),
factory(schema_name='chad'),
factory(schema_name='lebanon'))
yield
[r.delete() for r in data]


def pytest_generate_tests(metafunc, *args):
if 'serializer' in metafunc.fixturenames:
params = []
Expand All @@ -54,16 +38,3 @@ def test_list(client, viewset, format, ct, data, serializer):
assert res.status_code == 200, res
assert res.content
assert res['Content-Type'] == ct


@pytest.mark.parametrize("updates", [True, False])
@pytest.mark.parametrize("format,ct", FORMATS, ids=[f[0] for f in FORMATS])
def test_updates(client, viewset, format, ct, data, serializer, updates):
if updates:
task = EtlTask.objects.get_for_model(viewset.queryset.model)
task.update(last_changes=timezone.now())

res = client.get(f"{viewset.get_service().endpoint}updates/?format={format}")
assert res.status_code == 200, res
assert res.content
assert res['Content-Type'] == ct
10 changes: 10 additions & 0 deletions tests/api/test_api_data_updates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest
from api.test_api_data_list import FORMATS, pytest_generate_tests


@pytest.mark.parametrize("format,ct", FORMATS, ids=[f[0] for f in FORMATS])
def test_updates(client, viewset, format, ct, data, serializer):
res = client.get(f"{viewset.get_service().endpoint}updates/?format={format}")
assert res.status_code == 200, res
assert res.content
assert res['Content-Type'] == ct
17 changes: 17 additions & 0 deletions tests/api/test_api_data_updates_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.utils import timezone

import pytest
from api.test_api_data_list import FORMATS, pytest_generate_tests

from etools_datamart.apps.etl.models import EtlTask


@pytest.mark.parametrize("format,ct", FORMATS, ids=[f[0] for f in FORMATS])
def test_updates(client, viewset, format, ct, data, serializer):
task = EtlTask.objects.get_for_model(viewset.queryset.model)
task.update(last_changes=timezone.now())

res = client.get(f"{viewset.get_service().endpoint}updates/?format={format}")
assert res.status_code == 200, res
assert res.content
assert res['Content-Type'] == ct

0 comments on commit b17e2e5

Please sign in to comment.