-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOCS] Puts 1.0 documentation example code for how to run validations…
… under test (#10230)
- Loading branch information
1 parent
147f8f8
commit 0fd5529
Showing
7 changed files
with
200 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
""" | ||
This is an example script for how to create a Validation Definition. | ||
To test, run: | ||
pytest --docs-tests -k "docs_example_create_a_validation_definition" tests/integration/test_script_runner.py | ||
""" | ||
|
||
|
||
def set_up_context_for_example(context): | ||
# Create a Data Source | ||
data_source = context.data_sources.add_pandas_filesystem( | ||
name="my_data_source", base_directory="./data/folder_with_data" | ||
) | ||
# Create a Data Asset | ||
data_asset = data_source.add_csv_asset(name="my_data_asset") | ||
# Create a Batch Definition | ||
data_asset.add_batch_definition_path( | ||
name="my_batch_definition", path="yellow_tripdata_sample_2019-01.csv" | ||
) | ||
# Create an Expectation Suite | ||
suite = context.suites.add(gx.ExpectationSuite(name="my_expectation_suite")) | ||
# Add some Expectations | ||
suite.add_expectation( | ||
gx.expectations.ExpectColumnValuesToNotBeNull(column="pickup_datetime") | ||
) | ||
suite.add_expectation( | ||
gx.expectations.ExpectColumnValuesToNotBeNull(column="passenger_count") | ||
) | ||
|
||
|
||
# EXAMPLE SCRIPT STARTS HERE: | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/create_a_validation_definition.py - full code example"> | ||
import great_expectations as gx | ||
|
||
context = gx.get_context() | ||
# Hide this | ||
set_up_context_for_example(context) | ||
|
||
# Retrieve an Expectation Suite | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/create_a_validation_definition.py - retrieve an Expectation Suite"> | ||
expectation_suite_name = "my_expectation_suite" | ||
expectation_suite = context.suites.get(name=expectation_suite_name) | ||
# </snippet> | ||
|
||
# Retrieve a Batch Definition | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/create_a_validation_definition.py - retrieve a Batch Definition"> | ||
data_source_name = "my_data_source" | ||
data_asset_name = "my_data_asset" | ||
batch_definition_name = "my_batch_definition" | ||
batch_definition = ( | ||
context.get_datasource(data_source_name) | ||
.get_asset(data_asset_name) | ||
.get_batch_definition(batch_definition_name) | ||
) | ||
# </snippet> | ||
|
||
# Create a Validation Definition | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/create_a_validation_definition.py - create a Validation Definition"> | ||
definition_name = "my_validation_definition" | ||
validation_definition = gx.ValidationDefinition( | ||
data=batch_definition, suite=expectation_suite, name=definition_name | ||
) | ||
# </snippet> | ||
|
||
# Add the Validation Definition to the Data Context | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/create_a_validation_definition.py - save the Validation Definition to the Data Context"> | ||
validation_definition = context.validation_definitions.add(validation_definition) | ||
# </snippet> | ||
# </snippet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
This is an example script for how to run a Validation Definition. | ||
To test, run: | ||
""" | ||
|
||
|
||
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/run_validations/_examples/run_a_validation_definition.py - full code example"> | ||
import great_expectations as gx | ||
|
||
context = gx.get_context() | ||
# Hide this | ||
set_up_context_for_example(context) | ||
|
||
# Retrieve the Validation Definition | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/run_a_validation_definition.py - retrieve a Validation Definition"> | ||
validation_definition_name = "my_validation_definition" | ||
validation_definition = context.validation_definitions.get(validation_definition_name) | ||
# </snippet> | ||
|
||
# Run the Validation Definition | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/run_a_validation_definition.py - run a Validation Definition"> | ||
validation_results = validation_definition.run() | ||
# </snippet> | ||
|
||
# Review the Validation Results | ||
# <snippet name="docs/docusaurus/docs/core/run_validations/_examples/run_a_validation_definition.py - review Validation Results"> | ||
print(validation_results) | ||
# </snippet> | ||
# </snippet> | ||
|
||
|
||
# TODO: Set up the example script to use a GX Cloud Data Context and then | ||
# add this portion back in. | ||
# # Get the URL for Validation Results in GX Cloud | ||
# # <snippet name="docs/docusaurus/docs/core/run_validations/_examples/run_a_validation_definition.py - get GX Cloud Validation Result URL"> | ||
# print(validation_results.results_url) | ||
# # </snippet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.