Skip to content

Commit

Permalink
Add rule: public models should have an example SQL query in their des…
Browse files Browse the repository at this point in the history
…cription (#30)
  • Loading branch information
matthieucan authored May 24, 2024
1 parent 6011dac commit f68fff8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/dbt_score/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class Model:
database: The database name of the model.
schema: The schema name of the model.
raw_code: The raw code of the model.
language: The language of the model, e.g. sql.
access: The access level of the model, e.g. public.
alias: The alias of the model.
patch_path: The yml path of the model, e.g. `package://model_dir/dir/file.yml`.
tags: The list of tags attached to the model.
Expand All @@ -149,6 +151,8 @@ class Model:
database: str
schema: str
raw_code: str
language: str
access: str
alias: str | None = None
patch_path: str | None = None
tags: list[str] = field(default_factory=list)
Expand Down Expand Up @@ -200,6 +204,8 @@ def from_node(
database=node_values["database"],
schema=node_values["schema"],
raw_code=node_values["raw_code"],
language=node_values["language"],
access=node_values["access"],
alias=node_values["alias"],
patch_path=node_values["patch_path"],
tags=node_values["tags"],
Expand Down
12 changes: 11 additions & 1 deletion src/dbt_score/rules/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""All generic rules."""

from dbt_score import Model, RuleViolation, rule
from dbt_score import Model, RuleViolation, Severity, rule


@rule
Expand Down Expand Up @@ -39,3 +39,13 @@ def sql_has_reasonable_size(model: Model, max_lines: int = 200) -> RuleViolation
return RuleViolation(
message=f"SQL query too long: {count_lines} lines (> {max_lines})."
)


@rule(severity=Severity.LOW)
def public_model_has_example_sql(model: Model) -> RuleViolation | None:
"""The documentation of a public model should have an example query."""
if model.language == "sql" and model.access == "public":
if "```sql" not in model.description:
return RuleViolation(
"The model description does not include an example SQL query."
)
10 changes: 7 additions & 3 deletions tests/resources/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
"alias": "model1_alias",
"patch_path": "/path/to/model1.yml",
"tags": [],
"depends_on": {}
"depends_on": {},
"language": "sql",
"access": "protected"
},
"model.package.model2": {
"resource_type": "model",
"unique_id": "model.package.model2",
"name": "model2",
"relation_name": "database.schema.model2",
"description": "Description2.",
"description": "A great model.\nExample use:\n```sql\nselect 1;\n```",
"original_file_path": "/path/to/model2.sql",
"config": {},
"meta": {},
Expand All @@ -57,7 +59,9 @@
"alias": "model2_alias",
"patch_path": "/path/to/model2.yml",
"tags": [],
"depends_on": {}
"depends_on": {},
"language": "sql",
"access": "public"
},
"test.package.test1": {
"resource_type": "test",
Expand Down

0 comments on commit f68fff8

Please sign in to comment.