Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test performance #196

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
- <https://pytest-mock.readthedocs.io/en/latest/usage.html>
- <https://docs.python.org/3/library/unittest.mock.html#unittest.mock.MagicMock>
"""
from rdmo.core.utils import pypandoc # noqa: F401
return mocker.patch("pypandoc.convert_text")
2 changes: 1 addition & 1 deletion rdmo/conditions/tests/test_viewset_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion rdmo/conditions/tests/test_viewset_condition_multisite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/'
Expand Down
2 changes: 1 addition & 1 deletion rdmo/domain/tests/test_viewset_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/'
Expand Down
2 changes: 1 addition & 1 deletion rdmo/domain/tests/test_viewset_attribute_multisite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
33 changes: 18 additions & 15 deletions rdmo/options/tests/test_viewset_optionsets_multisite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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'])
Expand All @@ -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'])
Expand All @@ -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'])
Expand All @@ -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 = [{
Expand Down Expand Up @@ -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])
Expand All @@ -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])
Expand Down
Loading