2023-12-14 23:01:51 +05:30
from typing import Any , Dict
2022-09-12 23:12:52 +05:30
from unittest . mock import MagicMock , patch
import pytest
2022-12-12 19:53:12 +05:30
from pydantic import ValidationError
2022-09-12 23:12:52 +05:30
2023-11-23 09:45:25 +01:00
from datahub . configuration . common import AllowDenyPattern
2023-06-07 04:09:05 -04:00
from datahub . configuration . oauth import OAuthConfiguration
2023-05-09 02:43:57 +05:30
from datahub . configuration . pattern_utils import UUID_REGEX
2022-09-12 23:12:52 +05:30
from datahub . ingestion . api . source import SourceCapability
2022-12-28 21:50:37 +05:30
from datahub . ingestion . source . snowflake . constants import (
CLIENT_PREFETCH_THREADS ,
CLIENT_SESSION_KEEP_ALIVE ,
SnowflakeCloudProvider ,
)
2023-05-09 02:43:57 +05:30
from datahub . ingestion . source . snowflake . snowflake_config import (
2024-07-12 15:08:51 -07:00
DEFAULT_TEMP_TABLES_PATTERNS ,
2023-05-09 02:43:57 +05:30
SnowflakeV2Config ,
)
from datahub . ingestion . source . snowflake . snowflake_query import (
2023-11-23 09:45:25 +01:00
SnowflakeQuery ,
2023-05-09 02:43:57 +05:30
create_deny_regex_sql_filter ,
)
2023-03-16 04:55:52 -07:00
from datahub . ingestion . source . snowflake . snowflake_usage_v2 import (
SnowflakeObjectAccessEntry ,
)
2024-07-12 15:08:51 -07:00
from datahub . ingestion . source . snowflake . snowflake_utils import SnowsightUrlBuilder
2022-09-12 23:12:52 +05:30
from datahub . ingestion . source . snowflake . snowflake_v2 import SnowflakeV2Source
2023-12-14 23:01:51 +05:30
from tests . test_helpers import test_connection_helpers
default_oauth_dict : Dict [ str , Any ] = {
" client_id " : " client_id " ,
" client_secret " : " secret " ,
" use_certificate " : False ,
" provider " : " microsoft " ,
" scopes " : [ " datahub_role " ] ,
" authority_url " : " https://dev-abc.okta.com/oauth2/def/v1/token " ,
}
2022-09-12 23:12:52 +05:30
def test_snowflake_source_throws_error_on_account_id_missing ( ) :
2023-12-14 23:01:51 +05:30
with pytest . raises ( ValidationError , match = " account_id \n field required " ) :
2022-09-12 23:12:52 +05:30
SnowflakeV2Config . parse_obj (
{
" username " : " user " ,
" password " : " password " ,
}
)
2023-06-07 04:09:05 -04:00
def test_no_client_id_invalid_oauth_config ( ) :
2023-12-14 23:01:51 +05:30
oauth_dict = default_oauth_dict . copy ( )
del oauth_dict [ " client_id " ]
with pytest . raises ( ValueError , match = " client_id \n field required " ) :
2023-06-07 04:09:05 -04:00
OAuthConfiguration . parse_obj ( oauth_dict )
def test_snowflake_throws_error_on_client_secret_missing_if_use_certificate_is_false ( ) :
2023-12-14 23:01:51 +05:30
oauth_dict = default_oauth_dict . copy ( )
del oauth_dict [ " client_secret " ]
2023-06-07 04:09:05 -04:00
OAuthConfiguration . parse_obj ( oauth_dict )
2023-12-14 23:01:51 +05:30
with pytest . raises (
ValueError ,
match = " ' oauth_config.client_secret ' was none but should be set when using use_certificate false for oauth_config " ,
) :
2022-09-12 23:12:52 +05:30
SnowflakeV2Config . parse_obj (
{
" account_id " : " test " ,
" authentication_type " : " OAUTH_AUTHENTICATOR " ,
" oauth_config " : oauth_dict ,
}
)
2023-06-07 04:09:05 -04:00
def test_snowflake_throws_error_on_encoded_oauth_private_key_missing_if_use_certificate_is_true ( ) :
2023-12-14 23:01:51 +05:30
oauth_dict = default_oauth_dict . copy ( )
oauth_dict [ " use_certificate " ] = True
2023-06-07 04:09:05 -04:00
OAuthConfiguration . parse_obj ( oauth_dict )
2023-12-14 23:01:51 +05:30
with pytest . raises (
ValueError ,
match = " ' base64_encoded_oauth_private_key ' was none but should be set when using certificate for oauth_config " ,
) :
2022-09-12 23:12:52 +05:30
SnowflakeV2Config . parse_obj (
{
" account_id " : " test " ,
" authentication_type " : " OAUTH_AUTHENTICATOR " ,
" oauth_config " : oauth_dict ,
}
)
2023-06-07 04:09:05 -04:00
def test_snowflake_oauth_okta_does_not_support_certificate ( ) :
2023-12-14 23:01:51 +05:30
oauth_dict = default_oauth_dict . copy ( )
oauth_dict [ " use_certificate " ] = True
oauth_dict [ " provider " ] = " okta "
2023-06-07 04:09:05 -04:00
OAuthConfiguration . parse_obj ( oauth_dict )
2023-12-14 23:01:51 +05:30
with pytest . raises (
ValueError , match = " Certificate authentication is not supported for Okta. "
) :
2022-09-12 23:12:52 +05:30
SnowflakeV2Config . parse_obj (
{
" account_id " : " test " ,
" authentication_type " : " OAUTH_AUTHENTICATOR " ,
" oauth_config " : oauth_dict ,
}
)
2023-06-07 04:09:05 -04:00
def test_snowflake_oauth_happy_paths ( ) :
2023-12-14 23:01:51 +05:30
oauth_dict = default_oauth_dict . copy ( )
oauth_dict [ " provider " ] = " okta "
2023-06-07 04:09:05 -04:00
assert SnowflakeV2Config . parse_obj (
{
" account_id " : " test " ,
" authentication_type " : " OAUTH_AUTHENTICATOR " ,
2023-12-14 23:01:51 +05:30
" oauth_config " : oauth_dict ,
2023-06-07 04:09:05 -04:00
}
)
2023-12-14 23:01:51 +05:30
oauth_dict [ " use_certificate " ] = True
oauth_dict [ " provider " ] = " microsoft "
oauth_dict [ " encoded_oauth_public_key " ] = " publickey "
oauth_dict [ " encoded_oauth_private_key " ] = " privatekey "
2023-06-07 04:09:05 -04:00
assert SnowflakeV2Config . parse_obj (
{
" account_id " : " test " ,
" authentication_type " : " OAUTH_AUTHENTICATOR " ,
2023-12-14 23:01:51 +05:30
" oauth_config " : oauth_dict ,
2023-06-07 04:09:05 -04:00
}
)
2023-12-14 23:01:51 +05:30
default_config_dict : Dict [ str , Any ] = {
" username " : " user " ,
" password " : " password " ,
" account_id " : " https://acctname.snowflakecomputing.com " ,
" warehouse " : " COMPUTE_WH " ,
" role " : " sysadmin " ,
}
2022-09-12 23:12:52 +05:30
def test_account_id_is_added_when_host_port_is_present ( ) :
2023-12-14 23:01:51 +05:30
config_dict = default_config_dict . copy ( )
del config_dict [ " account_id " ]
config_dict [ " host_port " ] = " acctname "
config = SnowflakeV2Config . parse_obj ( config_dict )
2022-09-12 23:12:52 +05:30
assert config . account_id == " acctname "
2022-12-07 19:26:03 +05:30
def test_account_id_with_snowflake_host_suffix ( ) :
2023-12-14 23:01:51 +05:30
config = SnowflakeV2Config . parse_obj ( default_config_dict )
2023-04-11 02:44:42 +05:30
assert config . account_id == " acctname "
2022-12-07 19:26:03 +05:30
2022-09-12 23:12:52 +05:30
def test_snowflake_uri_default_authentication ( ) :
2023-12-14 23:01:51 +05:30
config = SnowflakeV2Config . parse_obj ( default_config_dict )
2023-10-18 11:34:45 -04:00
assert config . get_sql_alchemy_url ( ) == (
" snowflake://user:password@acctname "
" ?application=acryl_datahub "
" &authenticator=SNOWFLAKE "
" &role=sysadmin "
" &warehouse=COMPUTE_WH "
2022-09-12 23:12:52 +05:30
)
def test_snowflake_uri_external_browser_authentication ( ) :
2023-12-14 23:01:51 +05:30
config_dict = default_config_dict . copy ( )
del config_dict [ " password " ]
config_dict [ " authentication_type " ] = " EXTERNAL_BROWSER_AUTHENTICATOR "
config = SnowflakeV2Config . parse_obj ( config_dict )
2023-10-18 11:34:45 -04:00
assert config . get_sql_alchemy_url ( ) == (
" snowflake://user@acctname "
" ?application=acryl_datahub "
" &authenticator=EXTERNALBROWSER "
" &role=sysadmin "
" &warehouse=COMPUTE_WH "
2022-09-12 23:12:52 +05:30
)
def test_snowflake_uri_key_pair_authentication ( ) :
2023-12-14 23:01:51 +05:30
config_dict = default_config_dict . copy ( )
del config_dict [ " password " ]
config_dict [ " authentication_type " ] = " KEY_PAIR_AUTHENTICATOR "
config_dict [ " private_key_path " ] = " /a/random/path "
config_dict [ " private_key_password " ] = " a_random_password "
config = SnowflakeV2Config . parse_obj ( config_dict )
2022-09-12 23:12:52 +05:30
2023-10-18 11:34:45 -04:00
assert config . get_sql_alchemy_url ( ) == (
" snowflake://user@acctname "
" ?application=acryl_datahub "
" &authenticator=SNOWFLAKE_JWT "
" &role=sysadmin "
" &warehouse=COMPUTE_WH "
2022-09-12 23:12:52 +05:30
)
def test_options_contain_connect_args ( ) :
2023-12-14 23:01:51 +05:30
config = SnowflakeV2Config . parse_obj ( default_config_dict )
2022-09-12 23:12:52 +05:30
connect_args = config . get_options ( ) . get ( " connect_args " )
assert connect_args is not None
2022-12-12 19:53:12 +05:30
def test_snowflake_config_with_view_lineage_no_table_lineage_throws_error ( ) :
2023-12-14 23:01:51 +05:30
config_dict = default_config_dict . copy ( )
config_dict [ " include_view_lineage " ] = True
config_dict [ " include_table_lineage " ] = False
with pytest . raises (
ValidationError ,
match = " include_table_lineage must be True for include_view_lineage to be set " ,
) :
SnowflakeV2Config . parse_obj ( config_dict )
2022-12-12 19:53:12 +05:30
def test_snowflake_config_with_column_lineage_no_table_lineage_throws_error ( ) :
2023-12-14 23:01:51 +05:30
config_dict = default_config_dict . copy ( )
config_dict [ " include_column_lineage " ] = True
config_dict [ " include_table_lineage " ] = False
with pytest . raises (
ValidationError ,
match = " include_table_lineage must be True for include_column_lineage to be set " ,
) :
SnowflakeV2Config . parse_obj ( config_dict )
2022-12-12 19:53:12 +05:30
2022-12-28 21:50:37 +05:30
def test_snowflake_config_with_no_connect_args_returns_base_connect_args ( ) :
2023-12-14 23:01:51 +05:30
config : SnowflakeV2Config = SnowflakeV2Config . parse_obj ( default_config_dict )
2022-12-28 21:50:37 +05:30
assert config . get_options ( ) [ " connect_args " ] is not None
assert config . get_options ( ) [ " connect_args " ] == {
CLIENT_PREFETCH_THREADS : 10 ,
CLIENT_SESSION_KEEP_ALIVE : True ,
}
2023-04-05 06:16:07 +05:30
def test_private_key_set_but_auth_not_changed ( ) :
2023-12-14 23:01:51 +05:30
with pytest . raises (
ValidationError ,
match = " Either `private_key` and `private_key_path` is set but `authentication_type` is DEFAULT_AUTHENTICATOR. Should be set to ' KEY_PAIR_AUTHENTICATOR ' when using key pair authentication " ,
) :
2023-04-05 06:16:07 +05:30
SnowflakeV2Config . parse_obj (
{
" account_id " : " acctname " ,
" private_key_path " : " /a/random/path " ,
}
)
2022-12-28 21:50:37 +05:30
def test_snowflake_config_with_connect_args_overrides_base_connect_args ( ) :
2023-12-14 23:01:51 +05:30
config_dict = default_config_dict . copy ( )
config_dict [ " connect_args " ] = {
CLIENT_PREFETCH_THREADS : 5 ,
}
config : SnowflakeV2Config = SnowflakeV2Config . parse_obj ( config_dict )
2022-12-28 21:50:37 +05:30
assert config . get_options ( ) [ " connect_args " ] is not None
assert config . get_options ( ) [ " connect_args " ] [ CLIENT_PREFETCH_THREADS ] == 5
assert config . get_options ( ) [ " connect_args " ] [ CLIENT_SESSION_KEEP_ALIVE ] is True
2022-09-12 23:12:52 +05:30
@patch ( " snowflake.connector.connect " )
def test_test_connection_failure ( mock_connect ) :
mock_connect . side_effect = Exception ( " Failed to connect to snowflake " )
2023-12-14 23:01:51 +05:30
report = test_connection_helpers . run_test_connection (
SnowflakeV2Source , default_config_dict
)
test_connection_helpers . assert_basic_connectivity_failure (
report , " Failed to connect to snowflake "
)
2022-09-12 23:12:52 +05:30
@patch ( " snowflake.connector.connect " )
def test_test_connection_basic_success ( mock_connect ) :
2023-12-14 23:01:51 +05:30
report = test_connection_helpers . run_test_connection (
SnowflakeV2Source , default_config_dict
)
test_connection_helpers . assert_basic_connectivity_success ( report )
2022-09-12 23:12:52 +05:30
def setup_mock_connect ( mock_connect , query_results = None ) :
def default_query_results ( query ) :
if query == " select current_role() " :
return [ ( " TEST_ROLE " , ) ]
elif query == " select current_secondary_roles() " :
return [ ( ' { " roles " : " " , " value " : " " } ' , ) ]
elif query == " select current_warehouse() " :
return [ ( " TEST_WAREHOUSE " ) ]
2023-11-02 09:39:08 -07:00
raise ValueError ( f " Unexpected query: { query } " )
2022-09-12 23:12:52 +05:30
connection_mock = MagicMock ( )
cursor_mock = MagicMock ( )
cursor_mock . execute . side_effect = (
query_results if query_results is not None else default_query_results
)
connection_mock . cursor . return_value = cursor_mock
mock_connect . return_value = connection_mock
@patch ( " snowflake.connector.connect " )
def test_test_connection_no_warehouse ( mock_connect ) :
def query_results ( query ) :
if query == " select current_role() " :
return [ ( " TEST_ROLE " , ) ]
elif query == " select current_secondary_roles() " :
return [ ( ' { " roles " : " " , " value " : " " } ' , ) ]
elif query == " select current_warehouse() " :
return [ ( None , ) ]
elif query == ' show grants to role " TEST_ROLE " ' :
return [
( " " , " USAGE " , " DATABASE " , " DB1 " ) ,
( " " , " USAGE " , " SCHEMA " , " DB1.SCHEMA1 " ) ,
( " " , " REFERENCES " , " TABLE " , " DB1.SCHEMA1.TABLE1 " ) ,
]
elif query == ' show grants to role " PUBLIC " ' :
return [ ]
2023-11-02 09:39:08 -07:00
raise ValueError ( f " Unexpected query: { query } " )
2022-09-12 23:12:52 +05:30
setup_mock_connect ( mock_connect , query_results )
2023-12-14 23:01:51 +05:30
report = test_connection_helpers . run_test_connection (
SnowflakeV2Source , default_config_dict
)
test_connection_helpers . assert_basic_connectivity_success ( report )
test_connection_helpers . assert_capability_report (
capability_report = report . capability_report ,
success_capabilities = [ SourceCapability . CONTAINERS ] ,
failure_capabilities = {
SourceCapability . SCHEMA_METADATA : " Current role TEST_ROLE does not have permissions to use warehouse "
} ,
2022-12-28 21:50:37 +05:30
)
2022-09-12 23:12:52 +05:30
@patch ( " snowflake.connector.connect " )
def test_test_connection_capability_schema_failure ( mock_connect ) :
def query_results ( query ) :
if query == " select current_role() " :
return [ ( " TEST_ROLE " , ) ]
elif query == " select current_secondary_roles() " :
return [ ( ' { " roles " : " " , " value " : " " } ' , ) ]
elif query == " select current_warehouse() " :
return [ ( " TEST_WAREHOUSE " , ) ]
elif query == ' show grants to role " TEST_ROLE " ' :
return [ ( " " , " USAGE " , " DATABASE " , " DB1 " ) ]
elif query == ' show grants to role " PUBLIC " ' :
return [ ]
2023-11-02 09:39:08 -07:00
raise ValueError ( f " Unexpected query: { query } " )
2022-09-12 23:12:52 +05:30
setup_mock_connect ( mock_connect , query_results )
2023-12-14 23:01:51 +05:30
report = test_connection_helpers . run_test_connection (
SnowflakeV2Source , default_config_dict
)
test_connection_helpers . assert_basic_connectivity_success ( report )
test_connection_helpers . assert_capability_report (
capability_report = report . capability_report ,
success_capabilities = [ SourceCapability . CONTAINERS ] ,
failure_capabilities = {
SourceCapability . SCHEMA_METADATA : " Either no tables exist or current role does not have permissions to access them "
} ,
2022-09-12 23:12:52 +05:30
)
@patch ( " snowflake.connector.connect " )
def test_test_connection_capability_schema_success ( mock_connect ) :
def query_results ( query ) :
if query == " select current_role() " :
return [ ( " TEST_ROLE " , ) ]
elif query == " select current_secondary_roles() " :
return [ ( ' { " roles " : " " , " value " : " " } ' , ) ]
elif query == " select current_warehouse() " :
return [ ( " TEST_WAREHOUSE " ) ]
elif query == ' show grants to role " TEST_ROLE " ' :
return [
[ " " , " USAGE " , " DATABASE " , " DB1 " ] ,
[ " " , " USAGE " , " SCHEMA " , " DB1.SCHEMA1 " ] ,
[ " " , " REFERENCES " , " TABLE " , " DB1.SCHEMA1.TABLE1 " ] ,
]
elif query == ' show grants to role " PUBLIC " ' :
return [ ]
2023-11-02 09:39:08 -07:00
raise ValueError ( f " Unexpected query: { query } " )
2022-09-12 23:12:52 +05:30
setup_mock_connect ( mock_connect , query_results )
2023-12-14 23:01:51 +05:30
report = test_connection_helpers . run_test_connection (
SnowflakeV2Source , default_config_dict
)
test_connection_helpers . assert_basic_connectivity_success ( report )
test_connection_helpers . assert_capability_report (
capability_report = report . capability_report ,
success_capabilities = [
SourceCapability . CONTAINERS ,
SourceCapability . SCHEMA_METADATA ,
SourceCapability . DESCRIPTIONS ,
] ,
)
2022-09-12 23:12:52 +05:30
@patch ( " snowflake.connector.connect " )
def test_test_connection_capability_all_success ( mock_connect ) :
def query_results ( query ) :
if query == " select current_role() " :
return [ ( " TEST_ROLE " , ) ]
elif query == " select current_secondary_roles() " :
return [ ( ' { " roles " : " " , " value " : " " } ' , ) ]
elif query == " select current_warehouse() " :
return [ ( " TEST_WAREHOUSE " ) ]
elif query == ' show grants to role " TEST_ROLE " ' :
return [
( " " , " USAGE " , " DATABASE " , " DB1 " ) ,
( " " , " USAGE " , " SCHEMA " , " DB1.SCHEMA1 " ) ,
( " " , " SELECT " , " TABLE " , " DB1.SCHEMA1.TABLE1 " ) ,
( " " , " USAGE " , " ROLE " , " TEST_USAGE_ROLE " ) ,
]
elif query == ' show grants to role " PUBLIC " ' :
return [ ]
elif query == ' show grants to role " TEST_USAGE_ROLE " ' :
return [
[ " " , " USAGE " , " DATABASE " , " SNOWFLAKE " ] ,
[ " " , " USAGE " , " SCHEMA " , " ACCOUNT_USAGE " ] ,
[ " " , " USAGE " , " VIEW " , " SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY " ] ,
[ " " , " USAGE " , " VIEW " , " SNOWFLAKE.ACCOUNT_USAGE.ACCESS_HISTORY " ] ,
[ " " , " USAGE " , " VIEW " , " SNOWFLAKE.ACCOUNT_USAGE.OBJECT_DEPENDENCIES " ] ,
]
2023-11-02 09:39:08 -07:00
raise ValueError ( f " Unexpected query: { query } " )
2022-09-12 23:12:52 +05:30
setup_mock_connect ( mock_connect , query_results )
2023-12-14 23:01:51 +05:30
report = test_connection_helpers . run_test_connection (
SnowflakeV2Source , default_config_dict
)
test_connection_helpers . assert_basic_connectivity_success ( report )
test_connection_helpers . assert_capability_report (
capability_report = report . capability_report ,
success_capabilities = [
SourceCapability . CONTAINERS ,
SourceCapability . SCHEMA_METADATA ,
SourceCapability . DATA_PROFILING ,
SourceCapability . DESCRIPTIONS ,
SourceCapability . LINEAGE_COARSE ,
] ,
)
2022-12-17 00:30:42 +05:30
def test_aws_cloud_region_from_snowflake_region_id ( ) :
(
cloud ,
cloud_region_id ,
2024-07-12 15:08:51 -07:00
) = SnowsightUrlBuilder . get_cloud_region_from_snowflake_region_id (
" aws_ca_central_1 "
)
2022-12-17 00:30:42 +05:30
assert cloud == SnowflakeCloudProvider . AWS
assert cloud_region_id == " ca-central-1 "
(
cloud ,
cloud_region_id ,
2024-07-12 15:08:51 -07:00
) = SnowsightUrlBuilder . get_cloud_region_from_snowflake_region_id (
" aws_us_east_1_gov "
)
2022-12-17 00:30:42 +05:30
assert cloud == SnowflakeCloudProvider . AWS
assert cloud_region_id == " us-east-1 "
def test_google_cloud_region_from_snowflake_region_id ( ) :
(
cloud ,
cloud_region_id ,
2024-07-12 15:08:51 -07:00
) = SnowsightUrlBuilder . get_cloud_region_from_snowflake_region_id (
" gcp_europe_west2 "
)
2022-12-17 00:30:42 +05:30
assert cloud == SnowflakeCloudProvider . GCP
assert cloud_region_id == " europe-west2 "
def test_azure_cloud_region_from_snowflake_region_id ( ) :
(
cloud ,
cloud_region_id ,
2024-07-12 15:08:51 -07:00
) = SnowsightUrlBuilder . get_cloud_region_from_snowflake_region_id (
2022-12-17 00:30:42 +05:30
" azure_switzerlandnorth "
)
assert cloud == SnowflakeCloudProvider . AZURE
2023-07-12 17:11:33 +05:30
assert cloud_region_id == " switzerland-north "
2022-12-17 00:30:42 +05:30
(
cloud ,
cloud_region_id ,
2024-07-12 15:08:51 -07:00
) = SnowsightUrlBuilder . get_cloud_region_from_snowflake_region_id (
2022-12-17 00:30:42 +05:30
" azure_centralindia "
)
assert cloud == SnowflakeCloudProvider . AZURE
2023-07-12 17:11:33 +05:30
assert cloud_region_id == " central-india "
2022-12-17 00:30:42 +05:30
def test_unknown_cloud_region_from_snowflake_region_id ( ) :
2023-12-14 23:01:51 +05:30
with pytest . raises ( Exception , match = " Unknown snowflake region " ) :
2024-07-12 15:08:51 -07:00
SnowsightUrlBuilder . get_cloud_region_from_snowflake_region_id (
2022-12-17 00:30:42 +05:30
" somecloud_someregion "
)
2023-03-16 04:55:52 -07:00
def test_snowflake_object_access_entry_missing_object_id ( ) :
SnowflakeObjectAccessEntry (
* * {
" columns " : [
{ " columnName " : " A " } ,
{ " columnName " : " B " } ,
] ,
" objectDomain " : " View " ,
" objectName " : " SOME.OBJECT.NAME " ,
}
)
2023-05-09 02:43:57 +05:30
def test_snowflake_query_create_deny_regex_sql ( ) :
assert create_deny_regex_sql_filter ( [ ] , [ " col " ] ) == " "
assert (
create_deny_regex_sql_filter ( [ " .*tmp.* " ] , [ " col " ] )
== " NOT RLIKE(col, ' .*tmp.* ' , ' i ' ) "
)
assert (
create_deny_regex_sql_filter ( [ " .*tmp.* " , UUID_REGEX ] , [ " col " ] )
== " NOT RLIKE(col, ' .*tmp.* ' , ' i ' ) AND NOT RLIKE(col, ' [a-f0-9] {8} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {12} ' , ' i ' ) "
)
assert (
create_deny_regex_sql_filter ( [ " .*tmp.* " , UUID_REGEX ] , [ " col1 " , " col2 " ] )
== " NOT RLIKE(col1, ' .*tmp.* ' , ' i ' ) AND NOT RLIKE(col1, ' [a-f0-9] {8} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {12} ' , ' i ' ) AND NOT RLIKE(col2, ' .*tmp.* ' , ' i ' ) AND NOT RLIKE(col2, ' [a-f0-9] {8} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {12} ' , ' i ' ) "
)
assert (
2024-07-12 15:08:51 -07:00
create_deny_regex_sql_filter (
DEFAULT_TEMP_TABLES_PATTERNS , [ " upstream_table_name " ]
)
== r " NOT RLIKE(upstream_table_name, ' .* \ .FIVETRAN_.*_STAGING \ ..* ' , ' i ' ) AND NOT RLIKE(upstream_table_name, ' .*__DBT_TMP$ ' , ' i ' ) AND NOT RLIKE(upstream_table_name, ' .* \ .SEGMENT_[a-f0-9] {8} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {12} ' , ' i ' ) AND NOT RLIKE(upstream_table_name, ' .* \ .STAGING_.*_[a-f0-9] {8} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {4} [-_][a-f0-9] {12} ' , ' i ' ) AND NOT RLIKE(upstream_table_name, ' .* \ .(GE_TMP_|GE_TEMP_|GX_TEMP_)[0-9A-F] {8} ' , ' i ' ) "
2023-05-09 02:43:57 +05:30
)
2023-06-28 20:29:01 +05:30
def test_snowflake_temporary_patterns_config_rename ( ) :
conf = SnowflakeV2Config . parse_obj (
{
" account_id " : " test " ,
" username " : " user " ,
" password " : " password " ,
" upstreams_deny_pattern " : [ " .*tmp.* " ] ,
}
)
assert conf . temporary_tables_pattern == [ " .*tmp.* " ]
2023-11-23 09:45:25 +01:00
def test_email_filter_query_generation_with_one_deny ( ) :
email_filter = AllowDenyPattern ( deny = [ " .*@example.com " ] )
filter_query = SnowflakeQuery . gen_email_filter_query ( email_filter )
assert filter_query == " AND NOT (rlike(user_name, ' .*@example.com ' , ' i ' )) "
def test_email_filter_query_generation_without_any_filter ( ) :
email_filter = AllowDenyPattern ( )
filter_query = SnowflakeQuery . gen_email_filter_query ( email_filter )
assert filter_query == " "
def test_email_filter_query_generation_one_allow ( ) :
email_filter = AllowDenyPattern ( allow = [ " .*@example.com " ] )
filter_query = SnowflakeQuery . gen_email_filter_query ( email_filter )
assert filter_query == " AND (rlike(user_name, ' .*@example.com ' , ' i ' )) "
def test_email_filter_query_generation_one_allow_and_deny ( ) :
email_filter = AllowDenyPattern (
allow = [ " .*@example.com " , " .*@example2.com " ] ,
deny = [ " .*@example2.com " , " .*@example4.com " ] ,
)
filter_query = SnowflakeQuery . gen_email_filter_query ( email_filter )
assert (
filter_query
== " AND (rlike(user_name, ' .*@example.com ' , ' i ' ) OR rlike(user_name, ' .*@example2.com ' , ' i ' )) AND NOT (rlike(user_name, ' .*@example2.com ' , ' i ' ) OR rlike(user_name, ' .*@example4.com ' , ' i ' )) "
)
def test_email_filter_query_generation_with_case_insensitive_filter ( ) :
email_filter = AllowDenyPattern (
allow = [ " .*@example.com " ] , deny = [ " .*@example2.com " ] , ignoreCase = False
)
filter_query = SnowflakeQuery . gen_email_filter_query ( email_filter )
assert (
filter_query
== " AND (rlike(user_name, ' .*@example.com ' , ' c ' )) AND NOT (rlike(user_name, ' .*@example2.com ' , ' c ' )) "
)
2023-12-18 19:54:31 +01:00
def test_create_snowsight_base_url_us_west ( ) :
2024-07-12 15:08:51 -07:00
result = SnowsightUrlBuilder (
" account_locator " , " aws_us_west_2 " , privatelink = False
) . snowsight_base_url
2023-12-18 19:54:31 +01:00
assert result == " https://app.snowflake.com/us-west-2/account_locator/ "
def test_create_snowsight_base_url_ap_northeast_1 ( ) :
2024-07-12 15:08:51 -07:00
result = SnowsightUrlBuilder (
" account_locator " , " aws_ap_northeast_1 " , privatelink = False
) . snowsight_base_url
2023-12-18 19:54:31 +01:00
assert result == " https://app.snowflake.com/ap-northeast-1.aws/account_locator/ "