Permalink
Browse files

Fix tests so they run without Django

  • Loading branch information...
1 parent b5c8ba6 commit 3daf91b44730e55e5788d751d894c530ac71c5ff @willkg willkg committed Jun 28, 2012
@@ -11,7 +11,7 @@
try:
from django.conf import settings
except ImportError:
- import es_settings as settings
+ pass
try:
from statsd import statsd
@@ -1,26 +1,49 @@
+from functools import wraps
from unittest import TestCase
+from nose import SkipTest
from nose.tools import eq_
import pyes.exceptions
-from django.conf import settings
-
-from elasticutils.contrib.django import F, S, get_es, InvalidFieldActionError
from elasticutils.tests import FakeModel, ElasticTestCase
+# TODO: To run this file or import it requires Django be installed.
+# If Django isn't installed, we want to skip each test individually.
+# However, those requirements create a lot of tangled stuff in here.
+# It'd be nice if we could do this in a less tangled way and also
+
+
+def requires_django(fun):
+ @wraps(fun)
+ def _requires_django(*args, **kwargs):
+ try:
+ import django
+ except ImportError:
+ raise SkipTest
+ return fun(*args, **kwargs)
+ return _requires_django
+
+
class ESTest(TestCase):
+ @requires_django
def test_get_es_defaults(self):
"""Test that the ES has the correct defaults"""
+ from django.conf import settings
+ from elasticutils.contrib.django import get_es
+
es = get_es()
eq_(es.timeout, settings.ES_TIMEOUT)
# dump_curl defaults to False, but if dump_curl is Falsey,
# then pyes.es.ES sets its dump_curl attribute to None.
eq_(es.dump_curl, None)
eq_(es.default_indexes, [settings.ES_INDEXES['default']])
+ @requires_django
def test_get_es_overriding_defaults(self):
"""Test that overriding defaults works"""
+ from elasticutils.contrib.django import get_es
+
class Dumper(object):
def write(self, val):
print val
@@ -43,6 +66,14 @@ def setup_class(cls):
if cls.skip_tests:
return
+ try:
+ import django
+ except ImportError:
+ cls.skip_tests = True
+ return
+
+ from elasticutils.contrib.django import get_es
+
es = get_es()
try:
es.delete_index_if_exists(cls.index_name)
@@ -69,68 +100,109 @@ def teardown_class(cls):
if cls.skip_tests:
return
+ from elasticutils.contrib.django import get_es
+
es = get_es()
es.delete_index(cls.index_name)
+ @requires_django
def test_q(self):
+ from elasticutils.contrib.django import S
+
eq_(len(S(FakeModel).query(foo='bar')), 1)
eq_(len(S(FakeModel).query(foo='car')), 2)
+ @requires_django
def test_q_all(self):
+ from elasticutils.contrib.django import S
+
eq_(len(S(FakeModel)), 5)
+ @requires_django
def test_filter_empty_f(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(F() | F(tag='awesome'))), 3)
eq_(len(S(FakeModel).filter(F() & F(tag='awesome'))), 3)
eq_(len(S(FakeModel).filter(F() | F() | F(tag='awesome'))), 3)
eq_(len(S(FakeModel).filter(F() & F() & F(tag='awesome'))), 3)
eq_(len(S(FakeModel).filter(F())), 5)
+ @requires_django
def test_filter(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(tag='awesome')), 3)
eq_(len(S(FakeModel).filter(F(tag='awesome'))), 3)
+ @requires_django
def test_filter_and(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(tag='awesome', foo='bar')), 1)
eq_(len(S(FakeModel).filter(tag='awesome').filter(foo='bar')), 1)
eq_(len(S(FakeModel).filter(F(tag='awesome') & F(foo='bar'))), 1)
+ @requires_django
def test_filter_or(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(F(tag='awesome') | F(tag='boat'))), 4)
+ @requires_django
def test_filter_or_3(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(F(tag='awesome') | F(tag='boat') |
F(tag='boring'))), 5)
eq_(len(S(FakeModel).filter(or_={'foo': 'bar',
'or_': {'tag': 'boat',
'width': '5'}
})), 3)
+ @requires_django
def test_filter_complicated(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(F(tag='awesome', foo='bar') |
F(tag='boring'))), 2)
+ @requires_django
def test_filter_not(self):
+ from elasticutils.contrib.django import S, F
+
eq_(len(S(FakeModel).filter(~F(tag='awesome'))), 2)
eq_(len(S(FakeModel).filter(~(F(tag='boring') | F(tag='boat')))), 3)
eq_(len(S(FakeModel).filter(~F(tag='boat')).filter(~F(foo='bar'))), 3)
eq_(len(S(FakeModel).filter(~F(tag='boat', foo='barf'))), 5)
+ @requires_django
def test_filter_bad_field_action(self):
+ from elasticutils.contrib.django import S, F, InvalidFieldActionError
+
with self.assertRaises(InvalidFieldActionError):
len(S(FakeModel).filter(F(tag__faux='awesome')))
+ @requires_django
def test_facet(self):
+ from elasticutils.contrib.django import S
+
qs = S(FakeModel).facet(tags={'terms': {'field': 'tag'}})
tag_counts = dict((t['term'], t['count']) for t in qs.facets['tags'])
eq_(tag_counts, dict(awesome=3, boring=1, boat=1))
+ @requires_django
def test_order_by(self):
+ from elasticutils.contrib.django import S
+
res = S(FakeModel).filter(tag='awesome').order_by('-width')
eq_([d.id for d in res], [5, 3, 1])
+ @requires_django
def test_repr(self):
+ from elasticutils.contrib.django import S
+
res = S(FakeModel)[:2]
list_ = list(res)
@@ -1,5 +1,4 @@
from nose.tools import eq_
-import pyes.exceptions
from elasticutils import F, S, InvalidFieldActionError
from elasticutils.tests import FakeModel, ElasticTestCase
File renamed without changes.

0 comments on commit 3daf91b

Please sign in to comment.