def test_update_indicators(settings, tmpdir): if not settings.DATABASES['default']['ENGINE'].startswith( 'django.db.backends.postgresql'): pytest.skip('postgresql only') settings.MEDIA_ROOT = tmpdir.strpath now = datetime.datetime(2016, 1, 1, 0, 0, 0) frame = pd.DataFrame([(1, 0.1), (2, 0.2)], columns=['datetime', 'y']).set_index('datetime', drop=True) # Update, because last_update is None Indicator.objects.create(slug='a', update_freq=10, last_update=None) # Do not update, because it was updated 5 seconds ago and update frequency is 10 seconds Indicator.objects.create(slug='b', update_freq=10, last_update=now - timedelta(seconds=5)) # Update, because it was updated 20 seconds ago and update frequency is 10 seconds Indicator.objects.create(slug='c', update_freq=10, last_update=now - timedelta(seconds=20)) # Do not update, because error count is greather than 10 Indicator.objects.create(slug='d', update_freq=10, last_update=now - timedelta(seconds=20), error_count=20) indicators_ = [ ('a', { 'fetch': mock.Mock(return_value=frame) }), ('b', { 'fetch': mock.Mock(return_value=frame) }), ('c', { 'fetch': mock.Mock(return_value=frame) }), ('d', { 'fetch': mock.Mock(return_value=frame) }), ] indicators.update_indicators(indicators_, now) indicator = Indicator.objects.get(slug='a') assert indicator.error_count == 0 assert indicator.traceback == '' assert indicator.last_update >= now assert ls(tmpdir, tmpdir / 'indicators') == [ 'indicators/a.csv', 'indicators/c.csv', ] assert tuples(pd.read_csv(str(tmpdir / 'indicators/a.csv'), index_col=0)) == [ (1, 0.1), (2, 0.2), ]
def test_update_indicators_error(settings, tmpdir): if not settings.DATABASES['default']['ENGINE'].startswith('django.db.backends.postgresql'): pytest.skip('postgresql only') settings.MEDIA_ROOT = tmpdir.strpath Indicator.objects.create(slug='a', last_update=None, update_freq=10) now = datetime.datetime(2016, 1, 1, 0, 0, 0) indicators_ = [('a', {'fetch': mock.Mock(side_effect=Exception())})] indicators.update_indicators(indicators_, now) indicator = Indicator.objects.get(slug='a') assert indicator.error_count == 1 assert indicator.traceback != '' assert indicator.last_update is None assert ls(tmpdir, tmpdir / 'indicators') == []
def test_update_indicators_error(settings, tmpdir): if not settings.DATABASES['default']['ENGINE'].startswith( 'django.db.backends.postgresql'): pytest.skip('postgresql only') settings.MEDIA_ROOT = tmpdir.strpath Indicator.objects.create(slug='a', last_update=None, update_freq=10) now = datetime.datetime(2016, 1, 1, 0, 0, 0) indicators_ = [('a', {'fetch': mock.Mock(side_effect=Exception())})] indicators.update_indicators(indicators_, now) indicator = Indicator.objects.get(slug='a') assert indicator.error_count == 1 assert indicator.traceback != '' assert indicator.last_update is None assert ls(tmpdir, tmpdir / 'indicators') == []
def test_update_indicators(settings, tmpdir): if not settings.DATABASES['default']['ENGINE'].startswith('django.db.backends.postgresql'): pytest.skip('postgresql only') settings.MEDIA_ROOT = tmpdir.strpath now = datetime.datetime(2016, 1, 1, 0, 0, 0) frame = pd.DataFrame([(1, 0.1), (2, 0.2)], columns=['datetime', 'y']).set_index('datetime', drop=True) # Update, because last_update is None Indicator.objects.create(slug='a', update_freq=10, last_update=None) # Do not update, because it was updated 5 seconds ago and update frequency is 10 seconds Indicator.objects.create(slug='b', update_freq=10, last_update=now - timedelta(seconds=5)) # Update, because it was updated 20 seconds ago and update frequency is 10 seconds Indicator.objects.create(slug='c', update_freq=10, last_update=now - timedelta(seconds=20)) # Do not update, because error count is greather than 10 Indicator.objects.create(slug='d', update_freq=10, last_update=now - timedelta(seconds=20), error_count=20) indicators_ = [ ('a', {'fetch': mock.Mock(return_value=frame)}), ('b', {'fetch': mock.Mock(return_value=frame)}), ('c', {'fetch': mock.Mock(return_value=frame)}), ('d', {'fetch': mock.Mock(return_value=frame)}), ] indicators.update_indicators(indicators_, now) indicator = Indicator.objects.get(slug='a') assert indicator.error_count == 0 assert indicator.traceback == '' assert indicator.last_update >= now assert ls(tmpdir, tmpdir / 'indicators') == [ 'indicators/a.csv', 'indicators/c.csv', ] assert tuples(pd.read_csv(str(tmpdir / 'indicators/a.csv'), index_col=0)) == [ (1, 0.1), (2, 0.2), ]
def handle(self, *args, **options): self._info(options['verbosity'], 'Importing indicators...') indicators.import_indicators(indicators.INDICATORS) self._info(options['verbosity'], 'Updating indicators...') indicators.update_indicators(indicators.INDICATORS)