Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint the current dbt project only #53

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to

## [Unreleased]

- Lint the current dbt project only, not including the imported models.

## [0.2.0] - 2023-06-14

- Support Python 3.10.
Expand Down
2 changes: 1 addition & 1 deletion src/dbt_score/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def cli() -> None:
@click.option(
"--namespace",
"-n",
help="Namespace.",
help="Namespace to look for rules.",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small 'while there'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not for this PR, but) I think we can make it clearer or more user-friendly. What about renaming it to --rules-directory and describe it as "The directory that stores your custom rules."

default=None,
multiple=True,
)
Expand Down
7 changes: 6 additions & 1 deletion src/dbt_score/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,12 @@ def __init__(self, file_path: Path, select: Iterable[str] | None = None):
select: An optional dbt selection.
"""
self.raw_manifest = json.loads(file_path.read_text(encoding="utf-8"))
self.raw_nodes = self.raw_manifest.get("nodes", {})
self.project_name = self.raw_manifest["metadata"]["project_name"]
self.raw_nodes = {
node_id: node_values
for node_id, node_values in self.raw_manifest.get("nodes", {}).items()
if node_values["package_name"] == self.project_name
}
self.models: list[Model] = []
self.tests: dict[str, list[dict[str, Any]]] = defaultdict(list)

Expand Down
41 changes: 39 additions & 2 deletions tests/resources/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"metadata": {
"project_name": "package"
},
"nodes": {
"analysis.package.analysis1": {
"resource_type": "analysis"
"resource_type": "analysis",
"package_name": "package"
},
"model.package.model1": {
"resource_type": "model",
Expand Down Expand Up @@ -63,6 +67,36 @@
"language": "sql",
"access": "public"
},
"model.package2.model1": {
"resource_type": "model",
"unique_id": "model.package2.model1",
"name": "model1",
"relation_name": "database.schema.model1",
"description": "A great model.\nExample use:\n```sql\nselect 1;\n```",
"original_file_path": "/path/to/model1.sql",
"config": {},
"meta": {},
"columns": {
"a": {
"name": "column_a",
"description": "Column A.",
"data_type": "string",
"meta": {},
"constraints": [],
"tags": []
}
},
"package_name": "package2",
"database": "db",
"schema": "schema",
"raw_code": "SELECT x FROM y",
"alias": "model1_alias",
"patch_path": "/path/to/model1.yml",
"tags": [],
"depends_on": {},
"language": "sql",
"access": "public"
},
"test.package.test1": {
"resource_type": "test",
"attached_node": "model.package.model1",
Expand All @@ -73,6 +107,7 @@
"column_name": "a"
}
},
"package_name": "package",
"tags": []
},
"test.package.test2": {
Expand All @@ -83,10 +118,12 @@
"name": "type",
"kwargs": {}
},
"package_name": "package",
"tags": []
},
"test.package.test3": {
"resource_type": "test"
"resource_type": "test",
"package_name": "package"
}
}
}
3 changes: 3 additions & 0 deletions tests/resources/manifest_empty.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"metadata": {
"project_name": "package"
},
"nodes": {}
}
1 change: 1 addition & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_manifest_load(mock_read_text, raw_manifest):
node
for node in raw_manifest["nodes"].values()
if node["resource_type"] == "model"
and node["package_name"] == raw_manifest["metadata"]["project_name"]
]
)
assert loader.models[0].tests[0].name == "test2"
Expand Down