Tweak django tests to skip better
If django isn't installed, we want all the django tests to get skipped. This fixes an issue with get_es not existing because it doesn't get imported because django isn't installed.
This commit is contained in:
Родитель
dfee06475b
Коммит
b086f47a7c
|
@ -29,22 +29,38 @@ except ImportError:
|
|||
SKIP_TESTS = True
|
||||
|
||||
|
||||
def requires_django(fun):
|
||||
def require_django_or_skip(fun):
|
||||
@wraps(fun)
|
||||
def _requires_django(*args, **kwargs):
|
||||
def _require_django_or_skip(*args, **kwargs):
|
||||
if SKIP_TESTS:
|
||||
raise SkipTest
|
||||
return fun(*args, **kwargs)
|
||||
return _requires_django
|
||||
return _require_django_or_skip
|
||||
|
||||
|
||||
class DjangoElasticTestCase(ElasticTestCase):
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
if cls.skip_tests or SKIP_TESTS:
|
||||
return
|
||||
|
||||
super(DjangoElasticTestCase, cls).setup_class()
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
if cls.skip_tests or SKIP_TESTS:
|
||||
return
|
||||
|
||||
super(DjangoElasticTestCase, cls).setup_class()
|
||||
|
||||
|
||||
class TestS(TestCase):
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_require_mapping_type(self):
|
||||
"""The Django S requires a mapping type."""
|
||||
self.assertRaises(TypeError, S)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_get_indexes(self):
|
||||
"""Test get_indexes always returns a list of strings."""
|
||||
|
||||
|
@ -72,7 +88,7 @@ class TestS(TestCase):
|
|||
s = S(FakeDjangoMappingType).indexes('footest').indexes('footest2')
|
||||
eq_(s.get_indexes(), ['footest2'])
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_get_doctypes(self):
|
||||
"""Test get_doctypes always returns a list of strings."""
|
||||
# Pulls from ._meta.db_table.
|
||||
|
@ -90,13 +106,14 @@ class TestS(TestCase):
|
|||
eq_(s.get_doctypes(), ['footype2'])
|
||||
|
||||
|
||||
class QueryTest(ElasticTestCase):
|
||||
class QueryTest(DjangoElasticTestCase):
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
super(QueryTest, cls).setup_class()
|
||||
if cls.skip_tests or SKIP_TESTS:
|
||||
return
|
||||
|
||||
super(QueryTest, cls).setup_class()
|
||||
|
||||
cls.create_index()
|
||||
|
||||
data = [
|
||||
|
@ -116,16 +133,16 @@ class QueryTest(ElasticTestCase):
|
|||
|
||||
cls.refresh()
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_q(self):
|
||||
eq_(len(S(FakeDjangoMappingType).query(foo='bar')), 1)
|
||||
eq_(len(S(FakeDjangoMappingType).query(foo='car')), 2)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_q_all(self):
|
||||
eq_(len(S(FakeDjangoMappingType)), 5)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_empty_f(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F() | F(tag='awesome'))), 3)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F() & F(tag='awesome'))), 3)
|
||||
|
@ -133,22 +150,22 @@ class QueryTest(ElasticTestCase):
|
|||
eq_(len(S(FakeDjangoMappingType).filter(F() & F() & F(tag='awesome'))), 3)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F())), 5)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(tag='awesome')), 3)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F(tag='awesome'))), 3)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_and(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(tag='awesome', foo='bar')), 1)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(tag='awesome').filter(foo='bar')), 1)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F(tag='awesome') & F(foo='bar'))), 1)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_or(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F(tag='awesome') | F(tag='boat'))), 4)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_or_3(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F(tag='awesome') | F(tag='boat') |
|
||||
F(tag='boring'))), 5)
|
||||
|
@ -157,29 +174,29 @@ class QueryTest(ElasticTestCase):
|
|||
'width': '5'}
|
||||
})), 3)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_complicated(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(F(tag='awesome', foo='bar') |
|
||||
F(tag='boring'))), 2)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_not(self):
|
||||
eq_(len(S(FakeDjangoMappingType).filter(~F(tag='awesome'))), 2)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(~(F(tag='boring') | F(tag='boat')))), 3)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(~F(tag='boat')).filter(~F(foo='bar'))), 3)
|
||||
eq_(len(S(FakeDjangoMappingType).filter(~F(tag='boat', foo='barf'))), 5)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filter_bad_field_action(self):
|
||||
with self.assertRaises(InvalidFieldActionError):
|
||||
len(S(FakeDjangoMappingType).filter(F(tag__faux='awesome')))
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_facet(self):
|
||||
qs = S(FakeDjangoMappingType).facet('tag')
|
||||
eq_(facet_counts_dict(qs, 'tag'), dict(awesome=3, boring=1, boat=1))
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_filtered_facet(self):
|
||||
qs = S(FakeDjangoMappingType).query(foo='car').filter(width=5)
|
||||
|
||||
|
@ -191,7 +208,7 @@ class QueryTest(ElasticTestCase):
|
|||
eq_(facet_counts_dict(qs.facet('tag', filtered=True), 'tag'),
|
||||
{'awesome': 1})
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_global_facet(self):
|
||||
qs = S(FakeDjangoMappingType).query(foo='car').filter(width=5)
|
||||
|
||||
|
@ -203,7 +220,7 @@ class QueryTest(ElasticTestCase):
|
|||
eq_(facet_counts_dict(qs.facet('tag', global_=True), 'tag'),
|
||||
dict(awesome=3, boring=1, boat=1))
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_facet_raw(self):
|
||||
qs = S(FakeDjangoMappingType).facet_raw(tags={'terms': {'field': 'tag'}})
|
||||
eq_(facet_counts_dict(qs, 'tags'),
|
||||
|
@ -215,7 +232,7 @@ class QueryTest(ElasticTestCase):
|
|||
eq_(facet_counts_dict(qs, 'tags'),
|
||||
{'awesome': 2})
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_facet_raw_overrides_facet(self):
|
||||
"""facet_raw overrides facet with the same facet name."""
|
||||
qs = (S(FakeDjangoMappingType)
|
||||
|
@ -225,12 +242,12 @@ class QueryTest(ElasticTestCase):
|
|||
eq_(facet_counts_dict(qs, 'tag'),
|
||||
dict(awesome=3, boring=1, boat=1))
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_order_by(self):
|
||||
res = S(FakeDjangoMappingType).filter(tag='awesome').order_by('-width')
|
||||
eq_([d.id for d in res], [5, 3, 1])
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_repr(self):
|
||||
res = S(FakeDjangoMappingType)[:2]
|
||||
list_ = list(res)
|
||||
|
@ -238,28 +255,26 @@ class QueryTest(ElasticTestCase):
|
|||
eq_(repr(list_), repr(res))
|
||||
|
||||
|
||||
class IndexableTest(ElasticTestCase):
|
||||
class IndexableTest(DjangoElasticTestCase):
|
||||
index_name = 'elasticutilstest'
|
||||
|
||||
@classmethod
|
||||
def get_es(cls):
|
||||
return get_es()
|
||||
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
super(IndexableTest, cls).setup_class()
|
||||
if cls.skip_tests or SKIP_TESTS:
|
||||
return
|
||||
|
||||
def setUp(self):
|
||||
super(IndexableTest, self).setUp()
|
||||
if self.skip_tests or SKIP_TESTS:
|
||||
return
|
||||
IndexableTest.create_index()
|
||||
|
||||
def tearDown(self):
|
||||
super(IndexableTest, self).tearDown()
|
||||
if self.skip_tests or SKIP_TESTS:
|
||||
return
|
||||
IndexableTest.cleanup_index()
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_index(self):
|
||||
document = {'id': 1, 'name': 'odin skullcrusher'}
|
||||
|
||||
|
@ -274,7 +289,7 @@ class IndexableTest(ElasticTestCase):
|
|||
# Query it to make sure it's there.
|
||||
eq_(len(S(FakeDjangoMappingType).query(name__prefix='odin')), 1)
|
||||
|
||||
@requires_django
|
||||
@require_django_or_skip
|
||||
def test_bulk_index(self):
|
||||
documents = [
|
||||
{'id': 1, 'name': 'odin skullcrusher'},
|
||||
|
|
Загрузка…
Ссылка в новой задаче