Skip to content

Commit

Permalink
fix: handling metadata catalog null & self rel (#90)
Browse files Browse the repository at this point in the history
* fix: handling metadata catalog null & self rel

* chore: code format
  • Loading branch information
datnguye committed Feb 18, 2024
1 parent 7c99e66 commit 982f889
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
23 changes: 14 additions & 9 deletions dbterd/adapters/algos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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 = (
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/adapters/algos/test_test_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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"},
],
}
}
Expand Down

0 comments on commit 982f889

Please sign in to comment.