2019-01-20 10:05:50 +01:00
|
|
|
import os
|
2019-08-14 12:19:20 +02:00
|
|
|
from typing import cast
|
2019-01-20 10:05:50 +01:00
|
|
|
|
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
|
2019-08-14 12:19:20 +02:00
|
|
|
from openslides.utils.cache_providers import MemoryCacheProvider
|
2018-09-01 08:00:00 +02:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2019-01-20 10:05:50 +01:00
|
|
|
# Set an environment variable to stop the startup command
|
|
|
|
os.environ["NO_STARTUP"] = "1"
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
def get_marker_transaction(test):
|
2019-01-06 16:22:33 +01:00
|
|
|
marker = test.get_closest_marker("django_db")
|
2018-07-09 23:22:26 +02:00
|
|
|
if marker:
|
|
|
|
validate_django_db(marker)
|
2019-01-06 16:22:33 +01:00
|
|
|
return marker.kwargs["transaction"]
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
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
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2018-07-09 23:22:26 +02:00
|
|
|
|
|
|
|
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"):
|
2018-07-09 23:22:26 +02:00
|
|
|
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"):
|
2018-07-09 23:22:26 +02:00
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
items.sort(key=weight_test_case)
|
2018-08-30 09:07:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2019-07-10 15:54:17 +02:00
|
|
|
def constants(request, reset_cache):
|
2018-08-30 09:07:06 +02:00
|
|
|
"""
|
2019-07-10 15:54:17 +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 set_constants, get_constants_from_apps
|
|
|
|
|
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
|
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
|
|
|
|
2019-07-29 15:19:59 +02:00
|
|
|
# Set constant default change_id
|
2019-08-14 12:19:20 +02:00
|
|
|
cast(MemoryCacheProvider, element_cache.cache_provider).default_change_id = 1
|