| 
									
										
										
										
											2025-04-03 10:39:47 +05:30
										 |  |  | #  Copyright 2025 Collate | 
					
						
							|  |  |  | #  Licensed under the Collate Community License, Version 1.0 (the "License"); | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | #  you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | #  You may obtain a copy of the License at | 
					
						
							| 
									
										
										
										
											2025-04-03 10:39:47 +05:30
										 |  |  | #  https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | #  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | #  distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | #  See the License for the specific language governing permissions and | 
					
						
							|  |  |  | #  limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | OpenMetadata API test suite mixin test | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2024-06-14 15:23:51 +05:30
										 |  |  | from datetime import datetime, timezone | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | from unittest import TestCase | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from metadata.generated.schema.api.tests.createTestCase import CreateTestCaseRequest | 
					
						
							|  |  |  | from metadata.generated.schema.api.tests.createTestDefinition import ( | 
					
						
							|  |  |  |     CreateTestDefinitionRequest, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  | from metadata.generated.schema.api.tests.createTestSuite import ( | 
					
						
							|  |  |  |     CreateTestSuiteRequest, | 
					
						
							|  |  |  |     TestSuiteEntityName, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | from metadata.generated.schema.entity.services.connections.metadata.openMetadataConnection import ( | 
					
						
							|  |  |  |     OpenMetadataConnection, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-09-26 16:19:47 +05:30
										 |  |  | from metadata.generated.schema.security.client.openMetadataJWTClientConfig import ( | 
					
						
							|  |  |  |     OpenMetadataJWTClientConfig, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-11-23 10:46:56 +01:00
										 |  |  | from metadata.generated.schema.tests.basic import ( | 
					
						
							|  |  |  |     TestCaseResult, | 
					
						
							|  |  |  |     TestCaseStatus, | 
					
						
							|  |  |  |     TestResultValue, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | from metadata.generated.schema.tests.testCase import TestCase as OMetaTestCase | 
					
						
							|  |  |  | from metadata.generated.schema.tests.testCase import TestCaseParameterValue | 
					
						
							|  |  |  | from metadata.generated.schema.tests.testDefinition import ( | 
					
						
							|  |  |  |     EntityType, | 
					
						
							|  |  |  |     TestCaseParameterDefinition, | 
					
						
							|  |  |  |     TestDefinition, | 
					
						
							|  |  |  |     TestPlatform, | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | from metadata.generated.schema.tests.testSuite import TestSuite | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  | from metadata.generated.schema.type.basic import ( | 
					
						
							|  |  |  |     EntityLink, | 
					
						
							|  |  |  |     FullyQualifiedEntityName, | 
					
						
							|  |  |  |     Markdown, | 
					
						
							|  |  |  |     TestCaseEntityName, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | from metadata.ingestion.ometa.ometa_api import OpenMetadata | 
					
						
							| 
									
										
										
										
											2023-12-22 15:43:50 +01:00
										 |  |  | from metadata.utils.helpers import datetime_to_ts | 
					
						
							| 
									
										
										
										
											2022-11-23 10:46:56 +01:00
										 |  |  | from metadata.utils.time_utils import ( | 
					
						
							|  |  |  |     get_beginning_of_day_timestamp_mill, | 
					
						
							|  |  |  |     get_end_of_day_timestamp_mill, | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OMetaTestSuiteTest(TestCase): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Run this integration test with the local API available | 
					
						
							|  |  |  |     Install the ingestion package before running the tests | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     service_entity_id = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-26 16:19:47 +05:30
										 |  |  |     server_config = OpenMetadataConnection( | 
					
						
							|  |  |  |         hostPort="http://localhost:8585/api", | 
					
						
							|  |  |  |         authProvider="openmetadata", | 
					
						
							|  |  |  |         securityConfig=OpenMetadataJWTClientConfig( | 
					
						
							|  |  |  |             jwtToken="eyJraWQiOiJHYjM4OWEtOWY3Ni1nZGpzLWE5MmotMDI0MmJrOTQzNTYiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImlzQm90IjpmYWxzZSwiaXNzIjoib3Blbi1tZXRhZGF0YS5vcmciLCJpYXQiOjE2NjM5Mzg0NjIsImVtYWlsIjoiYWRtaW5Ab3Blbm1ldGFkYXRhLm9yZyJ9.tS8um_5DKu7HgzGBzS1VTA5uUjKWOCU0B_j08WXBiEC0mr0zNREkqVfwFDD-d24HlNEbrqioLsBuFRiwIWKc1m_ZlVQbG7P36RUxhuv2vbSp80FKyNM-Tj93FDzq91jsyNmsQhyNv_fNr3TXfzzSPjHt8Go0FMMP66weoKMgW2PbXlhVKwEuXUHyakLLzewm9UMeQaEiRzhiTMU3UkLXcKbYEJJvfNFcLwSl9W8JCO_l0Yj3ud-qt_nQYEZwqW6u5nfdQllN133iikV4fM5QZsMCnm8Rq1mvLR0y9bmJiD7fwM1tmJ791TUWqmKaTnP49U493VanKpUAfzIiOiIbhg" | 
					
						
							|  |  |  |         ), | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |     metadata = OpenMetadata(server_config) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert metadata.health_check() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test_definition = metadata.create_or_update( | 
					
						
							|  |  |  |         CreateTestDefinitionRequest( | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |             name=TestCaseEntityName("testDefinitionForIntegration"), | 
					
						
							|  |  |  |             description=Markdown( | 
					
						
							|  |  |  |                 root="this is a test definition for integration tests" | 
					
						
							|  |  |  |             ), | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |             entityType=EntityType.TABLE, | 
					
						
							|  |  |  |             testPlatforms=[TestPlatform.GreatExpectations], | 
					
						
							|  |  |  |             parameterDefinition=[TestCaseParameterDefinition(name="foo")], | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @classmethod | 
					
						
							|  |  |  |     def setUpClass(cls) -> None: | 
					
						
							|  |  |  |         """set up tests""" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-02 08:19:13 +02:00
										 |  |  |         cls.test_suite: TestSuite = cls.metadata.create_or_update_executable_test_suite( | 
					
						
							| 
									
										
										
										
											2023-03-31 16:57:53 +02:00
										 |  |  |             CreateTestSuiteRequest( | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |                 name=TestSuiteEntityName( | 
					
						
							|  |  |  |                     root="sample_data.ecommerce_db.shopify.dim_address.TestSuite" | 
					
						
							|  |  |  |                 ), | 
					
						
							|  |  |  |                 description=Markdown( | 
					
						
							|  |  |  |                     root="This is a test suite for the integration tests" | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2025-01-07 17:59:54 +01:00
										 |  |  |                 basicEntityReference=FullyQualifiedEntityName( | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |                     "sample_data.ecommerce_db.shopify.dim_address" | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2023-03-31 16:57:53 +02:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |         cls.metadata.create_or_update( | 
					
						
							|  |  |  |             CreateTestCaseRequest( | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |                 name=TestCaseEntityName("testCaseForIntegration"), | 
					
						
							|  |  |  |                 entityLink=EntityLink( | 
					
						
							|  |  |  |                     "<#E::table::sample_data.ecommerce_db.shopify.dim_address>" | 
					
						
							|  |  |  |                 ), | 
					
						
							| 
									
										
										
										
											2023-02-13 00:08:55 -08:00
										 |  |  |                 testDefinition=cls.test_definition.fullyQualifiedName, | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |                 parameterValues=[TestCaseParameterValue(name="foo", value="10")], | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 10:46:56 +01:00
										 |  |  |         cls.metadata.add_test_case_results( | 
					
						
							|  |  |  |             test_results=TestCaseResult( | 
					
						
							| 
									
										
										
										
											2024-06-14 15:23:51 +05:30
										 |  |  |                 timestamp=datetime_to_ts(datetime.now(timezone.utc)), | 
					
						
							| 
									
										
										
										
											2022-11-23 10:46:56 +01:00
										 |  |  |                 testCaseStatus=TestCaseStatus.Success, | 
					
						
							| 
									
										
										
										
											2023-12-22 15:43:50 +01:00
										 |  |  |                 result="Test Case Success", | 
					
						
							| 
									
										
										
										
											2022-11-23 10:46:56 +01:00
										 |  |  |                 sampleData=None, | 
					
						
							|  |  |  |                 testResultValue=[TestResultValue(name="foo", value="10")], | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             test_case_fqn="sample_data.ecommerce_db.shopify.dim_address.testCaseForIntegration", | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |     def test_get_or_create_test_suite(self): | 
					
						
							|  |  |  |         """test we get a test suite object""" | 
					
						
							|  |  |  |         test_suite = self.metadata.get_or_create_test_suite( | 
					
						
							| 
									
										
										
										
											2023-06-06 12:09:16 +02:00
										 |  |  |             "sample_data.ecommerce_db.shopify.dim_address.TestSuite" | 
					
						
							| 
									
										
										
										
											2023-06-02 08:19:13 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |         assert ( | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |             test_suite.name.root | 
					
						
							| 
									
										
										
										
											2023-06-06 12:09:16 +02:00
										 |  |  |             == "sample_data.ecommerce_db.shopify.dim_address.TestSuite" | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |         assert isinstance(test_suite, TestSuite) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_or_create_test_definition(self): | 
					
						
							|  |  |  |         """test we get a test definition object""" | 
					
						
							|  |  |  |         test_definition = self.metadata.get_or_create_test_definition( | 
					
						
							|  |  |  |             "testDefinitionForIntegration" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |         assert test_definition.name.root == "testDefinitionForIntegration" | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |         assert isinstance(test_definition, TestDefinition) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_or_create_test_case(self): | 
					
						
							|  |  |  |         """test we get a test case object""" | 
					
						
							|  |  |  |         test_case = self.metadata.get_or_create_test_case( | 
					
						
							|  |  |  |             "sample_data.ecommerce_db.shopify.dim_address.testCaseForIntegration" | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |         assert test_case.name.root == "testCaseForIntegration" | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |         assert isinstance(test_case, OMetaTestCase) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 12:52:09 +02:00
										 |  |  |     def test_create_test_case(self): | 
					
						
							|  |  |  |         """test we get a create the test case object if it does not exists""" | 
					
						
							|  |  |  |         test_case_fqn = ( | 
					
						
							|  |  |  |             "sample_data.ecommerce_db.shopify.dim_address.aNonExistingTestCase" | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         test_case = self.metadata.get_by_name( | 
					
						
							|  |  |  |             entity=OMetaTestCase, fqn=test_case_fqn, fields=["*"] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert test_case is None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_case = self.metadata.get_or_create_test_case( | 
					
						
							|  |  |  |             test_case_fqn, | 
					
						
							|  |  |  |             test_definition_fqn="columnValuesToMatchRegex", | 
					
						
							|  |  |  |             entity_link="<#E::table::sample_data.ecommerce_db.shopify.dim_address::columns::last_name>", | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |             test_case_parameter_values=[ | 
					
						
							|  |  |  |                 TestCaseParameterValue(name="regex", value=".*") | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2023-04-26 12:52:09 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-06-05 21:18:37 +02:00
										 |  |  |         assert test_case.name.root == "aNonExistingTestCase" | 
					
						
							| 
									
										
										
										
											2023-04-26 12:52:09 +02:00
										 |  |  |         assert isinstance(test_case, OMetaTestCase) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-23 10:46:56 +01:00
										 |  |  |     def test_get_test_case_results(self): | 
					
						
							|  |  |  |         """test get test case result method""" | 
					
						
							|  |  |  |         res = self.metadata.get_test_case_results( | 
					
						
							|  |  |  |             "sample_data.ecommerce_db.shopify.dim_address.testCaseForIntegration", | 
					
						
							|  |  |  |             get_beginning_of_day_timestamp_mill(), | 
					
						
							|  |  |  |             get_end_of_day_timestamp_mill(), | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert res | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |     @classmethod | 
					
						
							|  |  |  |     def tearDownClass(cls) -> None: | 
					
						
							| 
									
										
										
										
											2023-06-15 21:27:54 +02:00
										 |  |  |         cls.metadata.delete_executable_test_suite( | 
					
						
							| 
									
										
										
										
											2022-09-12 05:07:19 +02:00
										 |  |  |             entity=TestSuite, | 
					
						
							|  |  |  |             entity_id=cls.test_suite.id, | 
					
						
							|  |  |  |             recursive=True, | 
					
						
							|  |  |  |             hard_delete=True, | 
					
						
							|  |  |  |         ) |