2023-08-29 11:16:32 +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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Test Couchbase using the topology
|
|
|
|
"""
|
|
|
|
|
|
|
|
from unittest import TestCase
|
|
|
|
from unittest.mock import patch
|
|
|
|
|
|
|
|
from metadata.generated.schema.api.data.createTable import CreateTableRequest
|
|
|
|
from metadata.generated.schema.entity.data.database import Database
|
|
|
|
from metadata.generated.schema.entity.data.databaseSchema import DatabaseSchema
|
|
|
|
from metadata.generated.schema.entity.data.table import Column, DataType, TableType
|
|
|
|
from metadata.generated.schema.entity.services.databaseService import (
|
|
|
|
DatabaseConnection,
|
|
|
|
DatabaseService,
|
|
|
|
DatabaseServiceType,
|
|
|
|
)
|
|
|
|
from metadata.generated.schema.metadataIngestion.workflow import (
|
|
|
|
OpenMetadataWorkflowConfig,
|
|
|
|
)
|
|
|
|
from metadata.generated.schema.type.entityReference import EntityReference
|
2023-10-04 09:14:03 +02:00
|
|
|
from metadata.ingestion.ometa.ometa_api import OpenMetadata
|
2024-12-12 15:12:55 +05:30
|
|
|
from metadata.ingestion.source.database.common_nosql_source import TableNameAndType
|
2023-08-29 11:16:32 +05:30
|
|
|
from metadata.ingestion.source.database.couchbase.metadata import CouchbaseSource
|
|
|
|
|
|
|
|
mock_couch_config = {
|
|
|
|
"source": {
|
|
|
|
"type": "couchbase",
|
|
|
|
"serviceName": "local_couchbase",
|
|
|
|
"serviceConnection": {
|
|
|
|
"config": {
|
|
|
|
"type": "Couchbase",
|
|
|
|
"bucket": "default",
|
|
|
|
"username": "username",
|
|
|
|
"password": "password",
|
|
|
|
"hostport": "localhost",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"sourceConfig": {"config": {"type": "DatabaseMetadata"}},
|
|
|
|
},
|
|
|
|
"sink": {"type": "metadata-rest", "config": {}},
|
|
|
|
"workflowConfig": {
|
|
|
|
"openMetadataServerConfig": {
|
|
|
|
"hostPort": "http://localhost:8585/api",
|
|
|
|
"authProvider": "openmetadata",
|
2025-01-02 13:07:55 +05:30
|
|
|
"securityConfig": {"jwtToken": "couchbase"},
|
2023-08-29 11:16:32 +05:30
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
MOCK_DATABASE_SERVICE = DatabaseService(
|
|
|
|
id="85811038-099a-11ed-861d-0242ac120002",
|
|
|
|
name="local_couchbase",
|
|
|
|
connection=DatabaseConnection(),
|
|
|
|
serviceType=DatabaseServiceType.Couchbase,
|
|
|
|
)
|
|
|
|
|
|
|
|
MOCK_DATABASE = Database(
|
|
|
|
id="2aaa012e-099a-11ed-861d-0242ac120002",
|
|
|
|
name="default",
|
|
|
|
fullyQualifiedName="local_couchbase.default",
|
|
|
|
displayName="default",
|
|
|
|
description="",
|
|
|
|
service=EntityReference(
|
|
|
|
id="85811038-099a-11ed-861d-0242ac120002",
|
|
|
|
type="databaseService",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
MOCK_DATABASE_SCHEMA = DatabaseSchema(
|
|
|
|
id="2aaa012e-099a-11ed-861d-0242ac120056",
|
|
|
|
name="default",
|
|
|
|
fullyQualifiedName="local_couchbase.default.default",
|
|
|
|
displayName="default",
|
|
|
|
description="",
|
|
|
|
database=EntityReference(
|
|
|
|
id="2aaa012e-099a-11ed-861d-0242ac120002",
|
|
|
|
type="database",
|
|
|
|
),
|
|
|
|
service=EntityReference(
|
|
|
|
id="85811038-099a-11ed-861d-0242ac120002",
|
|
|
|
type="databaseService",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
MOCK_JSON_TABLE_DATA = [
|
|
|
|
{
|
|
|
|
"name": "mayur",
|
|
|
|
"age": 25,
|
|
|
|
"is_married": False,
|
|
|
|
"address": {"line": "random address"},
|
|
|
|
},
|
|
|
|
{"name": "onkar", "age": 26, "is_married": True},
|
|
|
|
]
|
|
|
|
|
|
|
|
MOCK_CREATE_TABLE = [
|
|
|
|
CreateTableRequest(
|
|
|
|
name="random_table",
|
|
|
|
tableType=TableType.Regular,
|
|
|
|
columns=[
|
|
|
|
Column(
|
|
|
|
name="name",
|
|
|
|
displayName="name",
|
|
|
|
dataType=DataType.STRING,
|
|
|
|
dataTypeDisplay=DataType.STRING.value,
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
name="age",
|
|
|
|
displayName="age",
|
|
|
|
dataType=DataType.INT,
|
|
|
|
dataTypeDisplay=DataType.INT.value,
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
name="is_married",
|
|
|
|
displayName="is_married",
|
|
|
|
dataType=DataType.BOOLEAN,
|
|
|
|
dataTypeDisplay=DataType.BOOLEAN.value,
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
name="address",
|
|
|
|
displayName="address",
|
2023-10-12 14:51:38 +02:00
|
|
|
dataType=DataType.JSON,
|
|
|
|
dataTypeDisplay=DataType.JSON.value,
|
2023-08-29 11:16:32 +05:30
|
|
|
children=[
|
|
|
|
Column(
|
|
|
|
name="line",
|
2023-10-08 20:08:51 +05:30
|
|
|
displayName="line",
|
2023-08-29 11:16:32 +05:30
|
|
|
dataType=DataType.STRING,
|
|
|
|
dataTypeDisplay=DataType.STRING.value,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
tableConstraints=None,
|
|
|
|
databaseSchema="local_couchbase.default.default",
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
EXPECTED_DATABASE_NAMES = ["default"]
|
|
|
|
|
|
|
|
EXPECTED_DATABASE_SCHEMA_NAMES = [
|
|
|
|
"random_schema",
|
|
|
|
"random1_schema",
|
|
|
|
]
|
|
|
|
|
|
|
|
MOCK_DATABASE_SCHEMA_NAMES = [
|
|
|
|
"random_schema",
|
|
|
|
"random1_schema",
|
|
|
|
]
|
|
|
|
|
|
|
|
EXPECTED_TABLE_NAMES = [
|
|
|
|
("random_table", TableType.Regular),
|
|
|
|
("random1_table", TableType.Regular),
|
|
|
|
]
|
|
|
|
|
|
|
|
MOCK_TABLE_NAMES = [
|
2024-12-12 15:12:55 +05:30
|
|
|
TableNameAndType(name="random_table"),
|
|
|
|
TableNameAndType(name="random1_table"),
|
2023-08-29 11:16:32 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def custom_column_compare(self, other):
|
|
|
|
return (
|
|
|
|
self.name == other.name
|
|
|
|
and self.description == other.description
|
|
|
|
and self.children == other.children
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CouchbaseUnitTest(TestCase):
|
|
|
|
@patch(
|
|
|
|
"metadata.ingestion.source.database.couchbase.metadata.CouchbaseSource.test_connection"
|
|
|
|
)
|
|
|
|
@patch("metadata.ingestion.source.database.couchbase.connection.get_connection")
|
|
|
|
def __init__(self, methodName, get_connection, test_connection) -> None:
|
|
|
|
super().__init__(methodName)
|
|
|
|
get_connection.return_value = False
|
|
|
|
test_connection.return_value = False
|
|
|
|
|
2024-06-07 04:36:17 +02:00
|
|
|
self.config = OpenMetadataWorkflowConfig.model_validate(mock_couch_config)
|
2023-08-29 11:16:32 +05:30
|
|
|
self.couch_source = CouchbaseSource.create(
|
|
|
|
mock_couch_config["source"],
|
2023-10-04 09:14:03 +02:00
|
|
|
OpenMetadata(self.config.workflowConfig.openMetadataServerConfig),
|
2023-08-29 11:16:32 +05:30
|
|
|
)
|
2024-03-25 18:20:40 +01:00
|
|
|
self.couch_source.context.get().__dict__[
|
2023-11-27 16:15:47 +05:30
|
|
|
"database_service"
|
2024-06-05 21:18:37 +02:00
|
|
|
] = MOCK_DATABASE_SERVICE.name.root
|
|
|
|
self.couch_source.context.get().__dict__["database"] = MOCK_DATABASE.name.root
|
2024-03-25 18:20:40 +01:00
|
|
|
self.couch_source.context.get().__dict__[
|
2023-11-27 16:15:47 +05:30
|
|
|
"database_schema"
|
2024-06-05 21:18:37 +02:00
|
|
|
] = MOCK_DATABASE_SCHEMA.name.root
|
2023-08-29 11:16:32 +05:30
|
|
|
|
|
|
|
def test_database_names(self):
|
|
|
|
assert EXPECTED_DATABASE_NAMES == list(self.couch_source.get_database_names())
|
|
|
|
|
|
|
|
def test_database_schema_names(self):
|
|
|
|
with patch.object(
|
|
|
|
CouchbaseSource,
|
|
|
|
"get_schema_name_list",
|
|
|
|
return_value=MOCK_DATABASE_SCHEMA_NAMES,
|
|
|
|
):
|
|
|
|
assert EXPECTED_DATABASE_SCHEMA_NAMES == list(
|
|
|
|
self.couch_source.get_database_schema_names()
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_table_names(self):
|
|
|
|
with patch.object(
|
2024-12-12 15:12:55 +05:30
|
|
|
CouchbaseSource,
|
|
|
|
"query_table_names_and_types",
|
|
|
|
return_value=MOCK_TABLE_NAMES,
|
2023-08-29 11:16:32 +05:30
|
|
|
):
|
|
|
|
assert EXPECTED_TABLE_NAMES == list(
|
|
|
|
self.couch_source.get_tables_name_and_type()
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_yield_tables(self):
|
|
|
|
Column.__eq__ = custom_column_compare
|
|
|
|
with patch.object(
|
|
|
|
CouchbaseSource, "get_table_columns_dict", return_value=MOCK_JSON_TABLE_DATA
|
|
|
|
):
|
2023-08-30 15:49:42 +02:00
|
|
|
assert MOCK_CREATE_TABLE == [
|
|
|
|
either.right
|
|
|
|
for either in self.couch_source.yield_table(EXPECTED_TABLE_NAMES[0])
|
|
|
|
]
|