diff --git a/dbterd/adapters/algos/base.py b/dbterd/adapters/algos/base.py index 2683ae8..32cfd37 100644 --- a/dbterd/adapters/algos/base.py +++ b/dbterd/adapters/algos/base.py @@ -164,14 +164,16 @@ def get_table_from_metadata(model_metadata, exposures=[], **kwargs) -> Table: ) # columns - for column in model_metadata.get("node", {}).get("catalog", {}).get("columns", []): - table.columns.append( - Column( - name=column.get("name", "").lower(), - data_type=column.get("type", "").lower(), - description=column.get("description", ""), + table_catalog = model_metadata.get("node", {}).get("catalog", {}) + if table_catalog: + for column in table_catalog.get("columns", []): + table.columns.append( + Column( + name=column.get("name", "").lower(), + data_type=column.get("type", "").lower(), + description=column.get("description", ""), + ) ) - ) if not table.columns: table.columns.append(Column()) @@ -541,10 +543,13 @@ def get_table_map_from_metadata(test_node, **kwargs): if len(test_parents) == 0: return ["", ""] # return dummies - need to be excluded manually - if len(test_parents) != 2: + if len(test_parents) == 1: + return [test_parents[0], test_parents[0]] # self FK + + if len(test_parents) > 2: logger.debug(f"Collected test parents: {test_parents}") raise click.BadParameter( - "Relationship test unexpectedly doesn't have 2 parents" + "Relationship test unexpectedly doesn't have >2 parents" ) test_metadata_to = ( diff --git a/tests/unit/adapters/algos/test_test_relationship.py b/tests/unit/adapters/algos/test_test_relationship.py index 298c8af..a45fef1 100644 --- a/tests/unit/adapters/algos/test_test_relationship.py +++ b/tests/unit/adapters/algos/test_test_relationship.py @@ -569,6 +569,30 @@ def test_get_tables_from_metadata_w_1_data( description=None, ), ), + ( + { + "node": { + "uniqueId": "model.package.name1", + "database": "db1", + "schema": "sc1", + "name": "name1", + "catalog": None, + } + }, + [], + dict(entity_name_format="resource.package.model"), + Table( + name="model.package.name1", + node_name="model.package.name1", + database="db1", + schema="sc1", + columns=[ + Column(name="unknown", data_type="unknown", description=""), + ], + raw_sql=None, + description=None, + ), + ), ], ) def test_get_table_from_metadata(self, model_metadata, exposures, kwargs, expected): @@ -748,6 +772,41 @@ def test_get_node_exposures_from_metadata(self, data, kwargs, expected): ) ], ), + ( + [ + { + "tests": { + "edges": [ + { + "node": { + "uniqueId": "test.relationship_1", + "meta": {}, + "testMetadata": { + "kwargs": { + "columnName": "coly", + "to": 'ref("x")', + "field": "colx", + } + }, + "parents": [ + {"uniqueId": "model.p.x"}, + ], + } + } + ] + } + } + ], + dict(algo="test_relationship", resource_type=["model"]), + [ + Ref( + name="test.relationship_1", + table_map=["model.p.x", "model.p.x"], + column_map=["colx", "coly"], + type="n1", + ) + ], + ), ], ) def test_get_relationships_from_metadata(self, data, kwargs, expected): @@ -776,6 +835,8 @@ def test_get_relationships_from_metadata(self, data, kwargs, expected): }, "parents": [ {"uniqueId": "model.p.x"}, + {"uniqueId": "model.p.y"}, + {"uniqueId": "model.p.z"}, ], } }