2022-10-10 11:36:20 +02:00
|
|
|
# Copyright 2022 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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Test database connectors with CLI
|
|
|
|
"""
|
|
|
|
from abc import abstractmethod
|
2023-04-18 11:56:16 +02:00
|
|
|
from typing import List, Optional
|
2022-10-10 11:36:20 +02:00
|
|
|
from unittest import TestCase
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from metadata.generated.schema.entity.data.table import Table
|
|
|
|
from metadata.ingestion.api.sink import SinkStatus
|
|
|
|
from metadata.ingestion.api.source import SourceStatus
|
|
|
|
|
2023-04-18 11:56:16 +02:00
|
|
|
from .e2e_types import E2EType
|
|
|
|
from .test_cli import CliBase
|
2022-10-10 11:36:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CliDBBase(TestCase):
|
2022-12-20 18:00:26 +05:30
|
|
|
"""
|
|
|
|
CLI DB Base class
|
|
|
|
"""
|
|
|
|
|
2023-04-14 15:58:31 +02:00
|
|
|
class TestSuite(TestCase, CliBase): # pylint: disable=too-many-public-methods
|
2022-12-20 18:00:26 +05:30
|
|
|
"""
|
|
|
|
TestSuite class to define test structure
|
|
|
|
"""
|
|
|
|
|
2022-10-10 11:36:20 +02:00
|
|
|
@pytest.mark.order(1)
|
|
|
|
def test_vanilla_ingestion(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""1. Deploy vanilla ingestion"""
|
2022-10-10 11:36:20 +02:00
|
|
|
# build config file for ingest
|
|
|
|
self.build_config_file(E2EType.INGEST)
|
|
|
|
# run ingest with new tables
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_for_vanilla_ingestion(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(2)
|
|
|
|
def test_create_table_with_profiler(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""2. create a new table + deploy ingestion with views, sample data, and profiler.
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. delete table in case it exists
|
|
|
|
2. create a table and a view
|
|
|
|
3. build config file for ingest
|
|
|
|
4. run ingest with new tables `self.run_command()` defaults to `ingestion`
|
|
|
|
5. build config file for profiler
|
|
|
|
6. run profiler with new tables
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.delete_table_and_view()
|
|
|
|
self.create_table_and_view()
|
|
|
|
self.build_config_file()
|
|
|
|
self.run_command()
|
|
|
|
self.build_config_file(E2EType.PROFILER)
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command("profile")
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_for_table_with_profiler(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(3)
|
|
|
|
def test_delete_table_is_marked_as_deleted(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""3. delete the new table + deploy marking tables as deleted
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. delete table created in previous test
|
|
|
|
2. build config file for ingest
|
|
|
|
3. run ingest `self.run_command()` defaults to `ingestion`
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.delete_table_and_view()
|
|
|
|
self.build_config_file()
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-31 18:12:26 +05:30
|
|
|
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_for_delete_table_is_marked_as_deleted(
|
|
|
|
source_status, sink_status
|
|
|
|
)
|
|
|
|
|
|
|
|
@pytest.mark.order(4)
|
|
|
|
def test_schema_filter_includes(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""4. vanilla ingestion + include schema filter pattern
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. build config file for ingest with filters
|
|
|
|
2. run ingest `self.run_command()` defaults to `ingestion`
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.build_config_file(
|
2023-04-14 15:58:31 +02:00
|
|
|
E2EType.INGEST_DB_FILTER_SCHEMA,
|
|
|
|
{"includes": self.get_includes_schemas()},
|
2022-10-10 11:36:20 +02:00
|
|
|
)
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-31 18:12:26 +05:30
|
|
|
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_filtered_schemas_includes(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(5)
|
|
|
|
def test_schema_filter_excludes(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""5. vanilla ingestion + exclude schema filter pattern
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. build config file for ingest with filters
|
|
|
|
2. run ingest `self.run_command()` defaults to `ingestion`
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.build_config_file(
|
2023-04-14 15:58:31 +02:00
|
|
|
E2EType.INGEST_DB_FILTER_SCHEMA,
|
|
|
|
{"excludes": self.get_includes_schemas()},
|
2022-10-10 11:36:20 +02:00
|
|
|
)
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_filtered_schemas_excludes(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(6)
|
|
|
|
def test_table_filter_includes(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""6. Vanilla ingestion + include table filter pattern
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. build config file for ingest with filters
|
|
|
|
2. run ingest `self.run_command()` defaults to `ingestion`
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.build_config_file(
|
2023-04-14 15:58:31 +02:00
|
|
|
E2EType.INGEST_DB_FILTER_TABLE, {"includes": self.get_includes_tables()}
|
2022-10-10 11:36:20 +02:00
|
|
|
)
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-31 18:12:26 +05:30
|
|
|
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_filtered_tables_includes(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(7)
|
|
|
|
def test_table_filter_excludes(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""7. Vanilla ingestion + exclude table filter pattern
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. build config file for ingest with filters
|
|
|
|
2. run ingest `self.run_command()` defaults to `ingestion`
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.build_config_file(
|
2023-04-14 15:58:31 +02:00
|
|
|
E2EType.INGEST_DB_FILTER_TABLE, {"excludes": self.get_includes_tables()}
|
2022-10-10 11:36:20 +02:00
|
|
|
)
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_filtered_tables_excludes(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(8)
|
|
|
|
def test_table_filter_mix(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""8. Vanilla ingestion + include schema filter pattern + exclude table filter pattern
|
|
|
|
|
|
|
|
We will perform the following steps:
|
|
|
|
1. build config file for ingest with filters
|
|
|
|
2. run ingest `self.run_command()` defaults to `ingestion`
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
self.build_config_file(
|
2023-04-14 15:58:31 +02:00
|
|
|
E2EType.INGEST_DB_FILTER_MIX,
|
2022-10-10 11:36:20 +02:00
|
|
|
{
|
|
|
|
"schema": {"includes": self.get_includes_schemas()},
|
|
|
|
"table": {
|
|
|
|
"includes": self.get_includes_tables(),
|
|
|
|
"excludes": self.get_excludes_tables(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
2023-01-24 14:18:56 +01:00
|
|
|
result = self.run_command()
|
2022-10-10 11:36:20 +02:00
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_filtered_mix(source_status, sink_status)
|
|
|
|
|
|
|
|
@pytest.mark.order(9)
|
|
|
|
def test_usage(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""9. Run queries in the source (creates, inserts, views) and ingest metadata & Lineage
|
|
|
|
|
|
|
|
This test will need to be implemented on the database specific test classes
|
|
|
|
"""
|
2022-10-10 11:36:20 +02:00
|
|
|
|
|
|
|
@pytest.mark.order(10)
|
|
|
|
def test_lineage(self) -> None:
|
2023-04-18 11:56:16 +02:00
|
|
|
"""10. Run queries in the source (creates, inserts, views) and ingest metadata & Lineage
|
|
|
|
|
|
|
|
This test will need to be implemented on the database specific test classes
|
|
|
|
"""
|
|
|
|
|
|
|
|
@pytest.mark.order(11)
|
|
|
|
def test_profiler_with_time_partition(self) -> None:
|
|
|
|
"""11. Test time partitioning for the profiler"""
|
|
|
|
time_partition = self.get_profiler_time_partition()
|
|
|
|
if time_partition:
|
|
|
|
processor_config = self.get_profiler_processor_config(
|
|
|
|
self.get_profiler_time_partition()
|
|
|
|
)
|
|
|
|
self.build_config_file(
|
|
|
|
E2EType.PROFILER_PROCESSOR,
|
|
|
|
{"processor": processor_config},
|
|
|
|
)
|
|
|
|
result = self.run_command("profile")
|
|
|
|
sink_status, source_status = self.retrieve_statuses(result)
|
|
|
|
self.assert_for_table_with_profiler_time_partition(
|
|
|
|
source_status,
|
|
|
|
sink_status,
|
|
|
|
)
|
2022-10-10 11:36:20 +02:00
|
|
|
|
|
|
|
def retrieve_table(self, table_name_fqn: str) -> Table:
|
|
|
|
return self.openmetadata.get_by_name(entity=Table, fqn=table_name_fqn)
|
|
|
|
|
|
|
|
def retrieve_sample_data(self, table_name_fqn: str) -> Table:
|
2022-12-12 15:08:01 +01:00
|
|
|
table: Table = self.openmetadata.get_by_name(
|
|
|
|
entity=Table, fqn=table_name_fqn
|
2022-10-10 11:36:20 +02:00
|
|
|
)
|
2022-12-12 15:08:01 +01:00
|
|
|
return self.openmetadata.get_sample_data(table=table)
|
2022-10-10 11:36:20 +02:00
|
|
|
|
2023-04-18 11:56:16 +02:00
|
|
|
def retrieve_profile(self, table_fqn: str) -> Table:
|
|
|
|
table: Table = self.openmetadata.get_latest_table_profile(fqn=table_fqn)
|
|
|
|
|
|
|
|
return table
|
|
|
|
|
2023-04-14 15:58:31 +02:00
|
|
|
def retrieve_lineage(self, entity_fqn: str) -> dict:
|
2022-10-10 11:36:20 +02:00
|
|
|
return self.openmetadata.client.get(
|
2023-04-14 15:58:31 +02:00
|
|
|
f"/lineage/table/name/{entity_fqn}?upstreamDepth=3&downstreamDepth=3"
|
2022-10-10 11:36:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_connector_name() -> str:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def create_table_and_view(self) -> None:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def delete_table_and_view(self) -> None:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_for_vanilla_ingestion(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
) -> None:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_for_table_with_profiler(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-04-18 11:56:16 +02:00
|
|
|
@abstractmethod
|
|
|
|
def assert_for_table_with_profiler_time_partition(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-10-10 11:36:20 +02:00
|
|
|
@abstractmethod
|
|
|
|
def assert_for_delete_table_is_marked_as_deleted(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_filtered_schemas_includes(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_filtered_schemas_excludes(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_filtered_tables_includes(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_filtered_tables_excludes(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def assert_filtered_mix(
|
|
|
|
self, source_status: SourceStatus, sink_status: SinkStatus
|
|
|
|
):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_includes_schemas() -> List[str]:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_includes_tables() -> List[str]:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def get_excludes_tables() -> List[str]:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-04-18 11:56:16 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_profiler_time_partition() -> Optional[dict]:
|
|
|
|
return None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_profiler_time_partition_results() -> Optional[dict]:
|
|
|
|
return None
|
|
|
|
|
2022-10-10 11:36:20 +02:00
|
|
|
@staticmethod
|
2023-04-14 15:58:31 +02:00
|
|
|
def get_test_type() -> str:
|
|
|
|
return "database"
|
2023-04-18 11:56:16 +02:00
|
|
|
|
|
|
|
def get_profiler_processor_config(self, config: dict) -> dict:
|
|
|
|
return {
|
|
|
|
"processor": {
|
|
|
|
"type": "orm-profiler",
|
|
|
|
"config": {"tableConfig": [config]},
|
|
|
|
}
|
|
|
|
}
|