diff --git a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py index 012d47cc..5bd0d943 100644 --- a/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py +++ b/series_tiempo_ar_api/apps/api/query/es_query/response_formatter.py @@ -33,10 +33,11 @@ def format_response(self): timestamp_dict = self.data_dict.setdefault(hit['key_as_string'], {}) timestamp_dict[self._data_dict_series_key(self.series[i])] = data else: + response = filter(lambda hit, mode=rep_mode: mode in hit, response) for hit in response: - data = hit[rep_mode] if rep_mode in hit else None timestamp_dict = self.data_dict.setdefault(hit.timestamp, {}) - timestamp_dict[self._data_dict_series_key(self.series[i])] = data + series = self._data_dict_series_key(self.series[i]) + timestamp_dict[series] = hit[rep_mode] if not self.data_dict: # No hay datos return [] diff --git a/series_tiempo_ar_api/apps/api/tests/es_query_tests.py b/series_tiempo_ar_api/apps/api/tests/es_query_tests.py index ece2564e..65aa4396 100644 --- a/series_tiempo_ar_api/apps/api/tests/es_query_tests.py +++ b/series_tiempo_ar_api/apps/api/tests/es_query_tests.py @@ -342,8 +342,8 @@ def test_end_of_period_with_rep_mode(self): orig_eop.sort('asc') end_of_period = orig_eop.run() - for i, row in enumerate(data[1:], 1): # El primero es nulo en pct change - value = end_of_period[i][1] / end_of_period[i - 1][1] - 1 + for i, row in enumerate(data): # El primero es nulo en pct change + value = end_of_period[i + 1][1] / end_of_period[i][1] - 1 self.assertAlmostEqual(value, row[1]) @@ -462,3 +462,15 @@ def test_min_max_collapse_agg_with_rep_mode(self): min_val = max([row[1] for row in data]) self.assertAlmostEqual(min_val, other_data[0][1], places=5) + + def test_rep_mode_has_no_leading_nulls(self): + self.query.add_series(self.single_series, 'percent_change', self.series_periodicity) + data = self.query.run() + + self.assertIsNotNone(data[0][1]) + + def test_year_rep_mode_has_no_leading_nulls(self): + self.query.add_series(self.single_series, 'percent_change_a_year_ago', self.series_periodicity) + data = self.query.run() + + self.assertIsNotNone(data[0][1]) diff --git a/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py b/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py index b4ec2628..75259922 100644 --- a/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py +++ b/series_tiempo_ar_api/apps/api/tests/pipeline_tests/ids_tests.py @@ -57,8 +57,8 @@ def test_global_rep_mode(self): other_query.sort('asc') other_data = other_query.run()['data'] - for index, row in enumerate(other_data[1:], start=1): - change = data[index][1] - data[index - 1][1] + for index, row in enumerate(other_data): + change = data[index + 1][1] - data[index][1] # La resta anterior trae pérdida de precisión si los números de 'data' son grandes self.assertAlmostEqual(row[1], change, places=5)