Skip to content

Commit

Permalink
#1 - Remove es dsl.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentguilbert committed Jan 13, 2017
1 parent 120e506 commit f691c23
Show file tree
Hide file tree
Showing 15 changed files with 199 additions and 545 deletions.
24 changes: 18 additions & 6 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@

class BaseTestCase(TransactionTestCase):

def setUp(self):
super(BaseTestCase, self).setUp()
self.trampoline_config = trampoline_config

def refresh(self):
trampoline_config.es.indices.refresh('_all')

def docExists(self, obj):
def docExists(self, obj, token_id=None):
doc_type = obj.get_es_doc_type()
index = obj.get_es_index()
return trampoline_config.es.exists(
index=index,
doc_type=doc_type,
id=obj.pk
id=token_id or obj.pk
)

def aliasExists(self, index, name):
Expand Down Expand Up @@ -53,8 +57,16 @@ def assertTypeExists(self, index, doc_type):
def assertTypeDoesntExist(self, index, doc_type):
self.assertFalse(self.typeExists(index, doc_type))

def assertDocExists(self, obj):
self.assertTrue(self.docExists(obj))
def assertDocExists(self, obj, token_id=None):
self.assertTrue(self.docExists(obj, token_id))

def assertDocDoesntExist(self, obj, token_id=None):
self.assertFalse(self.docExists(obj, token_id))

def createIndex(self, index):
trampoline_config.es.indices.create(index=index)
self.refresh()

def assertDocDoesntExist(self, obj):
self.assertFalse(self.docExists(obj))
def deleteIndex(self, index):
trampoline_config.es.indices.delete(index=index, ignore=404)
self.refresh()
5 changes: 5 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ def is_indexable(self):
return False
return True

def get_es_body(self):
if self.name == 'raise_exception':
raise RuntimeError
return super(Token, self).get_es_body()

@classmethod
def get_es_doc_type_mapping(self):
return {
Expand Down
1 change: 0 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
'disabled': False,
'fail_silently': True,
},
'VERSION_SUFFIX': '_1',
}

##################################################
Expand Down
213 changes: 49 additions & 164 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,135 +4,96 @@
from django.conf import settings
from django.core.management import call_command

from elasticsearch_dsl import Index

from tests.base import BaseTestCase
from tests.models import Token
from trampoline import get_trampoline_config

trampoline_config = get_trampoline_config()


class TestCommands(BaseTestCase):

def tearDown(self):
super(TestCommands, self).tearDown()
# Delete remnants of previous tests.
Index('foobar').delete(ignore=404)
Index('foobar_target').delete(ignore=404)

def test_es_create_index(self):
# Index name required.
with self.assertRaises(SystemExit):
call_command('es_create_index')
self.deleteIndex('foobar_target')
self.deleteIndex('foobar')
self.deleteIndex(Token.get_es_index())

# Index name isn't defined.
with self.assertRaises(SystemExit):
call_command('es_create_index', index_name='doesntexist')
def test_es_create_documents(self):
settings.TRAMPOLINE['OPTIONS']['disabled'] = True
token = Token.objects.create()
settings.TRAMPOLINE['OPTIONS']['disabled'] = False
self.assertDocDoesntExist(token)

# Dry run.
call_command(
'es_create_index',
index_name='foobar',
target_name='foobar_target',
dry_run=True
)
self.assertIndexDoesntExist('foobar_target')
call_command('es_create_documents', dry_run=True)
self.assertDocDoesntExist(token)

# Successful call.
call_command(
'es_create_index',
index_name='foobar',
target_name='foobar_target'
)
self.assertIndexExists('foobar_target')
self.assertTypeExists(index='foobar_target', doc_type='token')
self.assertIndexDoesntExist('foobar')
call_command('es_create_documents')
self.assertDocExists(token)

# Index already exists.
# If verbosity is not an int it defaults to 1.
with self.assertRaises(SystemExit):
call_command(
'es_create_index',
index_name='foobar',
target_name='foobar_target',
traceback=True,
verbosity='notint'
)
settings.TRAMPOLINE['OPTIONS']['disabled'] = True
token = Token.objects.create(name='raise_exception')
settings.TRAMPOLINE['OPTIONS']['disabled'] = False

def test_es_delete_index(self):
# Index name required.
with self.assertRaises(SystemExit):
call_command('es_delete_index')
call_command('es_create_documents')
self.assertDocDoesntExist(token)

# Index doesn't exist.
with self.assertRaises(SystemExit):
call_command(
'es_delete_index',
index_name='foobar',
yes=True
)
def test_es_create_indices(self):
# Dry run.
call_command('es_create_indices', dry_run=True)
self.assertIndexDoesntExist(Token.get_es_index())

index = Index('foobar')
index.create()
self.refresh()
self.assertIndexExists('foobar')
call_command('es_create_indices')
self.assertIndexExists(Token.get_es_index())

call_command(
'es_delete_index',
index_name='foobar',
yes=True
)
self.assertIndexDoesntExist('foobar')
# Skip already created indices silently.
call_command('es_create_indices')

def test_es_create_alias(self):
# Index name required.
with self.assertRaises(SystemExit):
call_command(
'es_create_alias',
target_name='foobar_target'
target='foobar_target'
)

# Target name required.
with self.assertRaises(SystemExit):
call_command(
'es_create_alias',
index_name='foobar'
index='foobar'
)

# Index doesn't exist.
with self.assertRaises(SystemExit):
call_command(
'es_create_alias',
index_name='foobar',
target_name='foobar_target'
index='foobar',
target='foobar_target'
)

index = Index('foobar_target')
index.create()
self.refresh()
self.createIndex('foobar_target')

# Alias with same name as index.
with self.assertRaises(SystemExit):
call_command(
'es_create_alias',
index_name='foobar_target',
target_name='foobar_target'
index='foobar_target',
target='foobar_target'
)

# Dry run.
call_command(
'es_create_alias',
index_name='foobar',
target_name='foobar_target',
index='foobar',
target='foobar_target',
dry_run=True
)
self.assertAliasDoesntExist(index='foobar_target', name='foobar')

call_command(
'es_create_alias',
index_name='foobar',
target_name='foobar_target'
index='foobar',
target='foobar_target'
)
self.assertAliasExists(index='foobar_target', name='foobar')

Expand All @@ -141,122 +102,46 @@ def test_es_delete_alias(self):
with self.assertRaises(SystemExit):
call_command(
'es_delete_alias',
target_name='foobar_target'
target='foobar_target'
)

# Target name required.
with self.assertRaises(SystemExit):
call_command(
'es_delete_alias',
index_name='foobar'
index='foobar'
)

# Index doesn't exist.
with self.assertRaises(SystemExit):
call_command(
'es_delete_alias',
index_name='foobar',
target_name='foobar_target',
index='foobar',
target='foobar_target',
yes=True
)

index = Index('foobar_target')
index.create()
self.refresh()
self.createIndex('foobar_target')

# Alias doesn't exist.
with self.assertRaises(SystemExit):
call_command(
'es_delete_alias',
index_name='foobar',
target_name='foobar_target',
index='foobar',
target='foobar_target',
yes=True
)

trampoline_config.connection.indices.put_alias(
index='foobar_target', name='foobar')
self.trampoline_config.es.indices.put_alias(
index='foobar_target',
name='foobar'
)
self.assertAliasExists(index='foobar_target', name='foobar')

call_command(
'es_delete_alias',
index_name='foobar',
target_name='foobar_target',
index='foobar',
target='foobar_target',
yes=True
)
self.assertAliasDoesntExist(index='foobar_target', name='foobar')

def test_es_create_documents(self):
# Index name required.
with self.assertRaises(SystemExit):
call_command('es_create_documents')

# index_name not in settings.
with self.assertRaises(SystemExit):
call_command(
'es_create_documents',
index_name='barfoo'
)

# Index doesn't exist.
with self.assertRaises(SystemExit):
call_command(
'es_create_documents',
index_name='foobar'
)

index = Index('foobar')
doc_type = Token.get_es_doc_type()
index.doc_type(doc_type)
index.create()
self.refresh()

# Disable auto indexing while creating objects.
settings.TRAMPOLINE['OPTIONS']['disabled'] = True
token = Token.objects.create(name='token')
token_not_indexable = Token.objects.create(name='not_indexable')
token_raise_exception = Token.objects.create(name='raise_exception')
settings.TRAMPOLINE['OPTIONS']['disabled'] = False

# Dry run.
call_command(
'es_create_documents',
index_name='foobar',
dry_run=True
)
self.assertDocDoesntExist(token)
self.assertDocDoesntExist(token_not_indexable)
self.assertDocDoesntExist(token_raise_exception)

# Cleanup stale documents.
token_stale = Token.objects.create(name='stale')
token_stale_pk = token_stale.pk
settings.TRAMPOLINE['OPTIONS']['disabled'] = True
token_stale.delete()
settings.TRAMPOLINE['OPTIONS']['disabled'] = False
self.refresh()

self.assertTrue(self.docExists(token_stale, token_stale_pk))
call_command(
'es_create_documents',
index_name='foobar',
cleanup=True
)

self.assertFalse(self.docExists(token_stale, token_stale_pk))
self.assertDocExists(token)
self.assertDocDoesntExist(token_not_indexable)
self.assertDocDoesntExist(token_raise_exception)

# Handle bad threads value.
call_command(
'es_create_documents',
index_name='foobar',
dry_run=True,
max_threads='foobar'
)
call_command(
'es_create_documents',
index_name='foobar',
dry_run=True,
max_threads=-42
)
Loading

0 comments on commit f691c23

Please sign in to comment.