From 867636012e498fa1c3f921b155025dc0a81a6745 Mon Sep 17 00:00:00 2001 From: Lucas Lavandeira <19612265+lucaslavandeira@users.noreply.github.com> Date: Tue, 11 Dec 2018 12:25:03 -0300 Subject: [PATCH] =?UTF-8?q?Validaciones=20sobre=20generaci=C3=B3n=20de=20d?= =?UTF-8?q?umps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Atacan casos borde en donde no existen series de tiempo sobre los nodos, o no se pudieron indexar correctamente --- series_tiempo_ar_api/apps/dump/generator/dta.py | 5 +++++ series_tiempo_ar_api/apps/dump/generator/generator.py | 11 ++++++++++- .../apps/dump/tests/csv_generator_tests.py | 10 ++++++++++ .../apps/dump/tests/dta_generator_tests.py | 7 +++++++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/series_tiempo_ar_api/apps/dump/generator/dta.py b/series_tiempo_ar_api/apps/dump/generator/dta.py index 14a6850c..93b292c6 100644 --- a/series_tiempo_ar_api/apps/dump/generator/dta.py +++ b/series_tiempo_ar_api/apps/dump/generator/dta.py @@ -24,6 +24,11 @@ def generate_dump(self, dump_name): dump_file = DumpFile.objects.filter(file_type=DumpFile.TYPE_CSV, file_name=dump_name, node=self.node).last() + + if dump_file is None: + GenerateDumpTask.info(self.task, f"No hay dumps CSV generados para el nodo {self.node.catalog_id}") + return + df = pd.read_csv(dump_file.file) if dump_name == DumpFile.FILENAME_VALUES: df = df[constants.STATA_VALUES_COLS] diff --git a/series_tiempo_ar_api/apps/dump/generator/generator.py b/series_tiempo_ar_api/apps/dump/generator/generator.py index 0bdf35de..78404792 100644 --- a/series_tiempo_ar_api/apps/dump/generator/generator.py +++ b/series_tiempo_ar_api/apps/dump/generator/generator.py @@ -34,8 +34,13 @@ def init_data(self): fields = Field.objects.all() if self.catalog: + try: + catalog = Catalog.objects.get(identifier=self.catalog) + except Catalog.DoesNotExist: + return + fields = Field.objects.filter( - distribution__dataset__catalog=Catalog.objects.get(identifier=self.catalog) + distribution__dataset__catalog=catalog ) fields = fields.filter( @@ -72,6 +77,10 @@ def init_data(self): } def generate(self): + if not self.fields: + GenerateDumpTask.info(self.task, f"No hay series cargadas para el catálogo {self.catalog}") + return + FullCsvGenerator(self.task, self.fields, self.catalog).generate() ValuesCsvGenerator(self.task, self.fields, self.catalog).generate() SourcesCsvGenerator(self.task, self.fields, self.catalog).generate() diff --git a/series_tiempo_ar_api/apps/dump/tests/csv_generator_tests.py b/series_tiempo_ar_api/apps/dump/tests/csv_generator_tests.py index 78055134..99018837 100644 --- a/series_tiempo_ar_api/apps/dump/tests/csv_generator_tests.py +++ b/series_tiempo_ar_api/apps/dump/tests/csv_generator_tests.py @@ -37,6 +37,16 @@ def setUpClass(cls): gen = DumpGenerator(cls.task) gen.generate() + def test_invalid_catalog(self): + task = GenerateDumpTask() + task.save() + + gen = DumpGenerator(task, "no_catalog") + gen.generate() + task.refresh_from_db() + + self.assertFalse(DumpFile.objects.filter(file_type=DumpFile.TYPE_CSV, task=task)) + def test_values_dump(self): file = self.task.dumpfile_set.get(file_name=DumpFile.FILENAME_VALUES).file reader = read_file_as_csv(file) diff --git a/series_tiempo_ar_api/apps/dump/tests/dta_generator_tests.py b/series_tiempo_ar_api/apps/dump/tests/dta_generator_tests.py index 3f7fe1c5..8cd39600 100644 --- a/series_tiempo_ar_api/apps/dump/tests/dta_generator_tests.py +++ b/series_tiempo_ar_api/apps/dump/tests/dta_generator_tests.py @@ -3,6 +3,7 @@ import faker import pandas as pd from django.test import TestCase +from django_datajsonar.models import Node from series_tiempo_ar_api.apps.dump.generator import constants from series_tiempo_ar_api.apps.dump.generator.dta import DtaGenerator @@ -25,6 +26,12 @@ def setUpClass(cls): enqueue_write_csv_task() super(DtaGeneratorTests, cls).setUpClass() + def test_generate_dta_no_csv_loaded(self): + node = Node.objects.create(catalog_id="empty_catalog", catalog_url="test.com", indexable=True) + task = GenerateDumpTask.objects.create() + DtaGenerator(task.id).generate() + self.assertFalse(DumpFile.objects.filter(node=node, file_type=DumpFile.TYPE_DTA)) + def test_generate_dta(self): task = GenerateDumpTask.objects.create() DtaGenerator(task.id).generate()