Skip to content

Commit

Permalink
test importing without introducing imported section
Browse files Browse the repository at this point in the history
  • Loading branch information
npeshkov committed Dec 16, 2024
1 parent 9935c21 commit a352233
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/dbt_score/rule_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _load(self, namespace_name: str) -> None:
module = importlib.import_module(module_name)
for obj_name in dir(module):
obj = module.__dict__[obj_name]
# skip adding objects imported from other modules
# Skip adding objects imported from other modules
if type(obj) is type and module.__name__ != obj.__module__:
continue
if type(obj) is type and issubclass(obj, Rule) and obj is not Rule:
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ severity=4
[tool.dbt-score.rules."tests.conftest.rule_with_config"]
model_name="model2"

[tool.dbt-score.rules."tests.rules.example.rule_test_example"]
[tool.dbt-score.rules."tests.rules.rules.rule_test_example"]
severity=4
rule_filter_names=["tests.rules.example.skip_model1"]
rule_filter_names=["tests.rules.rule_filters.skip_model1"]
14 changes: 0 additions & 14 deletions tests/rules/example.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/rules/imported/__init__.py

This file was deleted.

6 changes: 0 additions & 6 deletions tests/rules/imported/imported.py

This file was deleted.

14 changes: 0 additions & 14 deletions tests/rules/imported/original.py

This file was deleted.

15 changes: 15 additions & 0 deletions tests/rules/rule_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Example rule filters."""

from dbt_score import Model, rule_filter


@rule_filter
def skip_model1(model: Model) -> bool:
"""An example filter."""
return model.name != "model1"


@rule_filter
def skip_schemaX(model: Model) -> bool:
"""An example filter."""
return model.schema != "X"
10 changes: 10 additions & 0 deletions tests/rules/rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Example rules."""

from dbt_score import Model, RuleViolation, rule

from tests.rules.rule_filters import skip_schemaX


@rule(rule_filters={skip_schemaX()})
def rule_test_example(model: Model) -> RuleViolation | None:
"""An example rule."""
6 changes: 3 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_load_valid_toml_file(valid_config_path):
assert config.disabled_rules == ["foo.foo", "tests.bar"]
assert config.rules_config["foo.bar"].severity == Severity.CRITICAL
assert (
config.rules_config["tests.rules.example.rule_test_example"].severity
config.rules_config["tests.rules.rules.rule_test_example"].severity
== Severity.CRITICAL
)
assert config.badge_config.third.threshold == 6.5
Expand All @@ -28,8 +28,8 @@ def test_load_valid_toml_file(valid_config_path):
assert config.fail_project_under == 7.5
assert config.fail_any_item_under == 6.9
assert config.rules_config[
"tests.rules.example.rule_test_example"
].rule_filter_names == ["tests.rules.example.skip_model1"]
"tests.rules.rules.rule_test_example"
].rule_filter_names == ["tests.rules.rule_filters.skip_model1"]


def test_load_invalid_toml_file(caplog, invalid_config_path):
Expand Down
20 changes: 10 additions & 10 deletions tests/test_rule_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def test_rule_catalog_terminal(capsys, default_config):
stdout = capsys.readouterr().out
assert (
stdout
== """\x1B[1mtests.rules.example.rule_test_example\x1B[0m:
== """\x1B[1mtests.rules.nested.example.rule_test_nested_example\x1B[0m:
An example rule.
\x1B[1mtests.rules.nested.example.rule_test_nested_example\x1B[0m:
\x1B[1mtests.rules.rules.rule_test_example\x1B[0m:
An example rule.
"""
Expand All @@ -35,41 +35,41 @@ def test_rule_catalog_markdown(capsys, default_config):
stdout
== """# Doc for tests.rules
## `rule_test_example`
## `rule_test_nested_example`
An example rule.
??? quote "Source code"
```python
@rule()
def rule_test_example(model: Model) -> RuleViolation | None:
@rule
def rule_test_nested_example(model: Model) -> RuleViolation | None:
\"""An example rule.\"""
```
### Default configuration
```toml title="pyproject.toml"
[tool.dbt-score.rules."tests.rules.example.rule_test_example"]
[tool.dbt-score.rules."tests.rules.nested.example.rule_test_nested_example"]
severity = 2
```
## `rule_test_nested_example`
## `rule_test_example`
An example rule.
??? quote "Source code"
```python
@rule
def rule_test_nested_example(model: Model) -> RuleViolation | None:
@rule(rule_filters={skip_schemaX()})
def rule_test_example(model: Model) -> RuleViolation | None:
\"""An example rule.\"""
```
### Default configuration
```toml title="pyproject.toml"
[tool.dbt-score.rules."tests.rules.nested.example.rule_test_nested_example"]
[tool.dbt-score.rules."tests.rules.rules.rule_test_example"]
severity = 2
```
Expand Down
18 changes: 7 additions & 11 deletions tests/test_rule_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ def test_rule_registry_discovery(default_config):
r = RuleRegistry(default_config)
r._load("tests.rules")
assert sorted(r._rules.keys()) == [
"tests.rules.example.rule_test_example",
"tests.rules.imported.original.rule_to_be_imported",
"tests.rules.nested.example.rule_test_nested_example",
"tests.rules.rules.rule_test_example",
]
assert list(r._rule_filters.keys()) == [
"tests.rules.example.skip_model1",
"tests.rules.imported.original.rule_filter_to_be_imported",
"tests.rules.rule_filters.skip_model1",
"tests.rules.rule_filters.skip_schemaX",
]


Expand All @@ -29,8 +28,7 @@ def test_disabled_rule_registry_discovery():
r = RuleRegistry(config)
r._load("tests.rules")
assert sorted(r._rules.keys()) == [
"tests.rules.example.rule_test_example",
"tests.rules.imported.original.rule_to_be_imported",
"tests.rules.rules.rule_test_example",
]


Expand All @@ -40,9 +38,7 @@ def test_configured_rule_registry_discovery(valid_config_path):
config._load_toml_file(str(valid_config_path))
r = RuleRegistry(config)
r._load("tests.rules")
assert (
r.rules["tests.rules.example.rule_test_example"].severity == Severity.CRITICAL
)
assert r.rules["tests.rules.rules.rule_test_example"].severity == Severity.CRITICAL


def test_rule_registry_no_duplicates(default_config):
Expand All @@ -68,5 +64,5 @@ def test_rule_registry_rule_filters(valid_config_path, model1, model2):
r._load("tests.rules")
r._load_filters_into_rules()

assert not r.rules["tests.rules.example.rule_test_example"].should_evaluate(model1)
assert r.rules["tests.rules.example.rule_test_example"].should_evaluate(model2)
assert not r.rules["tests.rules.rules.rule_test_example"].should_evaluate(model1)
assert r.rules["tests.rules.rules.rule_test_example"].should_evaluate(model2)

0 comments on commit a352233

Please sign in to comment.