Skip to content

Commit

Permalink
Auto-round scores down
Browse files Browse the repository at this point in the history
  • Loading branch information
japborst committed Aug 22, 2024
1 parent 6e642ee commit bbdfb94
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
11 changes: 4 additions & 7 deletions src/dbt_score/formatters/human_readable_formatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Human readable formatter."""


from typing import Any

from dbt_score.evaluation import ModelResultsType
Expand Down Expand Up @@ -34,9 +33,7 @@ def model_evaluated(
"""Callback when a model has been evaluated."""
if score.value < self._config.fail_any_model_under:
self._failed_models.append((model, score))
print(
f"{score.badge} {self.bold(model.name)} (score: {round(score.value, 1)!s})"
)
print(f"{score.badge} {self.bold(model.name)} (score: {score.value!s})")
for rule, result in results.items():
if result is None:
print(f"{self.indent}{self.label_ok} {rule.source()}")
Expand All @@ -51,16 +48,16 @@ def model_evaluated(

def project_evaluated(self, score: Score) -> None:
"""Callback when a project has been evaluated."""
print(f"Project score: {self.bold(str(round(score.value, 1)))} {score.badge}")
print(f"Project score: {self.bold(str(score.value))} {score.badge}")

if len(self._failed_models) > 0:
print()
print(
f"Error: model score too low, fail_any_model_under = "
f"{self._config.fail_any_model_under}"
)
for model, score in self._failed_models:
print(f"Model {model.name} scored {round(score.value, 1)}")
for model, model_score in self._failed_models:
print(f"Model {model.name} scored {model_score.value}")

elif score.value < self._config.fail_project_under:
print()
Expand Down
5 changes: 5 additions & 0 deletions src/dbt_score/scoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import math
import typing
from dataclasses import dataclass

Expand All @@ -19,6 +20,10 @@ class Score:
value: float
badge: str

def __post_init__(self) -> None:
"""Auto-round score down to 1 decimal place."""
self.value = math.floor(self.value * 10) / 10


class Scorer:
"""Logic for computing scores."""
Expand Down
11 changes: 5 additions & 6 deletions tests/test_scoring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Unit tests for the scoring module."""


from dbt_score.rule import RuleViolation
from dbt_score.scoring import Score, Scorer

Expand All @@ -18,7 +17,7 @@ def test_scorer_model_severity_low(default_config, rule_severity_low):
assert scorer.score_model({rule_severity_low: Exception()}).value == 10.0
assert (
round(scorer.score_model({rule_severity_low: RuleViolation("error")}).value, 2)
== 6.67
== 6.6
)


Expand All @@ -31,7 +30,7 @@ def test_scorer_model_severity_medium(default_config, rule_severity_medium):
round(
scorer.score_model({rule_severity_medium: RuleViolation("error")}).value, 2
)
== 3.33
== 3.3
)


Expand Down Expand Up @@ -83,7 +82,7 @@ def test_scorer_model_multiple_rules(
).value,
2,
)
== 6.67
== 6.6
)

assert (
Expand All @@ -97,7 +96,7 @@ def test_scorer_model_multiple_rules(
).value,
2,
)
== 7.78
== 7.7
)

assert (
Expand All @@ -111,7 +110,7 @@ def test_scorer_model_multiple_rules(
).value,
2,
)
== 8.89
== 8.8
)


Expand Down

0 comments on commit bbdfb94

Please sign in to comment.