datus-postgresql 0.1.8__tar.gz → 0.1.9__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/.gitignore +8 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/PKG-INFO +4 -4
- datus_postgresql-0.1.9/datus_postgresql/tpch_data.py +84 -0
- datus_postgresql-0.1.9/docker-compose.yml +26 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/pyproject.toml +3 -3
- datus_postgresql-0.1.9/scripts/init_tpch_data.py +129 -0
- datus_postgresql-0.1.9/tests/integration/conftest.py +107 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/integration/test_integration.py +44 -20
- datus_postgresql-0.1.9/tests/integration/test_tpch.py +182 -0
- datus_postgresql-0.1.8/docker-compose.yml +0 -20
- datus_postgresql-0.1.8/scripts/init_tpch_data.py +0 -555
- datus_postgresql-0.1.8/tests/integration/conftest.py +0 -536
- datus_postgresql-0.1.8/tests/integration/test_tpch.py +0 -162
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/README.md +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/datus_postgresql/__init__.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/datus_postgresql/config.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/datus_postgresql/connector.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/datus_postgresql/handlers.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/__init__.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/conftest.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/integration/__init__.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/integration/test_contract.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/unit/__init__.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/unit/test_config.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/unit/test_connector_unit.py +0 -0
- {datus_postgresql-0.1.8 → datus_postgresql-0.1.9}/tests/unit/test_migration_mixin.py +0 -0
|
@@ -136,6 +136,14 @@ dmypy.json
|
|
|
136
136
|
.DS_Store
|
|
137
137
|
Thumbs.db
|
|
138
138
|
|
|
139
|
+
# AI agent local state (AGENTS.md and CLAUDE.md are tracked)
|
|
140
|
+
.claude/
|
|
141
|
+
|
|
142
|
+
# Generated by ci/run_pr_coverage.py
|
|
143
|
+
ci/coverage.xml
|
|
144
|
+
ci/diff-cover.json
|
|
145
|
+
ci/diff-cover-report.md
|
|
146
|
+
|
|
139
147
|
|
|
140
148
|
.omc
|
|
141
149
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: datus-postgresql
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.9
|
|
4
4
|
Summary: PostgreSQL database adapter for Datus
|
|
5
5
|
Project-URL: Homepage, https://github.com/Datus-ai/datus-db-adapters
|
|
6
6
|
Project-URL: Repository, https://github.com/Datus-ai/datus-db-adapters
|
|
@@ -14,8 +14,8 @@ Classifier: License :: OSI Approved :: Apache Software License
|
|
|
14
14
|
Classifier: Programming Language :: Python :: 3
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Requires-Python: >=3.12
|
|
17
|
-
Requires-Dist: datus-db-core>=0.1.
|
|
18
|
-
Requires-Dist: datus-sqlalchemy>=0.1.
|
|
17
|
+
Requires-Dist: datus-db-core>=0.1.7
|
|
18
|
+
Requires-Dist: datus-sqlalchemy>=0.1.8
|
|
19
19
|
Requires-Dist: psycopg2-binary>=2.9.11
|
|
20
20
|
Requires-Dist: pydantic>=2.0.0
|
|
21
21
|
Description-Content-Type: text/markdown
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Copyright 2025-present DatusAI, Inc.
|
|
2
|
+
# Licensed under the Apache License, Version 2.0.
|
|
3
|
+
# See http://www.apache.org/licenses/LICENSE-2.0 for details.
|
|
4
|
+
|
|
5
|
+
"""PostgreSQL-dialect TPC-H test data definitions.
|
|
6
|
+
|
|
7
|
+
Table names are unqualified: callers set the target schema through the
|
|
8
|
+
connector's ``schema_name`` (applied as ``SET search_path`` on every
|
|
9
|
+
statement), matching the Greenplum and GaussDB adapters.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from datus_db_core.testing.tpch import ROW_COUNTS, TPCH_TABLES, build_tpch_inserts
|
|
13
|
+
|
|
14
|
+
_TPCH_DDL_ITEMS = [
|
|
15
|
+
(
|
|
16
|
+
"tpch_region",
|
|
17
|
+
"""
|
|
18
|
+
CREATE TABLE IF NOT EXISTS "tpch_region" (
|
|
19
|
+
"regionkey" INTEGER NOT NULL,
|
|
20
|
+
"name" VARCHAR(25) NOT NULL,
|
|
21
|
+
"comment" VARCHAR(152),
|
|
22
|
+
PRIMARY KEY ("regionkey")
|
|
23
|
+
)
|
|
24
|
+
""",
|
|
25
|
+
),
|
|
26
|
+
(
|
|
27
|
+
"tpch_nation",
|
|
28
|
+
"""
|
|
29
|
+
CREATE TABLE IF NOT EXISTS "tpch_nation" (
|
|
30
|
+
"nationkey" INTEGER NOT NULL,
|
|
31
|
+
"name" VARCHAR(25) NOT NULL,
|
|
32
|
+
"regionkey" INTEGER NOT NULL,
|
|
33
|
+
"comment" VARCHAR(152),
|
|
34
|
+
PRIMARY KEY ("nationkey")
|
|
35
|
+
)
|
|
36
|
+
""",
|
|
37
|
+
),
|
|
38
|
+
(
|
|
39
|
+
"tpch_customer",
|
|
40
|
+
"""
|
|
41
|
+
CREATE TABLE IF NOT EXISTS "tpch_customer" (
|
|
42
|
+
"custkey" INTEGER NOT NULL,
|
|
43
|
+
"name" VARCHAR(25) NOT NULL,
|
|
44
|
+
"nationkey" INTEGER NOT NULL,
|
|
45
|
+
"acctbal" DECIMAL(15,2) NOT NULL,
|
|
46
|
+
"mktsegment" VARCHAR(10) NOT NULL,
|
|
47
|
+
PRIMARY KEY ("custkey")
|
|
48
|
+
)
|
|
49
|
+
""",
|
|
50
|
+
),
|
|
51
|
+
(
|
|
52
|
+
"tpch_orders",
|
|
53
|
+
"""
|
|
54
|
+
CREATE TABLE IF NOT EXISTS "tpch_orders" (
|
|
55
|
+
"orderkey" INTEGER NOT NULL,
|
|
56
|
+
"custkey" INTEGER NOT NULL,
|
|
57
|
+
"orderstatus" CHAR(1) NOT NULL,
|
|
58
|
+
"totalprice" DECIMAL(15,2) NOT NULL,
|
|
59
|
+
"orderdate" DATE NOT NULL,
|
|
60
|
+
PRIMARY KEY ("orderkey")
|
|
61
|
+
)
|
|
62
|
+
""",
|
|
63
|
+
),
|
|
64
|
+
(
|
|
65
|
+
"tpch_supplier",
|
|
66
|
+
"""
|
|
67
|
+
CREATE TABLE IF NOT EXISTS "tpch_supplier" (
|
|
68
|
+
"suppkey" INTEGER NOT NULL,
|
|
69
|
+
"name" VARCHAR(25) NOT NULL,
|
|
70
|
+
"nationkey" INTEGER NOT NULL,
|
|
71
|
+
"acctbal" DECIMAL(15,2) NOT NULL,
|
|
72
|
+
PRIMARY KEY ("suppkey")
|
|
73
|
+
)
|
|
74
|
+
""",
|
|
75
|
+
),
|
|
76
|
+
]
|
|
77
|
+
|
|
78
|
+
if [table for table, _ in _TPCH_DDL_ITEMS] != TPCH_TABLES:
|
|
79
|
+
raise ValueError("PostgreSQL TPC-H DDL order must match the shared TPCH_TABLES order")
|
|
80
|
+
|
|
81
|
+
TPCH_DDL = [ddl for _, ddl in _TPCH_DDL_ITEMS]
|
|
82
|
+
TPCH_DATA = build_tpch_inserts(lambda t: f'"{t}"')
|
|
83
|
+
|
|
84
|
+
__all__ = ["TPCH_DDL", "TPCH_DATA", "TPCH_TABLES", "ROW_COUNTS"]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# DATUS_TEST_* configures this container; the POSTGRESQL_* the tests read
|
|
2
|
+
# configures the client. See docs/adapter-testing-standard.md.
|
|
3
|
+
services:
|
|
4
|
+
postgres:
|
|
5
|
+
image: postgres:16
|
|
6
|
+
environment:
|
|
7
|
+
TZ: UTC
|
|
8
|
+
POSTGRES_DB: ${DATUS_TEST_POSTGRESQL_DATABASE:-test}
|
|
9
|
+
POSTGRES_USER: ${DATUS_TEST_POSTGRESQL_USER:-test_user}
|
|
10
|
+
POSTGRES_PASSWORD: ${DATUS_TEST_POSTGRESQL_PASSWORD:-test_password}
|
|
11
|
+
ports:
|
|
12
|
+
- "${POSTGRESQL_HOST_PORT:-5432}:5432"
|
|
13
|
+
healthcheck:
|
|
14
|
+
# A real query, not pg_isready: pg_isready reports "accepting
|
|
15
|
+
# connections" without authenticating, so a data volume left over from
|
|
16
|
+
# different credentials would come up healthy and fail only at test time.
|
|
17
|
+
test: ["CMD-SHELL", "psql -U \"$${POSTGRES_USER}\" -d \"$${POSTGRES_DB}\" -c 'SELECT 1' >/dev/null 2>&1"]
|
|
18
|
+
interval: 10s
|
|
19
|
+
timeout: 5s
|
|
20
|
+
retries: 5
|
|
21
|
+
start_period: 30s
|
|
22
|
+
volumes:
|
|
23
|
+
- postgres_data:/var/lib/postgresql/data
|
|
24
|
+
|
|
25
|
+
volumes:
|
|
26
|
+
postgres_data:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "datus-postgresql"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.9"
|
|
4
4
|
description = "PostgreSQL database adapter for Datus"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
@@ -18,8 +18,8 @@ classifiers = [
|
|
|
18
18
|
]
|
|
19
19
|
|
|
20
20
|
dependencies = [
|
|
21
|
-
"datus-db-core>=0.1.
|
|
22
|
-
"datus-sqlalchemy>=0.1.
|
|
21
|
+
"datus-db-core>=0.1.7",
|
|
22
|
+
"datus-sqlalchemy>=0.1.8",
|
|
23
23
|
"psycopg2-binary>=2.9.11",
|
|
24
24
|
"pydantic>=2.0.0",
|
|
25
25
|
]
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# Copyright 2025-present DatusAI, Inc.
|
|
3
|
+
# Licensed under the Apache License, Version 2.0.
|
|
4
|
+
# See http://www.apache.org/licenses/LICENSE-2.0 for details.
|
|
5
|
+
|
|
6
|
+
"""Initialize TPC-H sample data in PostgreSQL for Datus integration testing.
|
|
7
|
+
|
|
8
|
+
Creates the five TPC-H tables (region, nation, customer, orders, supplier) and
|
|
9
|
+
populates them with the shared dataset used by the integration suite. The DDL
|
|
10
|
+
and rows come from ``datus_postgresql.tpch_data`` — this script never holds a
|
|
11
|
+
second copy.
|
|
12
|
+
|
|
13
|
+
Usage:
|
|
14
|
+
python scripts/init_tpch_data.py \\
|
|
15
|
+
--host localhost --port 5432 \\
|
|
16
|
+
--username test_user --password test_password \\
|
|
17
|
+
--database test --schema public
|
|
18
|
+
|
|
19
|
+
# Drop existing tables first, then recreate
|
|
20
|
+
python scripts/init_tpch_data.py --drop
|
|
21
|
+
|
|
22
|
+
Environment variables (used as defaults):
|
|
23
|
+
POSTGRESQL_HOST, POSTGRESQL_PORT, POSTGRESQL_USER, POSTGRESQL_PASSWORD,
|
|
24
|
+
POSTGRESQL_DATABASE, POSTGRESQL_SCHEMA
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
import argparse
|
|
28
|
+
import os
|
|
29
|
+
import re
|
|
30
|
+
import sys
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def main():
|
|
34
|
+
parser = argparse.ArgumentParser(description="Initialize TPC-H data in PostgreSQL")
|
|
35
|
+
parser.add_argument("--host", default=os.getenv("POSTGRESQL_HOST", "localhost"))
|
|
36
|
+
parser.add_argument("--port", type=int, default=int(os.getenv("POSTGRESQL_PORT", "5432")))
|
|
37
|
+
parser.add_argument("--username", default=os.getenv("POSTGRESQL_USER", "test_user"))
|
|
38
|
+
parser.add_argument("--password", default=os.getenv("POSTGRESQL_PASSWORD", "test_password"))
|
|
39
|
+
parser.add_argument("--database", default=os.getenv("POSTGRESQL_DATABASE", "test"))
|
|
40
|
+
parser.add_argument("--schema", default=os.getenv("POSTGRESQL_SCHEMA", "public"))
|
|
41
|
+
parser.add_argument("--drop", action="store_true", help="Drop existing tables before creating")
|
|
42
|
+
args = parser.parse_args()
|
|
43
|
+
|
|
44
|
+
if not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", args.schema):
|
|
45
|
+
print("ERROR: --schema must be a valid SQL identifier (letters, digits, underscores).")
|
|
46
|
+
sys.exit(1)
|
|
47
|
+
|
|
48
|
+
try:
|
|
49
|
+
from datus_postgresql import PostgreSQLConfig, PostgreSQLConnector
|
|
50
|
+
from datus_postgresql.tpch_data import ROW_COUNTS, TPCH_DATA, TPCH_DDL, TPCH_TABLES
|
|
51
|
+
except ImportError:
|
|
52
|
+
print("ERROR: datus-postgresql is not installed.")
|
|
53
|
+
print(" pip install -e ../datus-postgresql")
|
|
54
|
+
sys.exit(1)
|
|
55
|
+
|
|
56
|
+
config = PostgreSQLConfig(
|
|
57
|
+
host=args.host,
|
|
58
|
+
port=args.port,
|
|
59
|
+
username=args.username,
|
|
60
|
+
password=args.password,
|
|
61
|
+
database=args.database,
|
|
62
|
+
schema_name=args.schema,
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
conn = None
|
|
66
|
+
schema = args.schema
|
|
67
|
+
|
|
68
|
+
try:
|
|
69
|
+
conn = PostgreSQLConnector(config)
|
|
70
|
+
if not conn.test_connection():
|
|
71
|
+
print("ERROR: Connection test failed.")
|
|
72
|
+
sys.exit(1)
|
|
73
|
+
print(f"Connected to PostgreSQL at {args.host}:{args.port}/{args.database}")
|
|
74
|
+
|
|
75
|
+
# Statements are unqualified; the connector applies `SET search_path`
|
|
76
|
+
# from schema_name to every statement.
|
|
77
|
+
if args.drop:
|
|
78
|
+
print("\nDropping existing TPC-H tables...")
|
|
79
|
+
for table in reversed(TPCH_TABLES):
|
|
80
|
+
conn.execute_ddl(f'DROP TABLE IF EXISTS "{schema}"."{table}" CASCADE')
|
|
81
|
+
print(f" Dropped {table}")
|
|
82
|
+
|
|
83
|
+
print("\nCreating TPC-H tables...")
|
|
84
|
+
for i, ddl in enumerate(TPCH_DDL):
|
|
85
|
+
result = conn.execute_ddl(ddl)
|
|
86
|
+
if not result.success:
|
|
87
|
+
print(f" Error creating {TPCH_TABLES[i]}: {result.error}")
|
|
88
|
+
sys.exit(1)
|
|
89
|
+
print(f" Created {TPCH_TABLES[i]}")
|
|
90
|
+
|
|
91
|
+
# ON CONFLICT DO NOTHING keeps a re-run without --drop idempotent.
|
|
92
|
+
print("\nInserting TPC-H data...")
|
|
93
|
+
for i, insert_sql in enumerate(TPCH_DATA):
|
|
94
|
+
result = conn.execute_insert(f"{insert_sql.rstrip()} ON CONFLICT DO NOTHING")
|
|
95
|
+
if not result.success:
|
|
96
|
+
print(f" Error inserting into {TPCH_TABLES[i]}: {result.error}")
|
|
97
|
+
sys.exit(1)
|
|
98
|
+
print(f" Inserted {ROW_COUNTS[i]} rows into {TPCH_TABLES[i]}")
|
|
99
|
+
|
|
100
|
+
print("\nVerification:")
|
|
101
|
+
has_mismatch = False
|
|
102
|
+
for i, table in enumerate(TPCH_TABLES):
|
|
103
|
+
result = conn.execute(
|
|
104
|
+
{"sql_query": f'SELECT COUNT(*) AS cnt FROM "{schema}"."{table}"'},
|
|
105
|
+
result_format="list",
|
|
106
|
+
)
|
|
107
|
+
count = result.sql_return[0]["cnt"]
|
|
108
|
+
expected = ROW_COUNTS[i]
|
|
109
|
+
status = "OK" if count == expected else "MISMATCH"
|
|
110
|
+
if count != expected:
|
|
111
|
+
has_mismatch = True
|
|
112
|
+
print(f" {table}: {count} rows [{status}]")
|
|
113
|
+
|
|
114
|
+
if has_mismatch:
|
|
115
|
+
print("\nVerification failed. Re-run with --drop for a clean re-init.")
|
|
116
|
+
sys.exit(2)
|
|
117
|
+
|
|
118
|
+
print("\nTPC-H data initialization complete!")
|
|
119
|
+
|
|
120
|
+
finally:
|
|
121
|
+
if conn is not None:
|
|
122
|
+
try:
|
|
123
|
+
conn.close()
|
|
124
|
+
except Exception:
|
|
125
|
+
pass
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
if __name__ == "__main__":
|
|
129
|
+
main()
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Copyright 2025-present DatusAI, Inc.
|
|
2
|
+
# Licensed under the Apache License, Version 2.0.
|
|
3
|
+
# See http://www.apache.org/licenses/LICENSE-2.0 for details.
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import Generator
|
|
7
|
+
|
|
8
|
+
import pytest
|
|
9
|
+
|
|
10
|
+
from datus_postgresql import PostgreSQLConfig, PostgreSQLConnector
|
|
11
|
+
from datus_postgresql.tpch_data import ROW_COUNTS, TPCH_DATA, TPCH_DDL, TPCH_TABLES
|
|
12
|
+
|
|
13
|
+
__all__ = ["ROW_COUNTS", "TPCH_DATA", "TPCH_DDL", "TPCH_TABLES"]
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
# ---------------------------------------------------------------------------
|
|
17
|
+
# Fixtures
|
|
18
|
+
# ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@pytest.fixture
|
|
22
|
+
def config() -> PostgreSQLConfig:
|
|
23
|
+
"""Create PostgreSQL configuration for integration tests from environment or defaults."""
|
|
24
|
+
return PostgreSQLConfig(
|
|
25
|
+
host=os.getenv("POSTGRESQL_HOST", "localhost"),
|
|
26
|
+
port=int(os.getenv("POSTGRESQL_PORT", "5432")),
|
|
27
|
+
username=os.getenv("POSTGRESQL_USER", "test_user"),
|
|
28
|
+
password=os.getenv("POSTGRESQL_PASSWORD", "test_password"),
|
|
29
|
+
database=os.getenv("POSTGRESQL_DATABASE", "test"),
|
|
30
|
+
schema_name=os.getenv("POSTGRESQL_SCHEMA", "public"),
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@pytest.fixture
|
|
35
|
+
def connector(config: PostgreSQLConfig) -> Generator[PostgreSQLConnector, None, None]:
|
|
36
|
+
"""Create and cleanup PostgreSQL connector for integration tests."""
|
|
37
|
+
conn = None
|
|
38
|
+
try:
|
|
39
|
+
conn = PostgreSQLConnector(config)
|
|
40
|
+
if not conn.test_connection():
|
|
41
|
+
pytest.skip("Database connection test failed")
|
|
42
|
+
yield conn
|
|
43
|
+
except Exception as e:
|
|
44
|
+
pytest.skip(f"Database not available: {e}")
|
|
45
|
+
finally:
|
|
46
|
+
if conn is not None:
|
|
47
|
+
try:
|
|
48
|
+
conn.close()
|
|
49
|
+
except Exception:
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
@pytest.fixture(scope="session")
|
|
54
|
+
def tpch_setup():
|
|
55
|
+
"""Set up TPC-H tables and data for integration tests (session-scoped).
|
|
56
|
+
|
|
57
|
+
Creates tables, inserts sample data, yields for tests, then drops tables.
|
|
58
|
+
Skips all tests if the database is not available.
|
|
59
|
+
"""
|
|
60
|
+
cfg = PostgreSQLConfig(
|
|
61
|
+
host=os.getenv("POSTGRESQL_HOST", "localhost"),
|
|
62
|
+
port=int(os.getenv("POSTGRESQL_PORT", "5432")),
|
|
63
|
+
username=os.getenv("POSTGRESQL_USER", "test_user"),
|
|
64
|
+
password=os.getenv("POSTGRESQL_PASSWORD", "test_password"),
|
|
65
|
+
database=os.getenv("POSTGRESQL_DATABASE", "test"),
|
|
66
|
+
schema_name=os.getenv("POSTGRESQL_SCHEMA", "public"),
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
conn = None
|
|
70
|
+
try:
|
|
71
|
+
conn = PostgreSQLConnector(cfg)
|
|
72
|
+
if not conn.test_connection():
|
|
73
|
+
pytest.skip("Database connection test failed")
|
|
74
|
+
except Exception as e:
|
|
75
|
+
pytest.skip(f"Database not available: {e}")
|
|
76
|
+
|
|
77
|
+
schema = cfg.schema_name
|
|
78
|
+
|
|
79
|
+
try:
|
|
80
|
+
# Statements are unqualified; the connector applies `SET search_path`
|
|
81
|
+
# from cfg.schema_name to every statement.
|
|
82
|
+
for table in TPCH_TABLES:
|
|
83
|
+
conn.execute_ddl(f'DROP TABLE IF EXISTS "{schema}"."{table}" CASCADE')
|
|
84
|
+
|
|
85
|
+
for i, ddl in enumerate(TPCH_DDL):
|
|
86
|
+
result = conn.execute_ddl(ddl)
|
|
87
|
+
if not result.success:
|
|
88
|
+
pytest.fail(f"Failed to create {TPCH_TABLES[i]}: {result.error}")
|
|
89
|
+
|
|
90
|
+
for i, insert_sql in enumerate(TPCH_DATA):
|
|
91
|
+
result = conn.execute_insert(insert_sql)
|
|
92
|
+
if not result.success:
|
|
93
|
+
pytest.fail(f"Failed to load {TPCH_TABLES[i]}: {result.error}")
|
|
94
|
+
|
|
95
|
+
yield conn
|
|
96
|
+
|
|
97
|
+
finally:
|
|
98
|
+
# Cleanup: drop tables in reverse order (foreign key safety)
|
|
99
|
+
for table in reversed(TPCH_TABLES):
|
|
100
|
+
try:
|
|
101
|
+
conn.execute_ddl(f'DROP TABLE IF EXISTS "{schema}"."{table}" CASCADE')
|
|
102
|
+
except Exception:
|
|
103
|
+
pass
|
|
104
|
+
try:
|
|
105
|
+
conn.close()
|
|
106
|
+
except Exception:
|
|
107
|
+
pass
|
|
@@ -103,27 +103,39 @@ def test_get_tables_with_ddl(connector: PostgreSQLConnector, config: PostgreSQLC
|
|
|
103
103
|
suffix = uuid.uuid4().hex[:8]
|
|
104
104
|
table_name = f"test_table_{suffix}"
|
|
105
105
|
|
|
106
|
-
connector.execute_ddl(
|
|
106
|
+
create_result = connector.execute_ddl(
|
|
107
107
|
f"""
|
|
108
|
-
CREATE TABLE IF NOT EXISTS {table_name} (
|
|
108
|
+
CREATE TABLE IF NOT EXISTS "{config.schema_name}"."{table_name}" (
|
|
109
109
|
id SERIAL PRIMARY KEY,
|
|
110
110
|
name VARCHAR(50)
|
|
111
111
|
)
|
|
112
112
|
"""
|
|
113
113
|
)
|
|
114
|
+
assert create_result.success, f"failed to create {table_name}: {create_result.error}"
|
|
114
115
|
|
|
115
116
|
try:
|
|
116
117
|
tables = connector.get_tables_with_ddl(schema_name=config.schema_name, tables=[table_name])
|
|
117
118
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
# The `tables` filter is exact, so the table just created is the only expected result.
|
|
120
|
+
assert [t["table_name"] for t in tables] == [table_name]
|
|
121
|
+
|
|
122
|
+
table = tables[0]
|
|
123
|
+
assert table["table_type"] == "table"
|
|
124
|
+
assert table["catalog_name"] == ""
|
|
125
|
+
assert table["database_name"] == config.database
|
|
126
|
+
assert table["schema_name"] == config.schema_name
|
|
127
|
+
assert table["identifier"] == f"{config.database}.{config.schema_name}.{table_name}"
|
|
128
|
+
|
|
129
|
+
# The DDL is reconstructed from get_schema(), not dumped by the server, so types
|
|
130
|
+
# come back without their length modifier ("character varying", not "varchar(50)").
|
|
131
|
+
definition = table["definition"]
|
|
132
|
+
assert definition.startswith(f'CREATE TABLE "{config.database}"."{config.schema_name}"."{table_name}" (')
|
|
133
|
+
assert '"id" integer NOT NULL' in definition
|
|
134
|
+
assert f"nextval('{table_name}_id_seq'::regclass)" in definition
|
|
135
|
+
assert '"name" character varying' in definition
|
|
136
|
+
assert 'PRIMARY KEY ("id")' in definition
|
|
125
137
|
finally:
|
|
126
|
-
connector.execute_ddl(f
|
|
138
|
+
connector.execute_ddl(f'DROP TABLE IF EXISTS "{config.schema_name}"."{table_name}"')
|
|
127
139
|
|
|
128
140
|
|
|
129
141
|
# ==================== View Tests ====================
|
|
@@ -145,29 +157,41 @@ def test_get_views_with_ddl(connector: PostgreSQLConnector, config: PostgreSQLCo
|
|
|
145
157
|
table_name = f"test_table_{suffix}"
|
|
146
158
|
|
|
147
159
|
# Create base table
|
|
148
|
-
connector.execute_ddl(
|
|
160
|
+
table_result = connector.execute_ddl(
|
|
149
161
|
f"""
|
|
150
|
-
CREATE TABLE IF NOT EXISTS {table_name} (
|
|
162
|
+
CREATE TABLE IF NOT EXISTS "{config.schema_name}"."{table_name}" (
|
|
151
163
|
id SERIAL PRIMARY KEY,
|
|
152
164
|
name VARCHAR(50)
|
|
153
165
|
)
|
|
154
166
|
"""
|
|
155
167
|
)
|
|
168
|
+
assert table_result.success, f"failed to create {table_name}: {table_result.error}"
|
|
156
169
|
|
|
157
170
|
try:
|
|
158
171
|
# Create view
|
|
159
|
-
connector.execute_ddl(
|
|
172
|
+
view_result = connector.execute_ddl(
|
|
173
|
+
f'CREATE VIEW "{config.schema_name}"."{view_name}" AS SELECT * FROM "{config.schema_name}"."{table_name}"'
|
|
174
|
+
)
|
|
175
|
+
assert view_result.success, f"failed to create {view_name}: {view_result.error}"
|
|
160
176
|
|
|
161
177
|
views = connector.get_views_with_ddl(schema_name=config.schema_name)
|
|
162
178
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
179
|
+
matched = [v for v in views if v["table_name"] == view_name]
|
|
180
|
+
assert len(matched) == 1, f"{view_name} missing from {[v['table_name'] for v in views]}"
|
|
181
|
+
|
|
182
|
+
view = matched[0]
|
|
183
|
+
assert view["table_type"] == "view"
|
|
184
|
+
assert view["catalog_name"] == ""
|
|
185
|
+
assert view["database_name"] == config.database
|
|
186
|
+
assert view["schema_name"] == config.schema_name
|
|
187
|
+
assert view["identifier"] == f"{config.database}.{config.schema_name}.{view_name}"
|
|
188
|
+
|
|
189
|
+
definition = view["definition"]
|
|
190
|
+
assert definition.startswith(f'CREATE VIEW "{config.database}"."{config.schema_name}"."{view_name}" AS')
|
|
191
|
+
assert table_name in definition
|
|
168
192
|
finally:
|
|
169
|
-
connector.execute_ddl(f
|
|
170
|
-
connector.execute_ddl(f
|
|
193
|
+
connector.execute_ddl(f'DROP VIEW IF EXISTS "{config.schema_name}"."{view_name}"')
|
|
194
|
+
connector.execute_ddl(f'DROP TABLE IF EXISTS "{config.schema_name}"."{table_name}"')
|
|
171
195
|
|
|
172
196
|
|
|
173
197
|
# ==================== Column Schema Tests ====================
|