mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2025-07-10 18:49:00 +00:00

* percona server for postgresql support The only meaningful difference is version string in percona server for postgresql. So commit propose universal and safe way to detect server version by integer string, not complicated parsing of unformatted string. * updated tests with get_server_version_num commented outdated tests --------- Co-authored-by: Sriharsha Chintalapani <harshach@users.noreply.github.com>
339 lines
10 KiB
Python
339 lines
10 KiB
Python
# 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 Postgres using the topology
|
|
"""
|
|
|
|
import types
|
|
from unittest import TestCase
|
|
from unittest.mock import patch
|
|
|
|
from sqlalchemy.types import VARCHAR
|
|
|
|
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, Constraint, DataType
|
|
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
|
|
from metadata.ingestion.source.database.common_pg_mappings import (
|
|
GEOMETRY,
|
|
POINT,
|
|
POLYGON,
|
|
)
|
|
from metadata.ingestion.source.database.postgres.metadata import PostgresSource
|
|
from metadata.ingestion.source.database.postgres.usage import PostgresUsageSource
|
|
from metadata.ingestion.source.database.postgres.utils import get_postgres_version
|
|
|
|
mock_postgres_config = {
|
|
"source": {
|
|
"type": "postgres",
|
|
"serviceName": "local_postgres1",
|
|
"serviceConnection": {
|
|
"config": {
|
|
"type": "Postgres",
|
|
"username": "username",
|
|
"authType": {
|
|
"password": "password",
|
|
},
|
|
"hostPort": "localhost:5432",
|
|
"database": "postgres",
|
|
"sslMode": "verify-ca",
|
|
"sslConfig": {
|
|
"caCertificate": "CA certificate content",
|
|
},
|
|
}
|
|
},
|
|
"sourceConfig": {
|
|
"config": {
|
|
"type": "DatabaseMetadata",
|
|
}
|
|
},
|
|
},
|
|
"sink": {
|
|
"type": "metadata-rest",
|
|
"config": {},
|
|
},
|
|
"workflowConfig": {
|
|
"openMetadataServerConfig": {
|
|
"hostPort": "http://localhost:8585/api",
|
|
"authProvider": "openmetadata",
|
|
"securityConfig": {"jwtToken": "postgres"},
|
|
}
|
|
},
|
|
}
|
|
|
|
mock_postgres_usage_config = {
|
|
"source": {
|
|
"type": "postgres-usage",
|
|
"serviceName": "local_postgres1",
|
|
"serviceConnection": {
|
|
"config": {
|
|
"type": "Postgres",
|
|
"username": "username",
|
|
"authType": {
|
|
"password": "password",
|
|
},
|
|
"hostPort": "localhost:5432",
|
|
"database": "postgres",
|
|
}
|
|
},
|
|
"sourceConfig": {
|
|
"config": {
|
|
"type": "DatabaseUsage",
|
|
"queryLogDuration": 1,
|
|
}
|
|
},
|
|
},
|
|
"sink": {
|
|
"type": "metadata-rest",
|
|
"config": {},
|
|
},
|
|
"workflowConfig": {
|
|
"openMetadataServerConfig": {
|
|
"hostPort": "http://localhost:8585/api",
|
|
"authProvider": "openmetadata",
|
|
"securityConfig": {"jwtToken": "postgres"},
|
|
}
|
|
},
|
|
}
|
|
|
|
MOCK_DATABASE_SERVICE = DatabaseService(
|
|
id="85811038-099a-11ed-861d-0242ac120002",
|
|
name="postgres_source",
|
|
connection=DatabaseConnection(),
|
|
serviceType=DatabaseServiceType.Postgres,
|
|
)
|
|
|
|
MOCK_DATABASE = Database(
|
|
id="2aaa012e-099a-11ed-861d-0242ac120002",
|
|
name="118146679784",
|
|
fullyQualifiedName="postgres_source.default",
|
|
displayName="118146679784",
|
|
description="",
|
|
service=EntityReference(
|
|
id="85811038-099a-11ed-861d-0242ac120002",
|
|
type="databaseService",
|
|
),
|
|
)
|
|
|
|
MOCK_DATABASE_SCHEMA = DatabaseSchema(
|
|
id="2aaa012e-099a-11ed-861d-0242ac120056",
|
|
name="default",
|
|
fullyQualifiedName="postgres_source.118146679784.default",
|
|
displayName="default",
|
|
description="",
|
|
database=EntityReference(
|
|
id="2aaa012e-099a-11ed-861d-0242ac120002",
|
|
type="database",
|
|
),
|
|
service=EntityReference(
|
|
id="2aaa012e-099a-11ed-861d-0242ac120002",
|
|
type="database",
|
|
),
|
|
)
|
|
|
|
|
|
MOCK_COLUMN_VALUE = [
|
|
{
|
|
"name": "username",
|
|
"type": VARCHAR(),
|
|
"nullable": True,
|
|
"default": None,
|
|
"autoincrement": False,
|
|
"system_data_type": "varchar(50)",
|
|
"comment": None,
|
|
},
|
|
{
|
|
"name": "geom_c",
|
|
"type": GEOMETRY(),
|
|
"nullable": True,
|
|
"default": None,
|
|
"autoincrement": False,
|
|
"system_data_type": "geometry",
|
|
"comment": None,
|
|
},
|
|
{
|
|
"name": "point_c",
|
|
"type": POINT(),
|
|
"nullable": True,
|
|
"default": None,
|
|
"autoincrement": False,
|
|
"system_data_type": "point",
|
|
"comment": None,
|
|
},
|
|
{
|
|
"name": "polygon_c",
|
|
"type": POLYGON(),
|
|
"nullable": True,
|
|
"default": None,
|
|
"autoincrement": False,
|
|
"comment": None,
|
|
"system_data_type": "polygon",
|
|
},
|
|
]
|
|
|
|
|
|
EXPECTED_COLUMN_VALUE = [
|
|
Column(
|
|
name="username",
|
|
displayName=None,
|
|
dataType=DataType.VARCHAR,
|
|
arrayDataType=None,
|
|
dataLength=1,
|
|
precision=None,
|
|
scale=None,
|
|
dataTypeDisplay="varchar(50)",
|
|
description=None,
|
|
fullyQualifiedName=None,
|
|
tags=None,
|
|
constraint=Constraint.NULL,
|
|
ordinalPosition=None,
|
|
jsonSchema=None,
|
|
children=None,
|
|
customMetrics=None,
|
|
profile=None,
|
|
),
|
|
Column(
|
|
name="geom_c",
|
|
displayName=None,
|
|
dataType=DataType.GEOMETRY,
|
|
arrayDataType=None,
|
|
dataLength=1,
|
|
precision=None,
|
|
scale=None,
|
|
dataTypeDisplay="geometry",
|
|
description=None,
|
|
fullyQualifiedName=None,
|
|
tags=None,
|
|
constraint=Constraint.NULL,
|
|
ordinalPosition=None,
|
|
jsonSchema=None,
|
|
children=None,
|
|
customMetrics=None,
|
|
profile=None,
|
|
),
|
|
Column(
|
|
name="point_c",
|
|
displayName=None,
|
|
dataType=DataType.GEOMETRY,
|
|
arrayDataType=None,
|
|
dataLength=1,
|
|
precision=None,
|
|
scale=None,
|
|
dataTypeDisplay="point",
|
|
description=None,
|
|
fullyQualifiedName=None,
|
|
tags=None,
|
|
constraint=Constraint.NULL,
|
|
ordinalPosition=None,
|
|
jsonSchema=None,
|
|
children=None,
|
|
customMetrics=None,
|
|
profile=None,
|
|
),
|
|
Column(
|
|
name="polygon_c",
|
|
displayName=None,
|
|
dataType=DataType.GEOMETRY,
|
|
arrayDataType=None,
|
|
dataLength=1,
|
|
precision=None,
|
|
scale=None,
|
|
dataTypeDisplay="polygon",
|
|
description=None,
|
|
fullyQualifiedName=None,
|
|
tags=None,
|
|
constraint=Constraint.NULL,
|
|
ordinalPosition=None,
|
|
jsonSchema=None,
|
|
children=None,
|
|
customMetrics=None,
|
|
profile=None,
|
|
),
|
|
]
|
|
|
|
|
|
class PostgresUnitTest(TestCase):
|
|
@patch(
|
|
"metadata.ingestion.source.database.common_db_source.CommonDbSourceService.test_connection"
|
|
)
|
|
def __init__(self, methodName, test_connection) -> None:
|
|
super().__init__(methodName)
|
|
test_connection.return_value = False
|
|
self.config = OpenMetadataWorkflowConfig.model_validate(mock_postgres_config)
|
|
self.postgres_source = PostgresSource.create(
|
|
mock_postgres_config["source"],
|
|
self.config.workflowConfig.openMetadataServerConfig,
|
|
)
|
|
|
|
self.postgres_source.context.get().__dict__[
|
|
"database_service"
|
|
] = MOCK_DATABASE_SERVICE.name.root
|
|
self.postgres_source.context.get().__dict__[
|
|
"database"
|
|
] = MOCK_DATABASE.name.root
|
|
self.postgres_source.context.get().__dict__[
|
|
"database_schema"
|
|
] = MOCK_DATABASE_SCHEMA.name.root
|
|
|
|
self.usage_config = OpenMetadataWorkflowConfig.model_validate(
|
|
mock_postgres_usage_config
|
|
)
|
|
self.postgres_usage_source = PostgresUsageSource.create(
|
|
mock_postgres_usage_config["source"],
|
|
self.usage_config.workflowConfig.openMetadataServerConfig,
|
|
)
|
|
|
|
def test_datatype(self):
|
|
inspector = types.SimpleNamespace()
|
|
inspector.get_columns = (
|
|
lambda table_name, schema_name, db_name: MOCK_COLUMN_VALUE
|
|
)
|
|
inspector.get_pk_constraint = lambda table_name, schema_name: []
|
|
inspector.get_unique_constraints = lambda table_name, schema_name: []
|
|
inspector.get_foreign_keys = lambda table_name, schema_name: []
|
|
result, _, _ = self.postgres_source.get_columns_and_constraints(
|
|
"public", "user", "postgres", inspector
|
|
)
|
|
for i, _ in enumerate(EXPECTED_COLUMN_VALUE):
|
|
self.assertEqual(result[i], EXPECTED_COLUMN_VALUE[i])
|
|
|
|
@patch("sqlalchemy.engine.base.Engine")
|
|
def test_get_version_info(self, engine):
|
|
# outdated with a switch to get_server_version_num instead of get_+server_version
|
|
# engine.execute.return_value = [["15.3 (Debian 15.3-1.pgdg110+1)"]]
|
|
# self.assertEqual("15.3", get_postgres_version(engine))
|
|
|
|
engine.execute.return_value = [["110016"]]
|
|
self.assertEqual("110016", get_postgres_version(engine))
|
|
|
|
engine.execute.return_value = [["90624"]]
|
|
self.assertEqual("90624", get_postgres_version(engine))
|
|
|
|
engine.execute.return_value = [[]]
|
|
self.assertIsNone(get_postgres_version(engine))
|
|
|
|
@patch("sqlalchemy.engine.base.Engine")
|
|
@patch(
|
|
"metadata.ingestion.source.database.common_db_source.CommonDbSourceService.connection"
|
|
)
|
|
def test_close_connection(self, engine, connection):
|
|
connection.return_value = True
|
|
self.postgres_source.close()
|