Skip to content

Commit

Permalink
Validaciones sobre generación de dumps
Browse files Browse the repository at this point in the history
Atacan casos borde en donde no existen series de tiempo sobre los nodos,
o no se pudieron indexar correctamente
  • Loading branch information
lucaslavandeira committed Dec 11, 2018
1 parent a65e554 commit 8676360
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions series_tiempo_ar_api/apps/dump/generator/dta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 10 additions & 1 deletion series_tiempo_ar_api/apps/dump/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions series_tiempo_ar_api/apps/dump/tests/csv_generator_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions series_tiempo_ar_api/apps/dump/tests/dta_generator_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 8676360

Please sign in to comment.