diff --git a/conftest.py b/conftest.py index 5dbd3652e6..b1dc53ddfa 100644 --- a/conftest.py +++ b/conftest.py @@ -50,7 +50,24 @@ def files(settings, tmp_path): return settings.MEDIA_ROOT -@pytest.fixture +@pytest.fixture(scope='session') def json_data(): json_file = Path(settings.BASE_DIR) / 'import' / 'catalogs.json' return {'elements': json.loads(json_file.read_text())} + + +@pytest.fixture +def mocked_convert_text(mocker): + """Mock the pypandoc.convert_text function. + + `mocked_convert_text` can be used in tests of the export views. + Use it to assert pypandoc would have been called with: + mocked_convert_text.assert_called(), mocked_convert_text.assert_called_once() or + mocked_convert_text.assert_called_once_with(). + + See: + - + - + """ + from rdmo.core.utils import pypandoc # noqa: F401 + return mocker.patch("pypandoc.convert_text") diff --git a/rdmo/conditions/tests/test_viewset_condition.py b/rdmo/conditions/tests/test_viewset_condition.py index efae4659ca..8ec9626c7c 100644 --- a/rdmo/conditions/tests/test_viewset_condition.py +++ b/rdmo/conditions/tests/test_viewset_condition.py @@ -285,7 +285,7 @@ def test_delete(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_detail_export(db, client, username, password, export_format): +def test_detail_export(db, client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) instance = Condition.objects.first() diff --git a/rdmo/conditions/tests/test_viewset_condition_multisite.py b/rdmo/conditions/tests/test_viewset_condition_multisite.py index 6d1f0da587..0041b678f9 100644 --- a/rdmo/conditions/tests/test_viewset_condition_multisite.py +++ b/rdmo/conditions/tests/test_viewset_condition_multisite.py @@ -32,7 +32,7 @@ def test_index(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_export(db, client, username, password, export_format): +def test_export(db, client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse(urlnames['export']) + export_format + '/' diff --git a/rdmo/domain/tests/test_viewset_attribute.py b/rdmo/domain/tests/test_viewset_attribute.py index 00c7c71310..69b33b5b33 100644 --- a/rdmo/domain/tests/test_viewset_attribute.py +++ b/rdmo/domain/tests/test_viewset_attribute.py @@ -54,7 +54,7 @@ def test_list(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_export(db, client, username, password, export_format): +def test_export(db, client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse(urlnames['export']) + export_format + '/' diff --git a/rdmo/domain/tests/test_viewset_attribute_multisite.py b/rdmo/domain/tests/test_viewset_attribute_multisite.py index 39bd7a663e..ee27a1c140 100644 --- a/rdmo/domain/tests/test_viewset_attribute_multisite.py +++ b/rdmo/domain/tests/test_viewset_attribute_multisite.py @@ -110,7 +110,7 @@ def test_delete_multisite(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_detail_export(db, client, username, password): +def test_detail_export(db, client, username, password, mocked_convert_text): client.login(username=username, password=password) instances = Attribute.objects.all() diff --git a/rdmo/options/tests/test_viewset_optionsets_multisite.py b/rdmo/options/tests/test_viewset_optionsets_multisite.py index 55bd2a30eb..21e3d3953a 100644 --- a/rdmo/options/tests/test_viewset_optionsets_multisite.py +++ b/rdmo/options/tests/test_viewset_optionsets_multisite.py @@ -11,9 +11,18 @@ from ..models import OptionSet from .test_viewset_optionsets import urlnames +pytestmark = pytest.mark.django_db + + +@pytest.fixture(scope="module") +def instances(django_db_blocker): + """Returns a queryset of all `OptionSet` objects in the test database, queries only once.""" + with django_db_blocker.unblock(): + return OptionSet.objects.all() + @pytest.mark.parametrize('username,password', users) -def test_list(db, client, username, password): +def test_list(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['list']) @@ -22,9 +31,8 @@ def test_list(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_detail(db, client, username, password): +def test_detail(client, username, password, instances): client.login(username=username, password=password) - instances = OptionSet.objects.all() for instance in instances: url = reverse(urlnames['detail'], args=[instance.pk]) @@ -33,9 +41,8 @@ def test_detail(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_nested(db, client, username, password): +def test_nested(client, username, password, instances): client.login(username=username, password=password) - instances = OptionSet.objects.all() for instance in instances: url = reverse(urlnames['nested'], args=[instance.pk]) @@ -44,7 +51,7 @@ def test_nested(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_index(db, client, username, password): +def test_index(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['index']) @@ -53,7 +60,7 @@ def test_index(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_export(db, client, username, password): +def test_export(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['export']) @@ -68,9 +75,8 @@ def test_export(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_create(db, client, username, password): +def test_create(client, username, password, instances): client.login(username=username, password=password) - instances = OptionSet.objects.all() for instance in instances: url = reverse(urlnames['list']) @@ -86,9 +92,8 @@ def test_create(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_update_m2m_multisite(db, client, username, password): +def test_update_m2m_multisite(client, username, password, instances): client.login(username=username, password=password) - instances = OptionSet.objects.all() for instance in instances: optionset_options = [{ @@ -119,9 +124,8 @@ def test_update_m2m_multisite(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_delete_multisite(db, client, username, password): +def test_delete_multisite(client, username, password, instances): client.login(username=username, password=password) - instances = OptionSet.objects.all() for instance in instances: url = reverse(urlnames['detail'], args=[instance.pk]) @@ -130,9 +134,8 @@ def test_delete_multisite(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_detail_export(db, client, username, password): +def test_detail_export(client, username, password, instances): client.login(username=username, password=password) - instances = OptionSet.objects.all() for instance in instances: url = reverse(urlnames['detail_export'], args=[instance.pk]) diff --git a/rdmo/projects/tests/test_view_project.py b/rdmo/projects/tests/test_view_project.py index b43660cf14..03effcc8d2 100644 --- a/rdmo/projects/tests/test_view_project.py +++ b/rdmo/projects/tests/test_view_project.py @@ -11,6 +11,8 @@ from ..forms import CatalogChoiceField from ..models import Project +pytestmark = pytest.mark.django_db + users = ( ('owner', 'owner'), ('manager', 'manager'), @@ -63,7 +65,7 @@ @pytest.mark.parametrize('username,password', users) -def test_list(db, client, username, password): +def test_list(client, username, password): client.login(username=username, password=password) url = reverse('projects') @@ -90,7 +92,7 @@ def test_list(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_detail(db, client, username, password, project_id): +def test_detail(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project', args=[project_id]) @@ -106,7 +108,7 @@ def test_detail(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) -def test_project_create_get(db, client, username, password): +def test_project_create_get(client, username, password): client.login(username=username, password=password) url = reverse('project_create') @@ -122,7 +124,7 @@ def test_project_create_get(db, client, username, password): assert response.status_code == 302 -def test_project_create_restricted_get(db, client, settings): +def test_project_create_restricted_get(client, settings): settings.PROJECT_CREATE_RESTRICTED = True settings.PROJECT_CREATE_GROUPS = ['projects'] @@ -138,7 +140,7 @@ def test_project_create_restricted_get(db, client, settings): assert response.status_code == 200 -def test_project_create_forbidden_get(db, client, settings): +def test_project_create_forbidden_get(client, settings): settings.PROJECT_CREATE_RESTRICTED = True client.login(username='user', password='user') @@ -150,7 +152,7 @@ def test_project_create_forbidden_get(db, client, settings): @pytest.mark.parametrize('username,password', users) -def test_project_create_get_for_extra_users_and_unavailable_catalogs(db, client, username, password): +def test_project_create_get_for_extra_users_and_unavailable_catalogs(client, username, password): client.login(username=username, password=password) Catalog.objects.create( @@ -199,7 +201,7 @@ def test_project_create_get_for_extra_users_and_unavailable_catalogs(db, client, @pytest.mark.parametrize('username,password', users) -def test_project_create_post(db, client, username, password): +def test_project_create_post(client, username, password): client.login(username=username, password=password) project_count = Project.objects.count() @@ -219,7 +221,7 @@ def test_project_create_post(db, client, username, password): assert Project.objects.count() == project_count -def test_project_create_post_restricted(db, client, settings): +def test_project_create_post_restricted(client, settings): settings.PROJECT_CREATE_RESTRICTED = True settings.PROJECT_CREATE_GROUPS = ['projects'] @@ -240,7 +242,7 @@ def test_project_create_post_restricted(db, client, settings): assert response.status_code == 302 -def test_project_create_post_forbidden(db, client, settings): +def test_project_create_post_forbidden(client, settings): settings.PROJECT_CREATE_RESTRICTED = True client.login(username='user', password='user') @@ -257,7 +259,7 @@ def test_project_create_post_forbidden(db, client, settings): @pytest.mark.parametrize('username,password', users) -def test_project_create_parent_post(db, client, username, password): +def test_project_create_parent_post(client, username, password): client.login(username=username, password=password) project_count = Project.objects.count() @@ -283,7 +285,7 @@ def test_project_create_parent_post(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_get(db, client, username, password, project_id): +def test_project_update_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_update', args=[project_id]) @@ -300,7 +302,7 @@ def test_project_update_get(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_post(db, client, username, password, project_id): +def test_project_update_post(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -326,7 +328,7 @@ def test_project_update_post(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_post_parent(db, client, username, password, project_id): +def test_project_update_post_parent(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -357,7 +359,7 @@ def test_project_update_post_parent(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_information_get(db, client, username, password, project_id): +def test_project_update_information_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_update_information', args=[project_id]) @@ -374,7 +376,7 @@ def test_project_update_information_get(db, client, username, password, project_ @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_information_post(db, client, username, password, project_id): +def test_project_update_information_post(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -399,7 +401,7 @@ def test_project_update_information_post(db, client, username, password, project @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_catalog_get(db, client, username, password, project_id): +def test_project_update_catalog_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_update_catalog', args=[project_id]) @@ -416,7 +418,7 @@ def test_project_update_catalog_get(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_catalog_post(db, client, username, password, project_id): +def test_project_update_catalog_post(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -440,7 +442,7 @@ def test_project_update_catalog_post(db, client, username, password, project_id) @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_tasks_get(db, client, username, password, project_id): +def test_project_update_tasks_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_update_tasks', args=[project_id]) @@ -457,7 +459,7 @@ def test_project_update_tasks_get(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_tasks_post(db, client, username, password, project_id): +def test_project_update_tasks_post(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -481,7 +483,7 @@ def test_project_update_tasks_post(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_views_get(db, client, username, password, project_id): +def test_project_update_views_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_update_views', args=[project_id]) @@ -498,7 +500,7 @@ def test_project_update_views_get(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_views_post(db, client, username, password, project_id): +def test_project_update_views_post(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -522,7 +524,7 @@ def test_project_update_views_post(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_parent_get(db, client, username, password, project_id): +def test_project_update_parent_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_update_parent', args=[project_id]) @@ -539,7 +541,7 @@ def test_project_update_parent_get(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_update_parent_post(db, client, username, password, project_id): +def test_project_update_parent_post(client, username, password, project_id): client.login(username=username, password=password) project = Project.objects.get(pk=project_id) @@ -567,7 +569,7 @@ def test_project_update_parent_post(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_delete_get(db, client, username, password, project_id): +def test_project_delete_get(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_delete', args=[project_id]) @@ -584,7 +586,7 @@ def test_project_delete_get(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_delete_post(db, client, username, password, project_id): +def test_project_delete_post(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_delete', args=[project_id]) @@ -604,7 +606,7 @@ def test_project_delete_post(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_export_xml(db, client, files, username, password, project_id): +def test_project_export_xml(client, files, username, password, project_id): client.login(username=username, password=password) url = reverse('project_export', args=[project_id, 'xml']) @@ -621,7 +623,7 @@ def test_project_export_xml(db, client, files, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_export_csv(db, client, username, password, project_id): +def test_project_export_csv(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_export', args=[project_id, 'csvcomma']) @@ -638,7 +640,7 @@ def test_project_export_csv(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_export_csvsemicolon(db, client, username, password, project_id): +def test_project_export_csvsemicolon(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_export', args=[project_id, 'csvsemicolon']) @@ -655,7 +657,7 @@ def test_project_export_csvsemicolon(db, client, username, password, project_id) @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_export_json(db, client, username, password, project_id): +def test_project_export_json(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_export', args=[project_id, 'json']) @@ -672,7 +674,7 @@ def test_project_export_json(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_answers(db, client, username, password, project_id): +def test_project_answers(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_answers', args=[project_id]) @@ -690,7 +692,7 @@ def test_project_answers(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) @pytest.mark.parametrize('export_format', export_formats) -def test_project_answers_export(db, client, username, password, project_id, export_format): +def test_project_answers_export(client, username, password, project_id, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse('project_answers_export', args=[project_id, export_format]) @@ -707,7 +709,7 @@ def test_project_answers_export(db, client, username, password, project_id, expo @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_view(db, client, username, password, project_id): +def test_project_view(client, username, password, project_id): client.login(username=username, password=password) project_views = Project.objects.get(pk=project_id).views.all() @@ -730,17 +732,22 @@ def test_project_view(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) @pytest.mark.parametrize('export_format', export_formats) -def test_project_view_export(db, client, username, password, project_id, export_format, files): +def test_project_view_export(client, username, password, project_id, export_format, files, mocked_convert_text): client.login(username=username, password=password) - project_views = Project.objects.get(pk=project_id).views.all() + project_views = View.objects.filter(projects__id=project_id).values_list("id", flat=True) - for view in View.objects.all(): + for view in View.objects.only("id"): url = reverse('project_view_export', args=[project_id, view.pk, export_format]) response = client.get(url) if project_id in view_project_permission_map.get(username, []): - if view in project_views: + if view.id in project_views: assert response.status_code == 200 + if export_format != "html": + mocked_convert_text.assert_called() + assert mocked_convert_text.call_args.kwargs["format"] == "html" + # e.g. assert "/tmp/tmpxabjgfje.rtf" ends with "rtf" + assert mocked_convert_text.call_args.kwargs["outputfile"].endswith(export_format) else: assert response.status_code == 404 else: @@ -752,7 +759,7 @@ def test_project_view_export(db, client, username, password, project_id, export_ @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_questions(db, client, username, password, project_id): +def test_project_questions(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_questions', args=[project_id]) @@ -768,7 +775,7 @@ def test_project_questions(db, client, username, password, project_id): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('project_id', projects) -def test_project_error(db, client, username, password, project_id): +def test_project_error(client, username, password, project_id): client.login(username=username, password=password) url = reverse('project_questions', args=[project_id]) diff --git a/rdmo/questions/tests/test_viewset_catalog.py b/rdmo/questions/tests/test_viewset_catalog.py index e1a4e2cbb7..ee1c3bd147 100644 --- a/rdmo/questions/tests/test_viewset_catalog.py +++ b/rdmo/questions/tests/test_viewset_catalog.py @@ -68,7 +68,7 @@ def test_index(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_export(db, client, username, password, export_format): +def test_export(db, client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse(urlnames['export']) + export_format + '/' diff --git a/rdmo/questions/tests/test_viewset_catalog_multisite.py b/rdmo/questions/tests/test_viewset_catalog_multisite.py index 5373671495..5cfa488b80 100644 --- a/rdmo/questions/tests/test_viewset_catalog_multisite.py +++ b/rdmo/questions/tests/test_viewset_catalog_multisite.py @@ -12,11 +12,26 @@ from ..models import Catalog from .test_viewset_catalog import export_formats, urlnames +pytestmark = pytest.mark.django_db + urlnames['catalog-toggle-site'] = 'v1-questions:catalog-toggle-site' +@pytest.fixture(scope="module") +def instance(django_db_blocker): + with django_db_blocker.unblock(): + return Catalog.objects.first() + + +@pytest.fixture(scope="module") +def instances(django_db_blocker): + """Returns a queryset of all `Catalog` objects in the test database, queries only once.""" + with django_db_blocker.unblock(): + return Catalog.objects.all() + + @pytest.mark.parametrize('username,password', users) -def test_list(db, client, username, password): +def test_list(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['list']) @@ -25,7 +40,7 @@ def test_list(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_index(db, client, username, password): +def test_index(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['index']) @@ -35,7 +50,7 @@ def test_index(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_export(db, client, username, password, export_format): +def test_export(client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse(urlnames['export']) + export_format + '/' @@ -50,9 +65,8 @@ def test_export(db, client, username, password, export_format): @pytest.mark.parametrize('username,password', users) -def test_detail(db, client, username, password): +def test_detail(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: url = reverse(urlnames['detail'], args=[instance.pk]) @@ -61,9 +75,8 @@ def test_detail(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_nested(db, client, username, password): +def test_nested(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: url = reverse(urlnames['nested'], args=[instance.pk]) @@ -72,9 +85,8 @@ def test_nested(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_create(db, client, username, password): +def test_create(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: url = reverse(urlnames['list']) @@ -91,9 +103,8 @@ def test_create(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_create_m2m(db, client, username, password): +def test_create_m2m(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: catalog_sections = [{ @@ -123,9 +134,8 @@ def test_create_m2m(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_update(db, client, username, password): +def test_update(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: catalog_sections = [{ @@ -153,9 +163,8 @@ def test_update(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_update_m2m(db, client, username, password): +def test_update_m2m(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: catalog_sections = [{ @@ -185,9 +194,8 @@ def test_update_m2m(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_delete(db, client, username, password): +def test_delete(client, username, password, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() for instance in instances: url = reverse(urlnames['detail'], args=[instance.pk]) @@ -197,9 +205,8 @@ def test_delete(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_detail_export(db, client, username, password, export_format): +def test_detail_export(client, username, password, export_format, instance): client.login(username=username, password=password) - instance = Catalog.objects.first() url = reverse(urlnames['detail_export'], args=[instance.pk]) + export_format + '/' response = client.get(url) @@ -215,9 +222,9 @@ def test_detail_export(db, client, username, password, export_format): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('add_or_remove,has_current_site_check', [('add', True), ('remove', False)]) @pytest.mark.parametrize('locked', [True, False]) -def test_update_catalog_toggle_site(db, client, username, password, add_or_remove, has_current_site_check, locked): +def test_update_catalog_toggle_site(client, username, password, add_or_remove, + has_current_site_check, locked, instances): client.login(username=username, password=password) - instances = Catalog.objects.all() current_site = Site.objects.get_current() for instance in instances: diff --git a/rdmo/questions/tests/test_viewset_page.py b/rdmo/questions/tests/test_viewset_page.py index e0147bb0b5..38a3cfe425 100644 --- a/rdmo/questions/tests/test_viewset_page.py +++ b/rdmo/questions/tests/test_viewset_page.py @@ -7,6 +7,8 @@ from ..models import Page +pytestmark = pytest.mark.django_db + users = ( ('editor', 'editor'), ('reviewer', 'reviewer'), @@ -49,8 +51,15 @@ export_formats = ('xml', 'rtf', 'odt', 'docx', 'html', 'markdown', 'tex', 'pdf') +@pytest.fixture(scope="module") +def instances(django_db_blocker): + """Returns a queryset of all `Page` objects in the test database, queries only once.""" + with django_db_blocker.unblock(): + return Page.objects.all() + + @pytest.mark.parametrize('username,password', users) -def test_list(db, client, username, password): +def test_list(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['list']) @@ -59,7 +68,7 @@ def test_list(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_index(db, client, username, password): +def test_index(client, username, password): client.login(username=username, password=password) url = reverse(urlnames['index']) @@ -69,7 +78,7 @@ def test_index(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_export(db, client, username, password, export_format): +def test_export(client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse(urlnames['export']) + export_format + '/' @@ -83,7 +92,7 @@ def test_export(db, client, username, password, export_format): assert child.tag in ['page', 'questionset', 'question'] -def test_export_search(db, client): +def test_export_search(client): client.login(username='editor', password='editor') url = reverse(urlnames['export']) + 'xml/?search=bar' @@ -92,9 +101,8 @@ def test_export_search(db, client): @pytest.mark.parametrize('username,password', users) -def test_detail(db, client, username, password): +def test_detail(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: url = reverse(urlnames['detail'], args=[instance.pk]) @@ -103,9 +111,8 @@ def test_detail(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_nested(db, client, username, password): +def test_nested(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: url = reverse(urlnames['nested'], args=[instance.pk]) @@ -114,9 +121,8 @@ def test_nested(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_create(db, client, username, password): +def test_create(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: url = reverse(urlnames['list']) @@ -138,9 +144,8 @@ def test_create(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_create_section(db, client, username, password): +def test_create_section(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: section = instance.sections.first() @@ -174,9 +179,8 @@ def test_create_section(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_create_m2m(db, client, username, password): +def test_create_m2m(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: page_questionsets = [{ @@ -223,9 +227,8 @@ def test_create_m2m(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_update(db, client, username, password): +def test_update(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: questionsets = [questionset.id for questionset in instance.questionsets.all()] @@ -256,9 +259,8 @@ def test_update(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_update_m2m(db, client, username, password): +def test_update_m2m(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: page_questionsets = [{ @@ -305,9 +307,8 @@ def test_update_m2m(db, client, username, password): @pytest.mark.parametrize('username,password', users) -def test_delete(db, client, username, password): +def test_delete(client, username, password, instances): client.login(username=username, password=password) - instances = Page.objects.all() for instance in instances: url = reverse(urlnames['detail'], args=[instance.pk]) @@ -317,7 +318,7 @@ def test_delete(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_detail_export(db, client, username, password, export_format): +def test_detail_export(client, username, password, export_format): client.login(username=username, password=password) instance = Page.objects.first() @@ -332,7 +333,7 @@ def test_detail_export(db, client, username, password, export_format): assert child.tag in ['page', 'questionset', 'question'] -def test_detail_export_full(db, client): +def test_detail_export_full(client): client.login(username='editor', password='editor') url = reverse(urlnames['detail_export'], args=[71]) + 'xml/?full=true' diff --git a/rdmo/questions/tests/test_viewset_page_multisite.py b/rdmo/questions/tests/test_viewset_page_multisite.py index 6bd56690a8..d804b01a0b 100644 --- a/rdmo/questions/tests/test_viewset_page_multisite.py +++ b/rdmo/questions/tests/test_viewset_page_multisite.py @@ -33,7 +33,7 @@ def test_index(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_export(db, client, username, password, export_format): +def test_export(db, client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) url = reverse(urlnames['export']) + export_format + '/' @@ -273,10 +273,9 @@ def test_delete(db, client, username, password): @pytest.mark.parametrize('username,password', users) @pytest.mark.parametrize('export_format', export_formats) -def test_detail_export(db, client, username, password, export_format): +def test_detail_export(db, client, username, password, export_format, mocked_convert_text): client.login(username=username, password=password) instance = Page.objects.first() - url = reverse(urlnames['detail_export'], args=[instance.pk]) + export_format + '/' response = client.get(url) assert response.status_code == status_map['detail'][username], response.content