diff --git a/tests/api/conftest.py b/tests/api/conftest.py index d84d81e4c..61ac191c0 100644 --- a/tests/api/conftest.py +++ b/tests/api/conftest.py @@ -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 @@ -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] diff --git a/tests/api/test_api_data.py b/tests/api/test_api_data_list.py similarity index 57% rename from tests/api/test_api_data.py rename to tests/api/test_api_data_list.py index e7d62f8c0..ef03ec3d9 100644 --- a/tests/api/test_api_data.py +++ b/tests/api/test_api_data_list.py @@ -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'), @@ -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 = [] @@ -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 diff --git a/tests/api/test_api_data_updates.py b/tests/api/test_api_data_updates.py new file mode 100644 index 000000000..3f7882c68 --- /dev/null +++ b/tests/api/test_api_data_updates.py @@ -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 diff --git a/tests/api/test_api_data_updates_task.py b/tests/api/test_api_data_updates_task.py new file mode 100644 index 000000000..fcf61461d --- /dev/null +++ b/tests/api/test_api_data_updates_task.py @@ -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