Skip to content

Commit

Permalink
[DOCS] Puts 1.0 example code for Checkpoints, Actions, and Result For…
Browse files Browse the repository at this point in the history
…mat under test. (#10232)
  • Loading branch information
Rachel-Reverie authored and deborahniesz committed Aug 20, 2024
1 parent e025895 commit 798e2bd
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 205 deletions.
36 changes: 36 additions & 0 deletions docs/docusaurus/docs/components/examples_under_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,40 @@
),
]

docs_examples_trigger_actions_based_on_validation_results = [
# Create a Checkpoint
IntegrationTestFixture(
# To test, run:
# pytest --docs-tests -k "docs_example_create_a_checkpoint" tests/integration/test_script_runner.py
name="docs_example_create_a_checkpoint",
user_flow_script="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py",
data_dir="docs/docusaurus/docs/components/_testing/test_data_sets/single_test_file",
# data_context_dir="",
backend_dependencies=[],
),
# Run a Checkpoint
IntegrationTestFixture(
# To test, run:
# pytest --docs-tests -k "docs_example_run_a_checkpoint" tests/integration/test_script_runner.py
name="docs_example_run_a_checkpoint",
user_flow_script="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py",
data_dir="docs/docusaurus/docs/components/_testing/test_data_sets/single_test_file",
# data_context_dir="",
backend_dependencies=[],
),
# Choose a Result Format
IntegrationTestFixture(
# To test, run:
# pytest --docs-tests -k "docs_example_choose_result_format" tests/integration/test_script_runner.py
name="docs_example_choose_result_format",
user_flow_script="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py",
data_dir="docs/docusaurus/docs/components/_testing/test_data_sets/single_test_file",
# data_context_dir="",
backend_dependencies=[],
),
]


learn_data_quality_use_cases = [
# Schema.
IntegrationTestFixture(
Expand Down Expand Up @@ -439,4 +473,6 @@

docs_tests.extend(example_scripts_for_define_expectations)

docs_tests.extend(docs_examples_trigger_actions_based_on_validation_results)

docs_tests.extend(learn_data_quality_use_cases)
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""
This is an example script for how to choose a Result Format.
To test, run:
pytest --docs-tests -k "docs_example_choose_result_format" tests/integration/test_script_runner.py
"""


def set_up_context_for_example(context):
# Create a Batch Definition (monthly)
source_folder = "./data/folder_with_data"
data_source_name = "my_data_source"
asset_name = "my_data_asset"
batch_definition_name = "my_batch_definition"
batch_definition_regex = (
r"yellow_tripdata_sample_(?P<year>\d{4})-(?P<month>\d{2})\.csv"
)
batch_definition = (
context.data_sources.add_pandas_filesystem(
name=data_source_name, base_directory=source_folder
)
.add_csv_asset(name=asset_name)
.add_batch_definition_monthly(
name=batch_definition_name, regex=batch_definition_regex
)
)
assert batch_definition.name == batch_definition_name

# Create an Expectation Suite
expectation_suite = context.suites.add(
gx.ExpectationSuite(name="my_expectation_suite")
)
# Add some Expectations
# ColumnMap
expectation = gx.expectations.ExpectColumnValuesToBeInSet(
column="passenger_count", value_set=[1, 2, 3, 4, 5]
)
expectation_suite.add_expectation(expectation)
# ColumnAggregate
expectation = gx.expectations.ExpectColumnMaxToBeBetween(
column="passenger_count", min_value=4, max_value=5
)
expectation_suite.add_expectation(expectation)

# Create a Validation Definition
validation_definition = context.validation_definitions.add(
gx.ValidationDefinition(
data=batch_definition,
suite=expectation_suite,
name="my_validation_definition",
)
)

# Create a Checkpoint
checkpoint_name = "my_checkpoint"
validation_definitions = [validation_definition]
action_list = []
context.checkpoints.add(
gx.Checkpoint(
name=checkpoint_name,
validation_definitions=validation_definitions,
actions=action_list,
result_format={"result_format": "COMPLETE"},
)
)


# EXAMPLE SCRIPT STARTS HERE:
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - full code example">
import great_expectations as gx

context = gx.get_context()
# Hide this
set_up_context_for_example(context)

validation_name = "my_validation_definition"
validation_definition = context.validation_definitions.get(validation_name)

checkpoint_name = "my_checkpoint"
checkpoint = context.checkpoints.get(checkpoint_name)

# BOOLEAN_ONLY Result Format
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - boolean_only Result Format">
boolean_result_format_dict = {"result_format": "BOOLEAN_ONLY"}
# </snippet>
validation_definition.run(result_format=boolean_result_format_dict)
# checkpoint.run(result_format=boolean_result_format_dict)

# BASIC Result Format
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - basic Result Format">
basic_result_format_dict = {"result_format": "BASIC"}
# </snippet>
validation_definition.run(result_format=basic_result_format_dict)
# checkpoint.run(result_format=basic_result_format_dict)

# SUMMARY Result Format
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - summary Result Format">
summary_result_format_dict = {"result_format": "SUMMARY"}
# </snippet>
validation_definition.run(result_format=summary_result_format_dict)
# checkpoint.run(result_format=summary_result_format_dict)

# COMPLETE Result Format
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - complete Result Format">
complete_result_format_dict = {"result_format": "COMPLETE"}
# </snippet>
validation_definition.run(result_format=complete_result_format_dict)
# checkpoint.run(result_format=complete_result_format_dict)
# </snippet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
"""
This is an example script for how to create a Checkpoint with Actions.
To test, run:
pytest --docs-tests -k "docs_example_create_a_checkpoint" tests/integration/test_script_runner.py
"""


def set_up_context_for_example(context):
# Create a Batch Definition
batch_definition = (
context.data_sources.add_pandas_filesystem(
name="my_data_source", base_directory="./data/folder_with_data"
)
.add_csv_asset(name="my_data_asset")
.add_batch_definition_path(
name="my_batch_definition", path="yellow_tripdata_sample_2019-01.csv"
)
)

# Create an Expectation Suite
expectation_suite = context.suites.add(
gx.ExpectationSuite(name="my_expectation_suite")
)
# Add some Expectations
expectation_suite.add_expectation(
gx.expectations.ExpectColumnValuesToNotBeNull(column="pickup_datetime")
)
expectation_suite.add_expectation(
gx.expectations.ExpectColumnValuesToNotBeNull(column="passenger_count")
)

# Create a Validation Definition
context.validation_definitions.add(
gx.ValidationDefinition(
data=batch_definition,
suite=expectation_suite,
name="my_validation_definition",
)
)


# EXAMPLE SCRIPT STARTS HERE:
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - full code example">
import great_expectations as gx

context = gx.get_context()
# Hide this
set_up_context_for_example(context)

# Create a list of one or more Validation Definitions for the Checkpoint to run
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - create a Validation Definitions list">
validation_definitions = [
context.validation_definitions.get("my_validation_definition")
]
# </snippet>

# Create a list of Actions for the Checkpoint to perform
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - define an Action list">
action_list = [
# This Action sends a Slack Notification if an Expectation fails.
gx.checkpoint.SlackNotificationAction(
name="send_slack_notification_on_failed_expectations",
slack_token="${validation_notification_slack_webhook}",
slack_channel="${validation_notification_slack_channel}",
notify_on="failure",
show_failed_expectations=True,
),
# This Action updates the Data Docs static website with the Validation
# Results after the Checkpoint is run.
gx.checkpoint.UpdateDataDocsAction(
name="update_all_data_docs",
),
]
# </snippet>

# Create the Checkpoint
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - create a Checkpoint">
checkpoint_name = "my_checkpoint"
checkpoint = gx.Checkpoint(
name=checkpoint_name,
validation_definitions=validation_definitions,
actions=action_list,
result_format={"result_format": "COMPLETE"},
)
# </snippet>

# Save the Checkpoint to the Data Context
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - save the Checkpoint to the Data Context">
context.checkpoints.add(checkpoint)
# </snippet>

# Retrieve the Checkpoint later
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - retrieve a Checkpoint from the Data Context">
checkpoint_name = "my_checkpoint"
checkpoint = context.checkpoints.get(checkpoint_name)
# </snippet>
# </snippet>
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
This is an example script for how to run a Checkpoint.
To test, run:
pytest --docs-tests -k "docs_example_run_a_checkpoint" tests/integration/test_script_runner.py
"""


def set_up_context_for_example(context):
# Create a Batch Definition (monthly)
source_folder = "./data/folder_with_data"
data_source_name = "my_data_source"
asset_name = "my_data_asset"
batch_definition_name = "my_batch_definition"
batch_definition_regex = (
r"yellow_tripdata_sample_(?P<year>\d{4})-(?P<month>\d{2})\.csv"
)
batch_definition = (
context.data_sources.add_pandas_filesystem(
name=data_source_name, base_directory=source_folder
)
.add_csv_asset(name=asset_name)
.add_batch_definition_monthly(
name=batch_definition_name, regex=batch_definition_regex
)
)
assert batch_definition.name == batch_definition_name

# Create an Expectation Suite
expectation_suite = context.suites.add(
gx.ExpectationSuite(name="my_expectation_suite")
)
# Add some Expectations
expectation_suite.add_expectation(
gx.expectations.ExpectColumnValuesToNotBeNull(column="pickup_datetime")
)
expectation_suite.add_expectation(
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - example Expectation">
gx.expectations.ExpectColumnMaxToBeBetween(
column="fare",
min_value={"$PARAMETER": "expect_fare_max_to_be_above"},
max_value={"$PARAMETER": "expect_fare_max_to_be_below"},
)
# </snippet>
)

# Create a Validation Definition
validation_definition = context.validation_definitions.add(
gx.ValidationDefinition(
data=batch_definition,
suite=expectation_suite,
name="my_validation_definition",
)
)

# Create a Checkpoint
checkpoint_name = "my_checkpoint"
validation_definitions = [validation_definition]
action_list = []
context.checkpoints.add(
gx.Checkpoint(
name=checkpoint_name,
validation_definitions=validation_definitions,
actions=action_list,
result_format={"result_format": "COMPLETE"},
)
)


# EXAMPLE SCRIPT STARTS HERE:
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - full code example">
import great_expectations as gx

context = gx.get_context()
# Hide this
set_up_context_for_example(context)

checkpoint = context.checkpoints.get("my_checkpoint")

batch_parameters = {"month": "01", "year": "2019"}

# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - define Expectation Parameters">
expectation_parameters = {
"expect_fare_max_to_be_above": 5.00,
"expect_fare_max_to_be_below": 1000.00,
}
# </snippet>

# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - run a Checkpoint">
validation_results = checkpoint.run(
batch_parameters=batch_parameters, expectation_parameters=expectation_parameters
)
# </snippet>
# </snippet>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ You can check the [Validation Results reference tables](#validation-results-refe

To create a `"BASIC"` result format configuration use the following code:

```python
basic_rf_dict = {"result_format": "BASIC"}
```python name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - basic Result Format"
```
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ When the `result_format` is `"BOOLEAN_ONLY"` Validation Results do not include a

To create a `"BOOLEAN_ONLY"` result format configuration use the following code:

```python title="Python"
boolean_only_rf_dict = {"result_format": "BOOLEAN_ONLY"}
```python title="Python" name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - boolean_only Result Format"
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ You can check the [Validation Results reference tables](#validation-results-refe

To create a `"COMPLETE"` result format configuration use the following code:

```python title="Python"
complete_rf_dict = {"result_format": "COMPLETE"}
```python title="Python" name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - complete Result Format"
```
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ When the `result_format` key is set to `"SUMMARY"` the Validation Results of eac
You can check the [Validation Results reference tables](#validation-results-reference-tables) to see what information is provided in the `result` dictionary.
To create a `"SUMMARY"` result format configuration use the following code:

```python title="Python"
summary_rf_dict = {"result_format": "SUMMARY"}
```python title="Python" name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - summary Result Format"
```
Loading

0 comments on commit 798e2bd

Please sign in to comment.