-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dbt): collect table schema metadata using the
dagster
dbt package
- Loading branch information
1 parent
805ddf2
commit be0ea82
Showing
7 changed files
with
129 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ venv/ | |
env/ | ||
**/*.duckdb | ||
**/*.duckdb.wal | ||
package-lock.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...braries/dagster-dbt/dagster_dbt_tests/dbt_projects/test_dagster_metadata/dependencies.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
packages: | ||
- local: "../../../packages/dagster" |
1 change: 1 addition & 0 deletions
1
.../libraries/dagster-dbt/dagster_dbt_tests/dbt_projects/test_dagster_metadata/manifest.json
Large diffs are not rendered by default.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
...on_modules/libraries/dagster-dbt/dagster_dbt_tests/packages/test_table_schema_metadata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
from typing import Iterator | ||
|
||
import pytest | ||
from dagster import AssetKey, AssetMaterialization, TableColumn, TableSchema | ||
from dagster_dbt.core.resources_v2 import DbtCliResource | ||
from pytest_mock import MockerFixture | ||
|
||
test_no_dagster_metadata_dbt_project_dir = ( | ||
Path(__file__).joinpath("..", "..", "dbt_projects", "test_dagster_meta_config").resolve() | ||
) | ||
test_no_dagster_metadata_manifest = json.loads( | ||
test_no_dagster_metadata_dbt_project_dir.joinpath("manifest.json").read_bytes() | ||
) | ||
|
||
test_dagster_metadata_dbt_project_dir = ( | ||
Path(__file__).joinpath("..", "..", "dbt_projects", "test_dagster_metadata").resolve() | ||
) | ||
test_dagster_metadata_manifest = json.loads( | ||
test_dagster_metadata_dbt_project_dir.joinpath("manifest.json").read_bytes() | ||
) | ||
|
||
|
||
@pytest.fixture(name="dbt") | ||
def dbt_fixture() -> Iterator[DbtCliResource]: | ||
dbt = DbtCliResource(project_dir=os.fspath(test_dagster_metadata_dbt_project_dir)) | ||
|
||
dbt.cli(["deps"]).wait() | ||
|
||
yield dbt | ||
|
||
|
||
def test_no_table_schema_metadata(mocker: MockerFixture) -> None: | ||
mock_context = mocker.MagicMock() | ||
mock_context.assets_def = None | ||
mock_context.has_assets_def = False | ||
|
||
dbt = DbtCliResource(project_dir=os.fspath(test_no_dagster_metadata_dbt_project_dir)) | ||
|
||
events = list( | ||
dbt.cli( | ||
["build"], | ||
manifest=test_no_dagster_metadata_manifest, | ||
context=mock_context, | ||
).stream() | ||
) | ||
materializations_by_asset_key = { | ||
dagster_event.asset_key: dagster_event | ||
for dagster_event in events | ||
if isinstance(dagster_event, AssetMaterialization) | ||
} | ||
customers_materialization = materializations_by_asset_key[AssetKey(["customers"])] | ||
|
||
assert "table_schema" not in customers_materialization.metadata | ||
|
||
|
||
def test_table_schema_metadata(mocker: MockerFixture, dbt: DbtCliResource) -> None: | ||
mock_context = mocker.MagicMock() | ||
mock_context.assets_def = None | ||
mock_context.has_assets_def = False | ||
|
||
events = list( | ||
dbt.cli( | ||
["build"], | ||
manifest=test_dagster_metadata_manifest, | ||
context=mock_context, | ||
).stream() | ||
) | ||
materializations_by_asset_key = { | ||
dagster_event.asset_key: dagster_event | ||
for dagster_event in events | ||
if isinstance(dagster_event, AssetMaterialization) | ||
} | ||
customers_materialization = materializations_by_asset_key[AssetKey(["customers"])] | ||
|
||
assert ( | ||
TableSchema( | ||
columns=[ | ||
TableColumn("customer_id", type="INTEGER"), | ||
TableColumn("first_name", type="character varying(256)"), | ||
TableColumn("last_name", type="character varying(256)"), | ||
TableColumn("first_order", type="DATE"), | ||
TableColumn("most_recent_order", type="DATE"), | ||
TableColumn("number_of_orders", type="BIGINT"), | ||
TableColumn("customer_lifetime_value", type="DOUBLE"), | ||
] | ||
) | ||
== customers_materialization.metadata["table_schema"].value | ||
) |