diff --git a/src/caosadvancedtools/table_json_conversion/table_generator.py b/src/caosadvancedtools/table_json_conversion/table_generator.py index 39aa62508a386e59091323086fd088ad28f3bddb..1f4fb813d48c57eecf6da3279d1f5a9fbd0b8832 100644 --- a/src/caosadvancedtools/table_json_conversion/table_generator.py +++ b/src/caosadvancedtools/table_json_conversion/table_generator.py @@ -71,6 +71,11 @@ class TableTemplateGenerator(ABC): Example: ``{"Training": {"__this__": ["date"], "Person": ["name", "email"]}}`` Here, ``date`` is the sole foreign key for Training. + + | It probably is worth extending the first example, with a case where a "Training" shall + be distiguished by the "name" and "email" of a "Person" which it references. The + foreign keys for this example are specified like this: + | ``{"Training": {"__this__": [["Person", "name"], ["Person", "email"]]}}`` """ def _generate_sheets_from_schema(self, schema: dict, foreign_keys: Optional[dict] = None @@ -121,7 +126,13 @@ class TableTemplateGenerator(ABC): return sheets def _get_foreign_keys(self, keys: dict, path: list) -> list: - """Return the foreign keys that are needed at the location to which path points.""" + """Return the foreign keys that are needed at the location to which path points. + +Returns +------- +foreign_keys: list + Contains strings or lists of strings. +""" msg = f"A foreign key definition is missing for path:\n{path}\nKeys are:\n{keys}" while path: if keys is None or path[0] not in keys: @@ -198,14 +209,17 @@ class TableTemplateGenerator(ABC): "string was given:\n" f"{array_path} -> {foreigns}") for foreign in foreigns: - internal_key = p2s(array_path + [foreign]) + if isinstance(foreign, str): + foreign = [foreign] + internal_key = p2s(array_path + foreign) if internal_key in sheets[sheetname]: - raise ValueError("The schema would lead to two columns with the same " - "name, which is forbidden:\n" + raise ValueError("The schema would lead to two columns with the " + "same name, which is forbidden:\n" f"{foreign} -> {internal_key}") ref_sheet = p2s(array_path) sheets[sheetname][internal_key] = ( - ColumnType.FOREIGN, f"see sheet '{ref_sheet}'", array_path + [foreign]) + ColumnType.FOREIGN, f"see sheet '{ref_sheet}'", + array_path + foreign) # Columns are added to the new sheet, thus we do not return any columns for the # current sheet. return {} @@ -301,7 +315,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 07409213ee373d01edcb13395ad6d7856b8185fa..179f121db5c5c49fbda3a38da5d7c72b93eda89a 100644 --- a/unittests/table_json_conversion/test_table_template_generator.py +++ b/unittests/table_json_conversion/test_table_template_generator.py @@ -263,7 +263,7 @@ def test_model_with_indirect_reference(): _compare_generated_to_known_good( schema_file=rfp("data/indirect_schema.json"), known_good=rfp("data/indirect_template.xlsx"), - foreign_keys={"Wrapper": ["Training.name", "Training.url"]}, + foreign_keys={"Wrapper": {"__this__": [["Training", "name"], ["Training", "url"]]}}, outfile=None)