diff --git a/src/caosadvancedtools/table_json_conversion/table_generator.py b/src/caosadvancedtools/table_json_conversion/table_generator.py index c037ff304a9cd02c6dc448b02ce97079fa820239..c69c4984a1f9c4b725c8276ff2603df082c9299c 100644 --- a/src/caosadvancedtools/table_json_conversion/table_generator.py +++ b/src/caosadvancedtools/table_json_conversion/table_generator.py @@ -89,7 +89,7 @@ class TableTemplateGenerator(ABC): ------- dict The structure of the dict, lets call it ``sheets`` is as follows: - ``sheets[sheetname][colname]= (COL_TYPE, description [path])`` + ``sheets[sheetname][colname]= (COL_TYPE, description, [path])`` I.e. the top level dict contains sheet names as keys and dicts as values. The inner dict has column names as keys and tuples as values. The tuples have the column type as the first element, the description of the corresponding property as @@ -223,7 +223,7 @@ class XLSXTemplateGenerator(TableTemplateGenerator): definition dict You need to pass the dict of a single sheet to this function. """ - return max([len(path) for _, _, path in sheetdef.values()]) + return max([len(path) for _, _, path in sheetdef.values()]) @staticmethod def _get_ordered_cols(sheetdef: dict) -> list: diff --git a/unittests/table_json_conversion/test_table_template_generator.py b/unittests/table_json_conversion/test_table_template_generator.py index fc67f435eea8d5a73bc34f7f918ee8ae5435a0ed..40f9d0b1db511ee7fdcfca4c3e2c58ff0edf3630 100644 --- a/unittests/table_json_conversion/test_table_template_generator.py +++ b/unittests/table_json_conversion/test_table_template_generator.py @@ -104,7 +104,7 @@ def test_generate_sheets_from_schema(): assert "Training" in sdef tdef = sdef['Training'] assert 'name' in tdef - assert tdef['name'] == (ColumnType.SCALAR, ["Training", 'name']) + assert tdef['name'] == (ColumnType.SCALAR, "The name of the Record to be created", ["Training", 'name']) # example case with open(rfp("model_schema.json")) as sfi: @@ -115,29 +115,30 @@ def test_generate_sheets_from_schema(): foreign_keys={'Training': (['date', 'url'], {})}) assert "Training" in sdef tdef = sdef['Training'] - assert tdef['date'] == (ColumnType.SCALAR, ["Training", 'date']) - assert tdef['url'] == (ColumnType.SCALAR, ["Training", 'url']) - assert tdef['supervisor.family_name'] == (ColumnType.SCALAR, ["Training", 'supervisor', - 'family_name']) - assert tdef['supervisor.given_name'] == (ColumnType.SCALAR, ["Training", 'supervisor', - 'given_name']) - assert tdef['supervisor.Organisation.name'] == (ColumnType.SCALAR, ["Training", 'supervisor', - 'Organisation', 'name']) - assert tdef['supervisor.Organisation.Country'] == (ColumnType.SCALAR, ["Training", 'supervisor', - 'Organisation', 'Country']) - assert tdef['duration'] == (ColumnType.SCALAR, ["Training", 'duration']) - assert tdef['participants'] == (ColumnType.SCALAR, ["Training", 'participants']) - assert tdef['subjects'] == (ColumnType.LIST, ["Training", 'subjects']) - assert tdef['remote'] == (ColumnType.SCALAR, ["Training", 'remote']) + assert tdef['date'] == (ColumnType.SCALAR, 'date', ["Training", 'date']) + assert tdef['url'] == (ColumnType.SCALAR, 'url', ["Training", 'url']) + assert tdef['supervisor.family_name'] == (ColumnType.SCALAR, None, ["Training", 'supervisor', + 'family_name']) + assert tdef['supervisor.given_name'] == (ColumnType.SCALAR, None, ["Training", 'supervisor', + 'given_name']) + assert tdef['supervisor.Organisation.name'] == (ColumnType.SCALAR, 'The name of the Record to be created', ["Training", 'supervisor', + 'Organisation', 'name']) + assert tdef['supervisor.Organisation.Country'] == (ColumnType.SCALAR, None, ["Training", 'supervisor', + 'Organisation', 'Country']) + assert tdef['duration'] == (ColumnType.SCALAR, None, ["Training", 'duration']) + assert tdef['participants'] == (ColumnType.SCALAR, None, ["Training", 'participants']) + assert tdef['subjects'] == (ColumnType.LIST, None, ["Training", 'subjects']) + assert tdef['remote'] == (ColumnType.SCALAR, None, ["Training", 'remote']) cdef = sdef['Training.coach'] - assert cdef['coach.family_name'] == (ColumnType.SCALAR, ["Training", 'coach', 'family_name']) - assert cdef['coach.given_name'] == (ColumnType.SCALAR, ["Training", 'coach', 'given_name']) - assert cdef['coach.Organisation.name'] == (ColumnType.SCALAR, ["Training", 'coach', - 'Organisation', 'name']) - assert cdef['coach.Organisation.Country'] == (ColumnType.SCALAR, ["Training", 'coach', - 'Organisation', 'Country']) - assert cdef['date'] == (ColumnType.FOREIGN, ["Training", 'date']) - assert cdef['url'] == (ColumnType.FOREIGN, ["Training", 'url']) + assert cdef['coach.family_name'] == (ColumnType.SCALAR, None, ["Training", 'coach', 'family_name']) + assert cdef['coach.given_name'] == (ColumnType.SCALAR, None, ["Training", 'coach', 'given_name']) + assert cdef['coach.Organisation.name'] == (ColumnType.SCALAR, 'The name of the Record to be created', ["Training", 'coach', + 'Organisation', 'name']) + assert cdef['coach.Organisation.Country'] == (ColumnType.SCALAR, None, ["Training", 'coach', + 'Organisation', 'Country']) + print(cdef) + assert cdef['Training.date'] == (ColumnType.FOREIGN, "see sheet 'Training'", ["Training", 'date']) + assert cdef['Training.url'] == (ColumnType.FOREIGN, "see sheet 'Training'", ["Training", 'url']) def test_get_foreign_keys(): @@ -149,7 +150,7 @@ def test_get_foreign_keys(): assert ['a'] == generator._get_foreign_keys(fkd, ['Training']) fkd = {"Training": {'hallo'}} - with pytest.raises(ValueError, match=r"A foreign key definition is missing for path:\n\[\]\n{'Training': \{'hallo'\}\}"): + with pytest.raises(ValueError, match=r"A foreign key definition is missing for path:\n\['Training'\]\n{'Training': \{'hallo'\}\}"): generator._get_foreign_keys(fkd, ['Training']) fkd = {"Training": (['a'], {'b': ['c']})} @@ -160,8 +161,8 @@ def test_get_foreign_keys(): def test_get_max_path_length(): - assert 4 == XLSXTemplateGenerator._get_max_path_length({'a': (1, [1, 2, 3]), - 'b': (2, [1, 2, 3, 4])}) + assert 4 == XLSXTemplateGenerator._get_max_path_length({'a': (1, 'desc', [1, 2, 3]), + 'b': (2, 'desc', [1, 2, 3, 4])}) def test_template_generator(): @@ -189,16 +190,13 @@ def test_template_generator(): assert ws.column_dimensions['A'].hidden is True - - - - rp = '/home/henrik/CaosDB/management/external/dimr/eingabemaske/django/laforms/settings/DevelSchema.json' - with open(rp) as sfi: - schema = json.load(sfi) - generator.generate(schema=schema, - foreign_keys={}, - filepath=path) - os.system(f'libreoffice {path}') +# rp = '/home/henrik/CaosDB/management/external/dimr/eingabemaske/django/laforms/settings/DevelSchema.json' +# with open(rp) as sfi: +# schema = json.load(sfi) +# generator.generate(schema=schema, +# foreign_keys={}, +# filepath=path) +# os.system(f'libreoffice {path}') # TODO test colisions of sheet or colnames # TODO test escaping of values