Skip to content

Commit

Permalink
Use rule.source() instead of reconstructing name
Browse files Browse the repository at this point in the history
  • Loading branch information
jochemvandooren committed May 14, 2024
1 parent 923d020 commit 4af4971
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/dbt_score/rule_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def _load(self, namespace_name: str) -> None:
for obj_name in dir(module):
obj = module.__dict__[obj_name]
if type(obj) is type and issubclass(obj, Rule) and obj is not Rule:
self._add_rule(f"{module_name}.{obj_name}", obj)
self._add_rule(obj)

def _add_rule(self, name: str, rule: Type[Rule]) -> None:
def _add_rule(self, rule: Type[Rule]) -> None:
"""Add a rule."""
if name in self._rules:
raise DuplicatedRuleException(name)
if name not in self.config.disabled_rules:
self._rules[name] = rule
if rule.source() in self._rules:
raise DuplicatedRuleException(rule.source())
if rule.source() not in self.config.disabled_rules:
self._rules[rule.source()] = rule

def load_all(self) -> None:
"""Load all rules, core and third-party."""
Expand Down
4 changes: 2 additions & 2 deletions tests/resources/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ rule_namespaces = ["namespace_foo"]
disabled_rules = ["foo", "bar"]


[tool.dbt-score.rules.foobar]
[tool.dbt-score.rules."foo.bar"]
severity=4

[tool.dbt-score.rules.rule_with_config]
[tool.dbt-score.rules."tests.conftest.rule_with_config"]
model_name="model2"

[tool.dbt-score.rules."tests.rules.example.rule_test_example"]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_load_valid_toml_file(valid_config_path):
config._load_toml_file(str(valid_config_path))
assert config.rule_namespaces == ["namespace_foo"]
assert config.disabled_rules == ["foo", "bar"]
assert config.rules_config["foobar"].severity == Severity.CRITICAL
assert config.rules_config["foo.bar"].severity == Severity.CRITICAL
assert (
config.rules_config["tests.rules.example.rule_test_example"].severity
== Severity.CRITICAL
Expand Down
18 changes: 9 additions & 9 deletions tests/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ def test_evaluation_low_medium_high(
mock_scorer = Mock()

rule_registry = RuleRegistry(default_config)
rule_registry._add_rule("rule_severity_low", rule_severity_low)
rule_registry._add_rule("rule_severity_medium", rule_severity_medium)
rule_registry._add_rule("rule_severity_high", rule_severity_high)
rule_registry._add_rule("rule_error", rule_error)
rule_registry._add_rule(rule_severity_low)
rule_registry._add_rule(rule_severity_medium)
rule_registry._add_rule(rule_severity_high)
rule_registry._add_rule(rule_error)
rule_registry.init_rules()

evaluation = Evaluation(
Expand Down Expand Up @@ -64,8 +64,8 @@ def test_evaluation_critical(
manifest_loader = ManifestLoader(manifest_path)

rule_registry = RuleRegistry(default_config)
rule_registry._add_rule("rule_severity_low", rule_severity_low)
rule_registry._add_rule("rule_severity_critical", rule_severity_critical)
rule_registry._add_rule(rule_severity_low)
rule_registry._add_rule(rule_severity_critical)
rule_registry.init_rules()

evaluation = Evaluation(
Expand Down Expand Up @@ -104,7 +104,7 @@ def test_evaluation_no_model(manifest_empty_path, rule_severity_low, default_con
manifest_loader = ManifestLoader(manifest_empty_path)

rule_registry = RuleRegistry(default_config)
rule_registry._add_rule("rule_severity_low", rule_severity_low)
rule_registry._add_rule(rule_severity_low)

evaluation = Evaluation(
rule_registry=rule_registry,
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_evaluation_rule_with_config(
config._load_toml_file(str(valid_config_path))

rule_registry = RuleRegistry(config)
rule_registry._add_rule("rule_with_config", rule_with_config)
rule_registry._add_rule(rule_with_config)
rule_registry.init_rules()

evaluation = Evaluation(
Expand All @@ -161,7 +161,7 @@ def test_evaluation_rule_with_config(

assert (
rule_with_config.default_config
!= rule_registry.rules["rule_with_config"].config
!= rule_registry.rules["tests.conftest.rule_with_config"].config
)
assert evaluation.results[model1][rule_with_config] is not None
assert evaluation.results[model2][rule_with_config] is None

0 comments on commit 4af4971

Please sign in to comment.