2022-09-26 19:41:40 +05:30
|
|
|
# Copyright 2021 Collate
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
# 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.
|
|
|
|
|
2023-06-20 15:16:45 +05:30
|
|
|
"""
|
|
|
|
snowflake unit tests
|
|
|
|
"""
|
|
|
|
# pylint: disable=line-too-long
|
2022-08-30 18:05:09 +05:30
|
|
|
from unittest import TestCase
|
2023-09-12 14:25:42 +02:00
|
|
|
from unittest.mock import PropertyMock, patch
|
2022-08-30 18:05:09 +05:30
|
|
|
|
2023-06-20 15:16:45 +05:30
|
|
|
from metadata.generated.schema.entity.data.table import TableType
|
2024-03-15 14:00:49 +01:00
|
|
|
from metadata.generated.schema.entity.services.ingestionPipelines.ingestionPipeline import (
|
|
|
|
PipelineStatus,
|
|
|
|
)
|
2022-08-30 18:05:09 +05:30
|
|
|
from metadata.generated.schema.metadataIngestion.workflow import (
|
|
|
|
OpenMetadataWorkflowConfig,
|
|
|
|
)
|
2022-12-27 15:00:22 +01:00
|
|
|
from metadata.ingestion.source.database.snowflake.metadata import SnowflakeSource
|
2024-01-08 20:16:35 +01:00
|
|
|
from metadata.ingestion.source.database.snowflake.models import SnowflakeStoredProcedure
|
2022-08-30 18:05:09 +05:30
|
|
|
|
2024-03-15 14:00:49 +01:00
|
|
|
SNOWFLAKE_CONFIGURATION = {
|
2022-08-30 18:05:09 +05:30
|
|
|
"source": {
|
|
|
|
"type": "snowflake",
|
|
|
|
"serviceName": "local_snowflake",
|
|
|
|
"serviceConnection": {
|
|
|
|
"config": {
|
|
|
|
"type": "Snowflake",
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"database": "database",
|
|
|
|
"warehouse": "warehouse",
|
|
|
|
"account": "account.region_name.cloud_service",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"sourceConfig": {"config": {"type": "DatabaseMetadata"}},
|
|
|
|
},
|
|
|
|
"sink": {"type": "metadata-rest", "config": {}},
|
|
|
|
"workflowConfig": {
|
|
|
|
"openMetadataServerConfig": {
|
|
|
|
"hostPort": "http://localhost:8585/api",
|
2022-09-26 16:19:47 +05:30
|
|
|
"authProvider": "openmetadata",
|
2025-01-02 13:07:55 +05:30
|
|
|
"securityConfig": {"jwtToken": "snowflake"},
|
2022-08-30 18:05:09 +05:30
|
|
|
}
|
|
|
|
},
|
2024-03-15 14:00:49 +01:00
|
|
|
"ingestionPipelineFQN": "snowflake.mock_pipeline",
|
2022-08-30 18:05:09 +05:30
|
|
|
}
|
|
|
|
|
2024-03-15 14:00:49 +01:00
|
|
|
SNOWFLAKE_INCREMENTAL_CONFIGURATION = {
|
|
|
|
**SNOWFLAKE_CONFIGURATION,
|
|
|
|
**{
|
|
|
|
"source": {
|
|
|
|
**SNOWFLAKE_CONFIGURATION["source"],
|
|
|
|
"sourceConfig": {
|
|
|
|
"config": {"type": "DatabaseMetadata", "incremental": {"enabled": True}}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
SNOWFLAKE_CONFIGURATIONS = {
|
|
|
|
"incremental": SNOWFLAKE_INCREMENTAL_CONFIGURATION,
|
|
|
|
"not_incremental": SNOWFLAKE_CONFIGURATION,
|
|
|
|
}
|
|
|
|
|
|
|
|
MOCK_PIPELINE_STATUSES = [
|
|
|
|
PipelineStatus(
|
2024-06-05 21:18:37 +02:00
|
|
|
runId="1",
|
2024-03-15 14:00:49 +01:00
|
|
|
pipelineState="success",
|
|
|
|
timestamp=10,
|
|
|
|
startDate=10,
|
|
|
|
endDate=20,
|
|
|
|
),
|
|
|
|
PipelineStatus(
|
2024-06-05 21:18:37 +02:00
|
|
|
runId="2",
|
2024-03-15 14:00:49 +01:00
|
|
|
pipelineState="success",
|
|
|
|
timestamp=30,
|
|
|
|
startDate=30,
|
|
|
|
endDate=50,
|
|
|
|
),
|
|
|
|
PipelineStatus(
|
2024-06-05 21:18:37 +02:00
|
|
|
runId="3",
|
2024-03-15 14:00:49 +01:00
|
|
|
pipelineState="failed",
|
|
|
|
timestamp=70,
|
|
|
|
startDate=70,
|
|
|
|
endDate=80,
|
|
|
|
),
|
|
|
|
]
|
2022-08-30 18:05:09 +05:30
|
|
|
|
|
|
|
RAW_CLUSTER_KEY_EXPRS = [
|
|
|
|
"LINEAR(c1, c2)",
|
|
|
|
"LINEAR(to_date(c1), substring(c2, 0, 10))",
|
|
|
|
"LINEAR(v:'Data':id::number)",
|
|
|
|
"LINEAR(to_date(substring(c2, 0, 10)))",
|
|
|
|
"col",
|
|
|
|
]
|
|
|
|
|
|
|
|
EXPECTED_PARTITION_COLUMNS = [
|
|
|
|
["c1", "c2"],
|
|
|
|
["c1", "c2"],
|
|
|
|
["v"],
|
|
|
|
["c2"],
|
|
|
|
["col"],
|
|
|
|
]
|
|
|
|
|
2023-06-20 15:16:45 +05:30
|
|
|
MOCK_DB_NAME = "SNOWFLAKE_SAMPLE_DATA"
|
|
|
|
MOCK_SCHEMA_NAME_1 = "INFORMATION_SCHEMA"
|
|
|
|
MOCK_SCHEMA_NAME_2 = "TPCDS_SF10TCL"
|
|
|
|
MOCK_VIEW_NAME = "COLUMNS"
|
|
|
|
MOCK_TABLE_NAME = "CALL_CENTER"
|
2023-10-20 09:14:08 +02:00
|
|
|
EXPECTED_SNOW_URL_VIEW = "https://app.snowflake.com/random_org/random_account/#/data/databases/SNOWFLAKE_SAMPLE_DATA/schemas/INFORMATION_SCHEMA/view/COLUMNS"
|
|
|
|
EXPECTED_SNOW_URL_TABLE = "https://app.snowflake.com/random_org/random_account/#/data/databases/SNOWFLAKE_SAMPLE_DATA/schemas/TPCDS_SF10TCL/table/CALL_CENTER"
|
2023-06-20 15:16:45 +05:30
|
|
|
|
2022-08-30 18:05:09 +05:30
|
|
|
|
2024-03-15 14:00:49 +01:00
|
|
|
def get_snowflake_sources():
|
|
|
|
sources = {}
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"metadata.ingestion.source.database.common_db_source.CommonDbSourceService.test_connection",
|
|
|
|
return_value=False,
|
|
|
|
):
|
2024-06-07 04:36:17 +02:00
|
|
|
config = OpenMetadataWorkflowConfig.model_validate(
|
2024-03-15 14:00:49 +01:00
|
|
|
SNOWFLAKE_CONFIGURATIONS["not_incremental"]
|
|
|
|
)
|
|
|
|
sources["not_incremental"] = SnowflakeSource.create(
|
|
|
|
SNOWFLAKE_CONFIGURATIONS["not_incremental"]["source"],
|
|
|
|
config.workflowConfig.openMetadataServerConfig,
|
|
|
|
SNOWFLAKE_CONFIGURATIONS["not_incremental"]["ingestionPipelineFQN"],
|
|
|
|
)
|
|
|
|
|
|
|
|
with patch(
|
|
|
|
"metadata.ingestion.source.database.incremental_metadata_extraction.IncrementalConfigCreator._get_pipeline_statuses",
|
|
|
|
return_value=MOCK_PIPELINE_STATUSES,
|
|
|
|
):
|
2024-06-07 04:36:17 +02:00
|
|
|
config = OpenMetadataWorkflowConfig.model_validate(
|
2024-03-15 14:00:49 +01:00
|
|
|
SNOWFLAKE_CONFIGURATIONS["incremental"]
|
|
|
|
)
|
|
|
|
sources["incremental"] = SnowflakeSource.create(
|
|
|
|
SNOWFLAKE_CONFIGURATIONS["incremental"]["source"],
|
|
|
|
config.workflowConfig.openMetadataServerConfig,
|
|
|
|
SNOWFLAKE_CONFIGURATIONS["incremental"]["ingestionPipelineFQN"],
|
|
|
|
)
|
|
|
|
return sources
|
|
|
|
|
|
|
|
|
2022-08-30 18:05:09 +05:30
|
|
|
class SnowflakeUnitTest(TestCase):
|
2023-06-20 15:16:45 +05:30
|
|
|
"""
|
|
|
|
Unit test for snowflake source
|
|
|
|
"""
|
|
|
|
|
2024-03-15 14:00:49 +01:00
|
|
|
def __init__(self, methodName) -> None:
|
2022-08-30 18:05:09 +05:30
|
|
|
super().__init__(methodName)
|
2024-03-15 14:00:49 +01:00
|
|
|
self.sources = get_snowflake_sources()
|
2022-08-30 18:05:09 +05:30
|
|
|
|
|
|
|
def test_partition_parse_columns(self):
|
2024-03-15 14:00:49 +01:00
|
|
|
for source in self.sources.values():
|
|
|
|
for idx, expr in enumerate(RAW_CLUSTER_KEY_EXPRS):
|
|
|
|
assert (
|
|
|
|
source.parse_column_name_from_expr(expr)
|
|
|
|
== EXPECTED_PARTITION_COLUMNS[idx]
|
|
|
|
)
|
2023-06-20 15:16:45 +05:30
|
|
|
|
2024-03-15 14:00:49 +01:00
|
|
|
def test_incremental_config_is_created_accordingly(self):
|
|
|
|
self.assertFalse(self.sources["not_incremental"].incremental.enabled)
|
|
|
|
|
|
|
|
self.assertTrue(self.sources["incremental"].incremental.enabled)
|
|
|
|
|
|
|
|
milliseconds_in_one_day = 24 * 60 * 60 * 1000
|
|
|
|
safety_margin_days = self.sources[
|
|
|
|
"incremental"
|
|
|
|
].source_config.incremental.safetyMarginDays
|
2023-06-20 15:16:45 +05:30
|
|
|
|
|
|
|
self.assertEqual(
|
2024-03-15 14:00:49 +01:00
|
|
|
self.sources["incremental"].incremental.start_timestamp,
|
|
|
|
30 - safety_margin_days * milliseconds_in_one_day,
|
2023-06-20 15:16:45 +05:30
|
|
|
)
|
|
|
|
|
2024-03-15 14:00:49 +01:00
|
|
|
def _assert_urls(self):
|
|
|
|
for source in self.sources.values():
|
|
|
|
self.assertEqual(
|
|
|
|
source.get_source_url(
|
|
|
|
database_name=MOCK_DB_NAME,
|
|
|
|
schema_name=MOCK_SCHEMA_NAME_2,
|
|
|
|
table_name=MOCK_TABLE_NAME,
|
|
|
|
table_type=TableType.Regular,
|
|
|
|
),
|
|
|
|
EXPECTED_SNOW_URL_TABLE,
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
source.get_source_url(
|
|
|
|
database_name=MOCK_DB_NAME,
|
|
|
|
schema_name=MOCK_SCHEMA_NAME_1,
|
|
|
|
table_name=MOCK_VIEW_NAME,
|
|
|
|
table_type=TableType.View,
|
|
|
|
),
|
|
|
|
EXPECTED_SNOW_URL_VIEW,
|
|
|
|
)
|
|
|
|
|
2023-06-20 15:16:45 +05:30
|
|
|
def test_source_url(self):
|
|
|
|
"""
|
|
|
|
method to test source url
|
|
|
|
"""
|
|
|
|
with patch.object(
|
2023-09-12 14:25:42 +02:00
|
|
|
SnowflakeSource,
|
|
|
|
"account",
|
|
|
|
return_value="random_account",
|
|
|
|
new_callable=PropertyMock,
|
2023-06-20 15:16:45 +05:30
|
|
|
):
|
|
|
|
with patch.object(
|
|
|
|
SnowflakeSource,
|
2023-10-20 09:14:08 +02:00
|
|
|
"org_name",
|
|
|
|
return_value="random_org",
|
2023-09-12 14:25:42 +02:00
|
|
|
new_callable=PropertyMock,
|
2023-06-20 15:16:45 +05:30
|
|
|
):
|
|
|
|
self._assert_urls()
|
|
|
|
|
|
|
|
with patch.object(
|
|
|
|
SnowflakeSource,
|
2023-10-20 09:14:08 +02:00
|
|
|
"org_name",
|
2023-09-12 14:25:42 +02:00
|
|
|
new_callable=PropertyMock,
|
2023-06-20 15:16:45 +05:30
|
|
|
return_value=None,
|
|
|
|
):
|
2024-03-15 14:00:49 +01:00
|
|
|
for source in self.sources.values():
|
|
|
|
self.assertIsNone(
|
|
|
|
source.get_source_url(
|
|
|
|
database_name=MOCK_DB_NAME,
|
|
|
|
schema_name=MOCK_SCHEMA_NAME_1,
|
|
|
|
table_name=MOCK_VIEW_NAME,
|
|
|
|
table_type=TableType.View,
|
|
|
|
)
|
2023-06-20 15:16:45 +05:30
|
|
|
)
|
2024-01-08 20:16:35 +01:00
|
|
|
|
|
|
|
def test_stored_procedure_validator(self):
|
|
|
|
"""Review how we are building the SP signature"""
|
|
|
|
|
|
|
|
sp_payload = SnowflakeStoredProcedure(
|
|
|
|
NAME="test_sp",
|
|
|
|
OWNER="owner",
|
|
|
|
LANGUAGE="SQL",
|
|
|
|
SIGNATURE="(NAME VARCHAR, NUMBER INT)",
|
|
|
|
COMMENT="comment",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual("(VARCHAR, INT)", sp_payload.unquote_signature())
|
|
|
|
|
|
|
|
# Check https://github.com/open-metadata/OpenMetadata/issues/14492
|
|
|
|
sp_payload = SnowflakeStoredProcedure(
|
|
|
|
NAME="test_sp",
|
|
|
|
OWNER="owner",
|
|
|
|
LANGUAGE="SQL",
|
|
|
|
SIGNATURE="()",
|
|
|
|
COMMENT="comment",
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual("()", sp_payload.unquote_signature())
|