Skip to content

Commit

Permalink
Feature/print result table (#45)
Browse files Browse the repository at this point in the history
* Print result table

* Reformat

---------

Co-authored-by: jochen <[email protected]>
  • Loading branch information
jochenchrist and jochenchrist authored Feb 21, 2024
1 parent a5359ad commit 0569817
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
51 changes: 50 additions & 1 deletion datacontract/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import typer
from click import Context
from rich import box
from rich import print
from rich.table import Table
from typer.core import TyperGroup
from typing_extensions import Annotated

Expand Down Expand Up @@ -91,12 +94,16 @@ def test(
help="Run the schema and quality tests on the example data within the data contract.")] = None,
publish: Annotated[str, typer.Option(
help="The url to publish the results after the test")] = None,
logs: Annotated[bool, typer.Option(
help="Print logs")] = False,
):
"""
Run schema and quality tests on configured servers.
"""
print(f"Testing {location}")
run = DataContract(data_contract_file=location, publish_url=publish, examples=examples).test()
if logs:
_print_logs(run)
_handle_result(run)


Expand All @@ -121,8 +128,9 @@ def export(


def _handle_result(run):
_print_table(run)
if run.result == "passed":
print(f"🟢 data contract is valid. Run {len(run.checks)} checks.")
print(f"🟢 data contract is valid. Run {len(run.checks)} checks. Took {(run.timestampEnd - run.timestampStart).total_seconds()} seconds.")
else:
print("🔴 data contract is invalid, found the following errors:")
i = 1
Expand All @@ -132,5 +140,46 @@ def _handle_result(run):
raise typer.Exit(code=1)


def _print_table(run):
table = Table(box=box.ROUNDED)
table.add_column("Result", no_wrap=True)
table.add_column("Check", max_width=100)
table.add_column("Field", max_width=32)
table.add_column("Details", max_width=50)
for check in run.checks:
table.add_row(with_markup(check.result), check.name, to_field(run, check), check.details)
print(table)


def to_field(run, check):
models = [c.model for c in run.checks]
if len(set(models)) > 1:
if check.field is None:
return check.model
return check.model + "." + check.field
else:
return check.field




def _print_logs(run):
print("\nLogs:")
for log in run.logs:
print(log.timestamp.strftime("%y-%m-%d %H:%M:%S"), log.level.ljust(5), log.message)


def with_markup(result):
if result == "passed":
return "[green]passed[/green]"
if result == "warning":
return "[yellow]warning[/yellow]"
if result == "failed":
return "[red]failed[/red]"
if result == "error":
return "[red]error[/red]"
return result


if __name__ == "__main__":
app()
12 changes: 9 additions & 3 deletions datacontract/engines/soda/check_soda_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,15 @@ def check_soda_execute(run: Run, data_contract: DataContractSpecification, serve

scan_results = scan.get_scan_results()
for c in scan_results.get("checks"):
check = Check(type="schema", result="passed" if c.get("outcome") == "pass" else "failed" if c.get(
"outcome") == "fail" else c.get("outcome"), reason=', '.join(c.get("outcomeReasons")), name=c.get("name"),
model=c.get("table"), field=c.get("column"), engine="soda-core", )
check = Check(
type="schema",
result="passed" if c.get("outcome") == "pass" else "failed" if c.get("outcome") == "fail" else c.get("outcome"),
reason=', '.join(c.get("outcomeReasons")),
name=c.get("name"),
model=c.get("table"),
field=c.get("column"),
engine="soda-core",
)
update_reason(check, c)
run.checks.append(check)

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"pyyaml~=6.0.1",
"requests~=2.31.0",
"fastparquet==2024.2.0",
"rich~=13.7.0",
"soda-core-bigquery~=3.2.1",
"soda-core-duckdb~=3.2.1",
"soda-core-postgres~=3.2.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_lint_invalid_data_contract():

def test_lint_cli_valid():
data_contract_file = "examples/lint/valid_datacontract.yaml"
expected_output = "🟢 data contract is valid. Run 2 checks.\n"
expected_output = "🟢 data contract is valid. Run 2 checks."

result = runner.invoke(app, ["lint", data_contract_file])

Expand Down

0 comments on commit 0569817

Please sign in to comment.