Skip to content

Commit

Permalink
test: improve test performance
Browse files Browse the repository at this point in the history
  • Loading branch information
afuetterer committed Jul 24, 2024
1 parent 6b0cef6 commit 22039f6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
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
45 changes: 23 additions & 22 deletions rdmo/questions/tests/test_viewset_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from ..models import Page

pytestmark = pytest.mark.django_db

users = (
('editor', 'editor'),
('reviewer', 'reviewer'),
Expand Down Expand Up @@ -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'])
Expand All @@ -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'])
Expand All @@ -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):
client.login(username=username, password=password)

url = reverse(urlnames['export']) + export_format + '/'
Expand All @@ -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'
Expand All @@ -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])
Expand All @@ -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])
Expand All @@ -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'])
Expand All @@ -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()
Expand Down Expand Up @@ -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 = [{
Expand Down Expand Up @@ -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()]
Expand Down Expand Up @@ -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 = [{
Expand Down Expand Up @@ -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])
Expand All @@ -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()

Expand All @@ -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'
Expand Down

0 comments on commit 22039f6

Please sign in to comment.