OpenSlides/server/tests/conftest.py

99 lines
3.0 KiB
Python
Raw Normal View History

2019-01-20 10:05:50 +01:00
import os
from typing import cast
2019-01-20 10:05:50 +01:00
2018-08-30 09:07:06 +02:00
import pytest
from asgiref.sync import async_to_sync
from django.test import TestCase, TransactionTestCase
2018-08-30 09:07:06 +02:00
from pytest_django.django_compat import is_django_unittest
from pytest_django.plugin import validate_django_db
2018-09-01 08:00:00 +02:00
from openslides.utils.cache import element_cache
from openslides.utils.cache_providers import MemoryCacheProvider
2018-09-01 08:00:00 +02:00
2019-01-20 10:05:50 +01:00
# Set an environment variable to stop the startup command
os.environ["NO_STARTUP"] = "1"
def pytest_collection_modifyitems(items):
"""
Helper until https://github.com/pytest-dev/pytest-django/issues/214 is fixed.
"""
2019-01-06 16:22:33 +01:00
def get_marker_transaction(test):
2019-01-06 16:22:33 +01:00
marker = test.get_closest_marker("django_db")
if marker:
validate_django_db(marker)
2019-01-06 16:22:33 +01:00
return marker.kwargs["transaction"]
return None
def has_fixture(test, fixture):
2019-07-03 07:46:23 +02:00
fixturenames = getattr(test, "fixturenames", None)
return fixturenames and fixture in fixturenames
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)
2019-01-06 16:22:33 +01:00
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
2019-01-06 16:22:33 +01:00
elif has_fixture(test, "db"):
return 0
if is_transaction_test_case_subclass or get_marker_transaction(test) is True:
return 1
2019-01-06 16:22:33 +01:00
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, reset_cache):
2018-08-30 09:07:06 +02:00
"""
Resets the constants on every test. The filled cache is needed to
build the constants, because some of them depends on the config.
2018-08-30 09:07:06 +02:00
Uses fake constants, if the db is not in use.
"""
from openslides.utils.constants import get_constants_from_apps, set_constants
2018-08-30 09:07:06 +02:00
2019-01-06 16:22:33 +01:00
if "django_db" in request.node.keywords or is_django_unittest(request):
2018-08-30 09:07:06 +02:00
# When the db is created, use the original constants
set_constants(get_constants_from_apps())
else:
# Else: Use fake constants
2019-01-06 16:22:33 +01:00
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
"""
2019-01-06 16:22:33 +01:00
if "django_db" in request.node.keywords or is_django_unittest(request):
2018-09-01 08:00:00 +02:00
# When the db is created, use the original cachables
async_to_sync(element_cache.cache_provider.clear_cache)()
2018-09-01 08:00:00 +02:00
element_cache.ensure_cache(reset=True)
# Set constant default change_id
cast(MemoryCacheProvider, element_cache.cache_provider).default_change_id = 1
2019-08-20 12:00:54 +02:00
@pytest.fixture(scope="session", autouse=True)
def set_default_user_backend(request):
"""
Sets the userbackend once.
"""
from openslides.users.user_backend import user_backend_manager
user_backend_manager.collect_backends_from_apps()