From c74a0078fbcca410a33f9c279b75ac380e1fae21 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sat, 5 Dec 2015 16:05:51 -0500 Subject: [PATCH] A better way to manage test settings --- tests/plugin_test.py | 75 ++++++++++++++++++++++-------------------- tests/test_settings.py | 6 ++-- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/tests/plugin_test.py b/tests/plugin_test.py index b98fcb1..aec7302 100644 --- a/tests/plugin_test.py +++ b/tests/plugin_test.py @@ -13,46 +13,51 @@ # Make Django templates outside of Django. # Originally taken from: http://stackoverflow.com/a/98178/14343 from django.conf import settings -test_settings = { - 'CACHES': { - 'default': { - 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + +def test_settings(): + """Create a dict full of default Django settings for the tests.""" + the_settings = { + 'CACHES': { + 'default': { + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', + }, + }, + 'DATABASES': { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': ':memory:', + } }, - }, - 'DATABASES': { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': ':memory:', - } - }, - 'ROOT_URLCONF': 'tests', -} - -if django.VERSION >= (1, 8): - test_settings.update({ - 'TEMPLATES': [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': ['templates'], # where the tests put things. - 'OPTIONS': { - 'debug': True, + 'ROOT_URLCONF': 'tests', + } + + if django.VERSION >= (1, 8): + the_settings.update({ + 'TEMPLATES': [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': ['templates'], # where the tests put things. + 'OPTIONS': { + 'debug': True, + }, }, - }, - ], - }) + ], + }) + + if django.VERSION < (1, 10): + # for {% ssi %} + the_settings['TEMPLATES'][0]['OPTIONS']['allowed_include_roots'] = ['/'] - if django.VERSION < (1, 10): - # for {% ssi %} - test_settings['TEMPLATES'][0]['OPTIONS']['allowed_include_roots'] = ['/'] + else: + the_settings.update({ + 'ALLOWED_INCLUDE_ROOTS': ['/'], # for {% ssi %} + 'TEMPLATE_DEBUG': True, + 'TEMPLATE_DIRS': ['templates'], # where the tests put things. + }) -else: - test_settings.update({ - 'ALLOWED_INCLUDE_ROOTS': ['/'], # for {% ssi %} - 'TEMPLATE_DEBUG': True, - 'TEMPLATE_DIRS': ['templates'], # where the tests put things. - }) + return the_settings -settings.configure(**test_settings) +settings.configure(**test_settings()) if hasattr(django, "setup"): django.setup() diff --git a/tests/test_settings.py b/tests/test_settings.py index ffbaa64..de606f5 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,18 +1,16 @@ """Settings tests for django_coverage_plugin.""" -import copy - import django from django.test.utils import override_settings from django_coverage_plugin import DjangoTemplatePluginException -from .plugin_test import DjangoPluginTestCase, test_settings +from .plugin_test import DjangoPluginTestCase, test_settings, django_start_at if django.VERSION >= (1, 8): DEBUG_FALSE_OVERRIDES = { - 'TEMPLATES': [copy.deepcopy(test_settings['TEMPLATES'][0])] + 'TEMPLATES': [test_settings()['TEMPLATES'][0]] } DEBUG_FALSE_OVERRIDES['TEMPLATES'][0]['OPTIONS']['debug'] = False else: