2016-12-17 09:30:20 +01:00
|
|
|
from contextlib import ContextDecorator
|
2017-08-24 12:26:55 +02:00
|
|
|
from typing import Any
|
2016-12-17 09:30:20 +01:00
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from django.core.cache import caches
|
2013-03-12 21:38:22 +01:00
|
|
|
from django.test import TestCase as _TestCase
|
2015-01-22 18:29:12 +01:00
|
|
|
from django.test.runner import DiscoverRunner
|
2013-03-12 21:38:22 +01:00
|
|
|
|
2017-08-22 14:17:20 +02:00
|
|
|
from ..core.config import config
|
|
|
|
|
2013-03-12 21:38:22 +01:00
|
|
|
|
2015-01-22 18:29:12 +01:00
|
|
|
class OpenSlidesDiscoverRunner(DiscoverRunner):
|
2017-08-24 12:26:55 +02:00
|
|
|
def run_tests(self, test_labels, extra_tests=None, **kwargs): # type: ignore
|
2015-01-22 18:29:12 +01:00
|
|
|
"""
|
|
|
|
Test Runner which does not create a database, if only unittest are run.
|
|
|
|
"""
|
|
|
|
if len(test_labels) == 1 and test_labels[0].startswith('tests.unit'):
|
|
|
|
# Do not create a test database, if only unittests are tested
|
|
|
|
create_database = False
|
|
|
|
else:
|
|
|
|
create_database = True
|
|
|
|
|
|
|
|
self.setup_test_environment()
|
|
|
|
suite = self.build_suite(test_labels, extra_tests)
|
|
|
|
if create_database:
|
|
|
|
old_config = self.setup_databases()
|
|
|
|
result = self.run_suite(suite)
|
|
|
|
if create_database:
|
|
|
|
self.teardown_databases(old_config)
|
|
|
|
self.teardown_test_environment()
|
|
|
|
return self.suite_result(suite, result)
|
|
|
|
|
|
|
|
|
2013-03-12 21:38:22 +01:00
|
|
|
class TestCase(_TestCase):
|
|
|
|
"""
|
2017-08-22 14:17:20 +02:00
|
|
|
Resets the config object after each test.
|
2016-06-02 12:47:01 +02:00
|
|
|
"""
|
2017-08-22 14:17:20 +02:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def tearDown(self) -> None:
|
2017-08-22 14:17:20 +02:00
|
|
|
config.key_to_id = {}
|
2016-12-17 09:30:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
class use_cache(ContextDecorator):
|
|
|
|
"""
|
|
|
|
Contextmanager that changes the code to use the local memory cache.
|
|
|
|
|
|
|
|
Can also be used as decorator for a function.
|
|
|
|
|
|
|
|
The code inside the contextmananger starts with an empty cache.
|
|
|
|
"""
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def __enter__(self) -> None:
|
2016-12-17 09:30:20 +01:00
|
|
|
cache = caches['locmem']
|
|
|
|
cache.clear()
|
|
|
|
self.patch = patch('openslides.utils.collection.cache', cache)
|
|
|
|
self.patch.start()
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def __exit__(self, *exc: Any) -> None:
|
2016-12-17 09:30:20 +01:00
|
|
|
self.patch.stop()
|