2018-08-30 09:07:06 +02:00
|
|
|
import pytest
|
2018-11-01 17:30:18 +01:00
|
|
|
from asgiref.sync import async_to_sync
|
2018-07-09 23:22:26 +02:00
|
|
|
from django.test import TestCase, TransactionTestCase
|
2018-08-30 09:07:06 +02:00
|
|
|
from pytest_django.django_compat import is_django_unittest
|
2018-07-09 23:22:26 +02:00
|
|
|
from pytest_django.plugin import validate_django_db
|
|
|
|
|
2018-09-01 08:00:00 +02:00
|
|
|
from openslides.utils.cache import element_cache
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
"""
|
|
|
|
Helper until https://github.com/pytest-dev/pytest-django/issues/214 is fixed.
|
|
|
|
"""
|
|
|
|
def get_marker_transaction(test):
|
2018-09-07 10:39:16 +02:00
|
|
|
marker = test.get_closest_marker('django_db')
|
2018-07-09 23:22:26 +02:00
|
|
|
if marker:
|
|
|
|
validate_django_db(marker)
|
|
|
|
return marker.kwargs['transaction']
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
def has_fixture(test, fixture):
|
|
|
|
funcargnames = getattr(test, 'funcargnames', None)
|
|
|
|
return funcargnames and fixture in funcargnames
|
|
|
|
|
|
|
|
def weight_test_case(test):
|
|
|
|
"""
|
|
|
|
Key function for ordering test cases like the Django test runner.
|
|
|
|
"""
|
|
|
|
is_test_case_subclass = test.cls and issubclass(test.cls, TestCase)
|
|
|
|
is_transaction_test_case_subclass = test.cls and issubclass(test.cls, TransactionTestCase)
|
|
|
|
|
|
|
|
if is_test_case_subclass or get_marker_transaction(test) is False:
|
|
|
|
return 0
|
|
|
|
elif has_fixture(test, 'db'):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if is_transaction_test_case_subclass or get_marker_transaction(test) is True:
|
|
|
|
return 1
|
|
|
|
elif has_fixture(test, 'transactional_db'):
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
items.sort(key=weight_test_case)
|
2018-08-30 09:07:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def constants(request):
|
|
|
|
"""
|
|
|
|
Resets the constants on every test.
|
|
|
|
|
|
|
|
Uses fake constants, if the db is not in use.
|
|
|
|
"""
|
|
|
|
from openslides.utils.constants import set_constants, get_constants_from_apps
|
|
|
|
|
|
|
|
if 'django_db' in request.node.keywords or is_django_unittest(request):
|
|
|
|
# When the db is created, use the original constants
|
|
|
|
set_constants(get_constants_from_apps())
|
|
|
|
else:
|
|
|
|
# Else: Use fake constants
|
|
|
|
set_constants({'constant1': 'value1', 'constant2': 'value2'})
|
2018-09-01 08:00:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def reset_cache(request):
|
|
|
|
"""
|
|
|
|
Resetts the cache for every test
|
|
|
|
"""
|
|
|
|
if 'django_db' in request.node.keywords or is_django_unittest(request):
|
|
|
|
# When the db is created, use the original cachables
|
2018-11-01 17:30:18 +01:00
|
|
|
async_to_sync(element_cache.cache_provider.clear_cache)()
|
2018-09-01 08:00:00 +02:00
|
|
|
element_cache.ensure_cache(reset=True)
|
2018-11-03 15:59:24 +01:00
|
|
|
|
|
|
|
# Set constant start_time
|
|
|
|
element_cache.start_time = 1
|