Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAINTENANCE] Additional tests for ValidationDefinition and Checkpoint freshness #10381

Merged
merged 6 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 9 additions & 2 deletions great_expectations/checkpoint/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@
CheckpointNotFreshError,
CheckpointRunWithoutValidationDefinitionError,
)
from great_expectations.exceptions.exceptions import CheckpointNotFoundError, StoreBackendError
from great_expectations.exceptions.exceptions import (
CheckpointNotFoundError,
InvalidKeyError,
StoreBackendError,
)
from great_expectations.render.renderer.renderer import Renderer

if TYPE_CHECKING:
Expand Down Expand Up @@ -286,7 +290,10 @@ def is_fresh(self) -> CheckpointFreshnessDiagnostics:

try:
checkpoint = store.get(key=key)
except StoreBackendError:
except (
StoreBackendError, # Generic error from stores
InvalidKeyError, # Ephemeral context error
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should follow up by patching the in-memory stores to return a consistent type

):
return CheckpointFreshnessDiagnostics(errors=[CheckpointNotFoundError(name=self.name)])

return CheckpointFreshnessDiagnostics(
Expand Down
6 changes: 5 additions & 1 deletion great_expectations/core/validation_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
ValidationDefinitionNotFreshError,
)
from great_expectations.exceptions.exceptions import (
InvalidKeyError,
StoreBackendError,
ValidationDefinitionNotFoundError,
)
Expand Down Expand Up @@ -144,7 +145,10 @@ def is_fresh(self) -> ValidationDefinitionFreshnessDiagnostics:

try:
validation_definition = store.get(key=key)
except StoreBackendError:
except (
StoreBackendError, # Generic error from stores
InvalidKeyError, # Ephemeral context error
):
return ValidationDefinitionFreshnessDiagnostics(
errors=[ValidationDefinitionNotFoundError(name=self.name)]
)
Expand Down
72 changes: 72 additions & 0 deletions tests/checkpoint/test_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
ResourceFreshnessError,
ValidationDefinitionNotAddedError,
)
from great_expectations.exceptions.exceptions import (
CheckpointNotFoundError,
ValidationDefinitionNotFoundError,
)
from great_expectations.exceptions.resource_freshness import ResourceFreshnessAggregateError
from great_expectations.expectations.expectation_configuration import ExpectationConfiguration
from tests.test_utils import working_directory
Expand Down Expand Up @@ -1187,3 +1191,71 @@ def test_is_fresh(
diagnostics.raise_for_error()
except ResourceFreshnessAggregateError as e:
assert [type(err) for err in e.errors] == error_list


def test_is_fresh_raises_error_when_checkpoint_not_found(in_memory_runtime_context):
context = in_memory_runtime_context

batch_definition = (
context.data_sources.add_pandas(name="my_pandas_ds")
.add_csv_asset(name="my_csv_asset", filepath_or_buffer="data.csv")
.add_batch_definition(name="my_batch_def")
)

suite = context.suites.add(ExpectationSuite(name="my_suite"))

validation_definition = context.validation_definitions.add(
ValidationDefinition(
name="my_validation_definition",
suite=suite,
data=batch_definition,
)
)

checkpoint = context.checkpoints.add(
Checkpoint(
name="my_checkpoint",
validation_definitions=[validation_definition],
)
)

context.checkpoints.delete(checkpoint.name)

diagnostics = checkpoint.is_fresh()
assert diagnostics.success is False
assert len(diagnostics.errors) == 1
assert isinstance(diagnostics.errors[0], CheckpointNotFoundError)


def test_is_fresh_raises_error_when_child_deps_not_found(in_memory_runtime_context):
context = in_memory_runtime_context

batch_definition = (
context.data_sources.add_pandas(name="my_pandas_ds")
.add_csv_asset(name="my_csv_asset", filepath_or_buffer="data.csv")
.add_batch_definition(name="my_batch_def")
)

suite = context.suites.add(ExpectationSuite(name="my_suite"))

validation_definition = context.validation_definitions.add(
ValidationDefinition(
name="my_validation_definition",
suite=suite,
data=batch_definition,
)
)

checkpoint = context.checkpoints.add(
Checkpoint(
name="my_checkpoint",
validation_definitions=[validation_definition],
)
)

context.validation_definitions.delete(validation_definition.name)

diagnostics = checkpoint.is_fresh()
assert diagnostics.success is False
assert len(diagnostics.errors) == 1
assert isinstance(diagnostics.errors[0], ValidationDefinitionNotFoundError)
55 changes: 55 additions & 0 deletions tests/core/test_validation_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
ValidationDefinitionNotAddedError,
ValidationDefinitionRelatedResourcesFreshnessError,
)
from great_expectations.exceptions.exceptions import (
BatchDefinitionNotFoundError,
ExpectationSuiteNotFoundError,
ValidationDefinitionNotFoundError,
)
from great_expectations.exceptions.resource_freshness import ResourceFreshnessAggregateError
from great_expectations.execution_engine.execution_engine import ExecutionEngine
from great_expectations.expectations.expectation_configuration import (
Expand Down Expand Up @@ -886,3 +891,53 @@ def test_is_fresh(
diagnostics.raise_for_error()
except ResourceFreshnessAggregateError as e:
assert [type(err) for err in e.errors] == error_list


def test_is_fresh_raises_error_when_validation_definition_not_found(in_memory_runtime_context):
context = in_memory_runtime_context

batch_definition = (
context.data_sources.add_pandas(name="my_pandas_ds")
.add_csv_asset(name="my_csv_asset", filepath_or_buffer="data.csv")
.add_batch_definition(name="my_batch_def")
)
suite = context.suites.add(ExpectationSuite(name="my_suite"))
validation_definition = context.validation_definitions.add(
ValidationDefinition(
name="my_validation_definition",
suite=suite,
data=batch_definition,
)
)

context.validation_definitions.delete(validation_definition.name)

diagnostics = validation_definition.is_fresh()
assert diagnostics.success is False
assert len(diagnostics.errors) == 1
assert isinstance(diagnostics.errors[0], ValidationDefinitionNotFoundError)


def test_is_fresh_raises_error_when_child_deps_not_found(in_memory_runtime_context):
context = in_memory_runtime_context

datasource = context.data_sources.add_pandas(name="my_pandas_ds")
asset = datasource.add_csv_asset(name="my_csv_asset", filepath_or_buffer="data.csv")
batch_definition = asset.add_batch_definition(name="my_batch_def")
suite = context.suites.add(ExpectationSuite(name="my_suite"))
validation_definition = context.validation_definitions.add(
ValidationDefinition(
name="my_validation_definition",
suite=suite,
data=batch_definition,
)
)

asset.delete_batch_definition(batch_definition.name)
context.suites.delete(suite.name)

diagnostics = validation_definition.is_fresh()
assert diagnostics.success is False
assert len(diagnostics.errors) == 2
assert isinstance(diagnostics.errors[0], BatchDefinitionNotFoundError)
assert isinstance(diagnostics.errors[1], ExpectationSuiteNotFoundError)
Loading