Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

disable ES_LIVE_INDEX during tests

  • Loading branch information...
commit ca5ba024848fe1d1742cebb00f1d8254689cf044 1 parent f773219
@groovecoder groovecoder authored
View
74 apps/devmo/tests/__init__.py
@@ -2,8 +2,12 @@
import mock
-from django.conf import settings
+from django.conf import settings, UserSettingsHolder
from django.contrib.auth.models import User
+from django.utils.functional import wraps
+
+import constance.config
+from constance.backends import database as constance_database
import test_utils
from nose.plugins.skip import SkipTest
@@ -53,3 +57,71 @@ def test_new(self, fetch_user_feed):
class SkippedTestCase(test_utils.TestCase):
def setUp(self):
raise SkipTest()
+
+
+class overrider(object):
+ """
+ See http://djangosnippets.org/snippets/2437/
+
+ Acts as either a decorator, or a context manager. If it's a decorator it
+ takes a function and returns a wrapped function. If it's a contextmanager
+ it's used with the ``with`` statement. In either event entering/exiting
+ are called before and after, respectively, the function/block is executed.
+ """
+ def __init__(self, **kwargs):
+ self.options = kwargs
+
+ def __enter__(self):
+ self.enable()
+
+ def __exit__(self, exc_type, exc_value, traceback):
+ self.disable()
+
+ def __call__(self, func):
+ @wraps(func)
+ def inner(*args, **kwargs):
+ with self:
+ return func(*args, **kwargs)
+ return inner
+
+ def enable(self):
+ pass
+
+ def disable(self):
+ pass
+
+
+class override_constance_settings(overrider):
+ """Decorator / context manager to override constance settings and defeat
+its caching."""
+
+ def enable(self):
+ self.old_cache = constance_database.db_cache
+ constance_database.db_cache = None
+ self.old_settings = dict((k, getattr(constance.config, k))
+ for k in dir(constance.config))
+ for k, v in self.options.items():
+ constance.config._backend.set(k, v)
+
+ def disable(self):
+ for k, v in self.old_settings.items():
+ constance.config._backend.set(k, v)
+ constance_database.db_cache = self.old_cache
+
+
+class override_settings(overrider):
+ """Decorator / context manager to override Django settings"""
+
+ def enable(self):
+ self.old_settings = settings._wrapped
+ # brute-force settings.__dict__ to override test-utils' settings_test
+ if settings.__name__ == 'settings_test':
+ settings.__dict__.update(dict(self.options.items()))
+ else:
+ override = UserSettingsHolder(settings._wrapped)
+ for key, new_value in self.options.items():
+ setattr(override, key, new_value)
+ settings._wrapped = override
+
+ def disable(self):
+ settings._wrapped = self.old_settings
View
2  apps/search/tests/test_tasks.py
@@ -2,12 +2,14 @@
from search.tests import ElasticTestCase
+from devmo.tests import override_settings
from wiki.models import DocumentType
from wiki.tests import document
class TestLiveIndexing(ElasticTestCase):
+ @override_settings(ES_LIVE_INDEX=True)
def test_live_indexing_docs(self):
S = DocumentType.search
count_before = S().count()
View
1  apps/search/views.py
@@ -18,7 +18,6 @@ def jsonp_is_valid(func):
def search(request):
- import pdb; pdb.set_trace()
if not flag_is_active(request, 'elasticsearch'):
"""Google Custom Search results page."""
query = request.GET.get('q', '')
View
1  settings_test.py
@@ -1 +1,2 @@
CELERY_ALWAYS_EAGER = True
+ES_LIVE_INDEX = False
Please sign in to comment.
Something went wrong with that request. Please try again.