Skip to content

Commit c9b369a

Browse files
committed
add test for es_update --start and fix regression in --start flag
#39 es_update start flag broken
1 parent 1e4d4ad commit c9b369a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

django_elastic_migrations/management/commands/es_update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def add_arguments(self, parser):
4747
"--start",
4848
dest="start_date",
4949
help="The start date for indexing. Can be any dateutil-parsable string;"
50-
" YYYY-MM-DDTHH:MM:SS is recommended to avoid confusion",
50+
"YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS is recommended to avoid confusion",
5151
)
5252

5353
def handle(self, *args, **options):

django_elastic_migrations/models.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,14 @@ def __init__(self, *args, **kwargs):
965965
super(UpdateIndexAction, self).__init__(*args, **kwargs)
966966
if self.task_kwargs == '{}' and self.self_kwargs:
967967
# retain a history of how this command was called
968-
self.task_kwargs = json.dumps(self.self_kwargs, sort_keys=True)
968+
try:
969+
self.task_kwargs = json.dumps(self.self_kwargs, sort_keys=True)
970+
except TypeError as e:
971+
if 'Object of type datetime is not JSON serializable' in str(e):
972+
for key, val in self.self_kwargs.items():
973+
if isinstance(val, datetime.date):
974+
self.self_kwargs[key] = str(val)
975+
self.task_kwargs = json.dumps(self.self_kwargs, sort_keys=True)
969976

970977
self._batch_num = 0
971978
self._expected_remaining = 0

tests/test_management_commands.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import (absolute_import, division, print_function, unicode_literals)
22

33
import logging
4+
from datetime import datetime, timedelta
45
from unittest import skip
56

67
from django.contrib.humanize.templatetags.humanize import ordinal
@@ -17,6 +18,10 @@
1718
log = logging.getLogger(__file__)
1819

1920

21+
def days_ago(d):
22+
return datetime.now() - timedelta(days=d)
23+
24+
2025
# noinspection PyUnresolvedReferences
2126
class CommonDEMTestUtilsMixin(object):
2227

@@ -227,6 +232,26 @@ def test_newer_flag(self):
227232
actual_num=actual_num_docs
228233
))
229234

235+
def test_start_flag(self):
236+
self.check_basic_setup_and_get_models()
237+
238+
num_docs = MovieSearchIndex.get_num_docs()
239+
self.assertEqual(num_docs, 0)
240+
241+
Movie.objects.all().update(last_modified=days_ago(3))
242+
call_command('es_update', 'movies', '--start={}'.format(days_ago(2).isoformat()))
243+
num_docs = MovieSearchIndex.get_num_docs()
244+
# we had last modified set to three days ago, and we only updated last two days,
245+
# so we should not have any.
246+
self.assertEqual(num_docs, 0)
247+
248+
Movie.objects.all().update(last_modified=days_ago(1))
249+
call_command('es_update', 'movies', '--start={}'.format(days_ago(2).isoformat()))
250+
num_docs = MovieSearchIndex.get_num_docs()
251+
# we had last modified set to one days ago, and we only updated last two days,
252+
# so we should have two
253+
self.assertEqual(num_docs, 2)
254+
230255

231256
@skip("Skipped multiprocessing tests until SQLLite can be integrated into test setup")
232257
# @tag('multiprocessing')

0 commit comments

Comments
 (0)