Skip to content

Commit

Permalink
Merge 07b590d into b1a2294
Browse files Browse the repository at this point in the history
  • Loading branch information
lrromero committed Apr 9, 2019
2 parents b1a2294 + 07b590d commit e855f6a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pydatajson/writers.py
Expand Up @@ -445,9 +445,9 @@ def _generate_field_table(catalog):
def _generate_theme_table(catalog):
headers = []
themes = []

catalog_themes = catalog.get_themes() or []
# tabula diccionarios con estructura, como listas planas de diccionarios
for theme in catalog.get_themes():
for theme in catalog_themes:
tab_theme = _tabulate_nested_dict(theme, "theme")
themes.append(tab_theme)

Expand Down
38 changes: 24 additions & 14 deletions tests/test_readers_and_writers.py
Expand Up @@ -7,10 +7,11 @@

import os.path
import unittest

import nose
import vcr

from tempfile import NamedTemporaryFile

from tests.support.factories.xlsx import CSV_TABLE, WRITE_XLSX_TABLE
from tests.support.factories.xlsx import READ_XLSX_TABLE

Expand All @@ -19,10 +20,11 @@
except ImportError:
from unittest import mock
import filecmp
from .context import pydatajson
from tests.context import pydatajson
from pydatajson.core import DataJson
from pydatajson.helpers import ensure_dir_exists
from pydatajson.custom_exceptions import NonParseableCatalog
from . import xl_methods
from tests import xl_methods
import openpyxl as pyxl

my_vcr = vcr.VCR(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
Expand All @@ -45,7 +47,7 @@ def setUp(cls):
ensure_dir_exists(cls.SAMPLES_DIR)
ensure_dir_exists(cls.RESULTS_DIR)
ensure_dir_exists(cls.TEMP_DIR)
cls.dj = pydatajson.DataJson()
cls.dj = DataJson()
cls.maxDiff = None
cls.longMessage = True

Expand Down Expand Up @@ -163,7 +165,7 @@ def test_read_local_xlsx_catalog(self):

def test_read_written_xlsx_catalog(self):
"""read_catalog puede leer XLSX creado por write_xlsx_catalog"""
original_catalog = pydatajson.DataJson(
original_catalog = DataJson(
os.path.join(self.SAMPLES_DIR, "catalogo_justicia.json"))

tmp_xlsx = os.path.join(self.TEMP_DIR, "xlsx_catalog.xlsx")
Expand Down Expand Up @@ -227,19 +229,16 @@ def test_write_json_catalog_is_write_json(self, mock_write_json):

def test_read_write_both_formats_yields_the_same(self):
for suffix in ['xlsx', 'json']:
catalog = pydatajson.core.DataJson(
os.path.join(self.SAMPLES_DIR,
"catalogo_justicia." + suffix))
catalog = DataJson(os.path.join(self.SAMPLES_DIR,
"catalogo_justicia." + suffix))
catalog.to_json(os.path.join(self.TEMP_DIR,
"saved_catalog.json"))
catalog.to_xlsx(os.path.join(self.TEMP_DIR,
"saved_catalog.xlsx"))
catalog_json = pydatajson.core.DataJson(
os.path.join(self.TEMP_DIR,
"saved_catalog.xlsx"))
catalog_xlsx = pydatajson.core.DataJson(
os.path.join(self.TEMP_DIR,
"saved_catalog.xlsx"))
catalog_json = DataJson(os.path.join(self.TEMP_DIR,
"saved_catalog.xlsx"))
catalog_xlsx = DataJson(os.path.join(self.TEMP_DIR,
"saved_catalog.xlsx"))
self.assertEqual(catalog_json, catalog_xlsx)

# la llamada to_xlsx() genera los indices en el catalogo original
Expand Down Expand Up @@ -312,6 +311,17 @@ def test_read_failing_xlsx_catalog_raises_non_parseable_error(self):
def test_failing_suffixless_catalog_raises_non_parseable_error(self):
pydatajson.readers.read_catalog('inexistent_file')

def test_xlsx_write_missing_optional_fields_and_themes(self):
with NamedTemporaryFile(suffix='.xlsx') as tempfile:
catalog = DataJson(os.path.join(self.SAMPLES_DIR,
"minimum_data.json"))
catalog.to_xlsx(tempfile.name)
written_datajson = DataJson(tempfile.name)
written_dataset = written_datajson.datasets[0]
written_distribution = written_datajson.distributions[0]
self.assertTrue('theme' not in written_dataset)
self.assertTrue('field' not in written_distribution)


if __name__ == '__main__':
nose.run(defaultTest=__name__)

0 comments on commit e855f6a

Please sign in to comment.