Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
from typing import Callable, Type

import polars as pl
import pytest

from wimsey import profile
from wimsey import execution


def raise_exception_patch(exception_type: Type[Exception]) -> Callable:
"""Creates a patch that will throw an error"""

def raise_exception(*args, **kwargs) -> None:
raise exception_type

return raise_exception


def test_starter_tests_from_sampling_returns_passing_test() -> None:
df = pl.DataFrame(
{
Expand Down Expand Up @@ -77,3 +89,16 @@ def test_save_tests_from_samples_creates_expected_and_runnable_file(tmp_path) ->
)
result = execution.test(df, str(tmp_path / "cool.json"))
assert result.success


def test_validate_or_build_falls_back_to_save_starter_tests_from_sampling_if_validate_crashes(
monkeypatch,
) -> None:
monkeypatch.setattr(profile, "validate", raise_exception_patch(FileNotFoundError))
monkeypatch.setattr(
profile,
"save_starter_tests_from_sampling",
raise_exception_patch(ZeroDivisionError),
)
with pytest.raises(ZeroDivisionError):
profile.validate_or_build(None, None)
2 changes: 1 addition & 1 deletion wimsey/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.0"
__version__ = "0.6.0"
4 changes: 3 additions & 1 deletion wimsey/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def test(


def validate(
df: FrameT, contract: str | list[dict] | dict, storage_options: dict | None = None
df: FrameT,
contract: str | list[dict] | dict,
storage_options: dict | None = None,
) -> FrameT:
"""
Carry out tests on dataframe, returning original dataframe if tests are
Expand Down
31 changes: 31 additions & 0 deletions wimsey/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from narwhals.stable.v1.typing import FrameT

from wimsey.dataframe import profile_from_sampling, profile_from_samples
from wimsey.execution import validate


class _StarterTestStatus(Enum):
Expand Down Expand Up @@ -222,3 +223,33 @@ def _type_starter_tests(
test |= {"be_one_of": list(types)}
tests.append(test)
return tests


def validate_or_build(
df: FrameT,
contract: str,
samples: int = 100,
n: int | None = None,
fraction: int | None = None,
margin: float = 1,
storage_options: dict | None = None,
) -> FrameT:
"""
Will attempt to validate based on a given contract, but if that contract does not exist yet
will generate one from sampling the dataset.

Will fall back to starter_tests_from_sampling (a list samples is not possible with
only one dataframe), see *starter_tests_from_sampling* and *save_starter_tests_from_sampling*
for more details on use of keyword arguments aside from df, contract and storage_options.
"""
try:
validate(df, contract=contract, storage_options=storage_options)
except FileNotFoundError:
save_starter_tests_from_sampling(
path=contract,
df=df,
samples=samples,
n=n,
fraction=fraction,
margin=margin,
)