2024-05-28 09:30:30 +02:00
|
|
|
import sys
|
2024-06-05 21:18:37 +02:00
|
|
|
from typing import List
|
2024-05-28 09:30:30 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from metadata.generated.schema.entity.services.databaseService import DatabaseService
|
|
|
|
from metadata.generated.schema.metadataIngestion.testSuitePipeline import (
|
|
|
|
TestSuiteConfigType,
|
|
|
|
TestSuitePipeline,
|
|
|
|
)
|
|
|
|
from metadata.generated.schema.metadataIngestion.workflow import (
|
|
|
|
OpenMetadataWorkflowConfig,
|
|
|
|
Processor,
|
|
|
|
Sink,
|
|
|
|
Source,
|
|
|
|
SourceConfig,
|
|
|
|
WorkflowConfig,
|
|
|
|
)
|
|
|
|
from metadata.generated.schema.tests.basic import TestCaseStatus
|
|
|
|
from metadata.generated.schema.tests.testCase import TestCase
|
2024-07-02 09:56:35 +02:00
|
|
|
from metadata.generated.schema.tests.testSuite import TestSuite
|
2024-06-05 21:18:37 +02:00
|
|
|
from metadata.generated.schema.type.basic import ComponentConfig
|
2024-07-12 09:44:21 +02:00
|
|
|
from metadata.ingestion.api.status import TruncatedStackTraceError
|
2024-05-28 09:30:30 +02:00
|
|
|
from metadata.ingestion.ometa.ometa_api import OpenMetadata
|
|
|
|
from metadata.workflow.data_quality import TestSuiteWorkflow
|
2024-07-17 08:11:34 +02:00
|
|
|
from metadata.workflow.metadata import MetadataWorkflow
|
2024-05-28 09:30:30 +02:00
|
|
|
|
|
|
|
if not sys.version_info >= (3, 9):
|
|
|
|
pytest.skip("requires python 3.9+", allow_module_level=True)
|
|
|
|
|
|
|
|
|
2024-07-17 08:11:34 +02:00
|
|
|
@pytest.fixture()
|
2024-05-28 09:30:30 +02:00
|
|
|
def run_data_quality_workflow(
|
2024-07-17 08:11:34 +02:00
|
|
|
run_workflow,
|
|
|
|
ingestion_config,
|
|
|
|
db_service: DatabaseService,
|
|
|
|
metadata: OpenMetadata,
|
|
|
|
sink_config,
|
|
|
|
workflow_config,
|
2024-05-28 09:30:30 +02:00
|
|
|
):
|
2024-07-17 08:11:34 +02:00
|
|
|
run_workflow(MetadataWorkflow, ingestion_config)
|
|
|
|
test_suite_config = OpenMetadataWorkflowConfig(
|
2024-05-28 09:30:30 +02:00
|
|
|
source=Source(
|
|
|
|
type=TestSuiteConfigType.TestSuite.value,
|
|
|
|
serviceName="MyTestSuite",
|
|
|
|
sourceConfig=SourceConfig(
|
|
|
|
config=TestSuitePipeline(
|
|
|
|
type=TestSuiteConfigType.TestSuite,
|
2024-06-05 21:18:37 +02:00
|
|
|
entityFullyQualifiedName=f"{db_service.fullyQualifiedName.root}.dvdrental.public.customer",
|
2024-05-28 09:30:30 +02:00
|
|
|
)
|
|
|
|
),
|
|
|
|
serviceConnection=db_service.connection,
|
|
|
|
),
|
|
|
|
processor=Processor(
|
|
|
|
type="orm-test-runner",
|
2024-06-05 21:18:37 +02:00
|
|
|
config=ComponentConfig(
|
|
|
|
{
|
|
|
|
"testCases": [
|
|
|
|
{
|
|
|
|
"name": "first_name_includes_tom_and_jerry_wo_enum",
|
|
|
|
"testDefinitionName": "columnValuesToBeInSet",
|
|
|
|
"columnName": "first_name",
|
|
|
|
"parameterValues": [
|
|
|
|
{"name": "allowedValues", "value": "['Tom', 'Jerry']"}
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "first_name_includes_tom_and_jerry",
|
|
|
|
"testDefinitionName": "columnValuesToBeInSet",
|
|
|
|
"columnName": "first_name",
|
|
|
|
"parameterValues": [
|
|
|
|
{"name": "allowedValues", "value": "['Tom', 'Jerry']"},
|
|
|
|
{"name": "matchEnum", "value": ""},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "first_name_is_tom_or_jerry",
|
|
|
|
"testDefinitionName": "columnValuesToBeInSet",
|
|
|
|
"columnName": "first_name",
|
|
|
|
"parameterValues": [
|
|
|
|
{"name": "allowedValues", "value": "['Tom', 'Jerry']"},
|
|
|
|
{"name": "matchEnum", "value": "True"},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
2024-05-28 09:30:30 +02:00
|
|
|
),
|
|
|
|
),
|
2024-07-17 08:11:34 +02:00
|
|
|
sink=Sink.model_validate(sink_config),
|
|
|
|
workflowConfig=WorkflowConfig.model_validate(workflow_config),
|
2024-05-28 09:30:30 +02:00
|
|
|
)
|
2024-07-17 08:11:34 +02:00
|
|
|
test_suite_processor = TestSuiteWorkflow.create(test_suite_config)
|
2024-07-12 09:44:21 +02:00
|
|
|
test_suite_processor.execute()
|
|
|
|
test_suite_processor.raise_from_status()
|
2024-07-02 09:56:35 +02:00
|
|
|
yield
|
|
|
|
test_suite: TestSuite = metadata.get_by_name(
|
|
|
|
TestSuite, "MyTestSuite", nullable=True
|
|
|
|
)
|
|
|
|
if test_suite:
|
|
|
|
metadata.delete(TestSuite, test_suite.id, recursive=True, hard_delete=True)
|
2024-05-28 09:30:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_case_name,expected_status",
|
|
|
|
[
|
|
|
|
("first_name_includes_tom_and_jerry_wo_enum", TestCaseStatus.Success),
|
|
|
|
("first_name_includes_tom_and_jerry", TestCaseStatus.Success),
|
|
|
|
("first_name_is_tom_or_jerry", TestCaseStatus.Failed),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_data_quality(
|
|
|
|
run_data_quality_workflow, metadata: OpenMetadata, test_case_name, expected_status
|
|
|
|
):
|
|
|
|
test_cases: List[TestCase] = metadata.list_entities(
|
|
|
|
TestCase, fields=["*"], skip_on_failure=True
|
|
|
|
).entities
|
|
|
|
test_case: TestCase = next(
|
2024-06-05 21:18:37 +02:00
|
|
|
(t for t in test_cases if t.name.root == test_case_name), None
|
2024-05-28 09:30:30 +02:00
|
|
|
)
|
|
|
|
assert test_case is not None
|
|
|
|
assert test_case.testCaseResult.testCaseStatus == expected_status
|
2024-07-02 09:56:35 +02:00
|
|
|
|
|
|
|
|
2024-07-17 08:11:34 +02:00
|
|
|
@pytest.fixture()
|
|
|
|
def incpompatible_column_type_config(db_service, workflow_config, sink_config):
|
|
|
|
return {
|
2024-07-02 09:56:35 +02:00
|
|
|
"source": {
|
|
|
|
"type": "TestSuite",
|
|
|
|
"serviceName": "MyTestSuite",
|
|
|
|
"sourceConfig": {
|
|
|
|
"config": {
|
|
|
|
"type": "TestSuite",
|
|
|
|
"entityFullyQualifiedName": f"{db_service.fullyQualifiedName.root}.dvdrental.public.customer",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"processor": {
|
|
|
|
"type": "orm-test-runner",
|
|
|
|
"config": {
|
|
|
|
"testCases": [
|
|
|
|
{
|
|
|
|
"name": "incompatible_column_type",
|
|
|
|
"testDefinitionName": "columnValueMaxToBeBetween",
|
|
|
|
"columnName": "first_name",
|
|
|
|
"parameterValues": [
|
|
|
|
{"name": "minValueForMaxInCol", "value": "0"},
|
|
|
|
{"name": "maxValueForMaxInCol", "value": "10"},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "compatible_test",
|
|
|
|
"testDefinitionName": "columnValueMaxToBeBetween",
|
|
|
|
"columnName": "customer_id",
|
|
|
|
"parameterValues": [
|
|
|
|
{"name": "minValueForMaxInCol", "value": "0"},
|
|
|
|
{"name": "maxValueForMaxInCol", "value": "10"},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
},
|
2024-07-17 08:11:34 +02:00
|
|
|
"sink": sink_config,
|
|
|
|
"workflowConfig": workflow_config,
|
2024-07-02 09:56:35 +02:00
|
|
|
}
|
2024-07-17 08:11:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_incompatible_column_type(
|
|
|
|
patch_passwords_for_db_services,
|
|
|
|
run_workflow,
|
|
|
|
ingestion_config,
|
|
|
|
incpompatible_column_type_config,
|
|
|
|
metadata: OpenMetadata,
|
|
|
|
db_service,
|
|
|
|
):
|
|
|
|
run_workflow(MetadataWorkflow, ingestion_config)
|
|
|
|
test_suite_processor = run_workflow(
|
|
|
|
TestSuiteWorkflow, incpompatible_column_type_config, raise_from_status=False
|
|
|
|
)
|
2024-07-12 09:44:21 +02:00
|
|
|
assert test_suite_processor.steps[0].get_status().failures == [
|
|
|
|
TruncatedStackTraceError(
|
2024-07-02 09:56:35 +02:00
|
|
|
name="Incompatible Column for Test Case",
|
|
|
|
error="Test case incompatible_column_type of type columnValueMaxToBeBetween is not compatible with column first_name of type VARCHAR",
|
|
|
|
)
|
|
|
|
], "Test case incompatible_column_type should fail"
|
|
|
|
assert (
|
|
|
|
f"{db_service.fullyQualifiedName.root}.dvdrental.public.customer.customer_id.compatible_test"
|
2024-07-12 09:44:21 +02:00
|
|
|
in test_suite_processor.steps[1].get_status().records
|
2024-07-02 09:56:35 +02:00
|
|
|
), "Test case compatible_test should pass"
|