|
| 1 | +"""Run a live GaussDB SQLAlchemy integration probe. |
| 2 | +
|
| 3 | +The script expects GAUSSDB_TEST_URL or --url and creates temporary tables with |
| 4 | +``codex_*`` prefixes. It is intentionally framework-free so it can run on a |
| 5 | +database host even when pytest is not installed. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import os |
| 12 | +import uuid |
| 13 | + |
| 14 | +from sqlalchemy import Column |
| 15 | +from sqlalchemy import Index |
| 16 | +from sqlalchemy import Integer |
| 17 | +from sqlalchemy import MetaData |
| 18 | +from sqlalchemy import String |
| 19 | +from sqlalchemy import Table |
| 20 | +from sqlalchemy import UniqueConstraint |
| 21 | +from sqlalchemy import create_engine |
| 22 | +from sqlalchemy import inspect |
| 23 | +from sqlalchemy import text |
| 24 | + |
| 25 | + |
| 26 | +def table_name(prefix: str) -> str: |
| 27 | + return f"{prefix}_{uuid.uuid4().hex[:12]}" |
| 28 | + |
| 29 | + |
| 30 | +def assert_true(condition: bool, message: str) -> None: |
| 31 | + if not condition: |
| 32 | + raise AssertionError(message) |
| 33 | + |
| 34 | + |
| 35 | +def run(url: str) -> tuple[str, ...]: |
| 36 | + from alembic.migration import MigrationContext |
| 37 | + from alembic.operations import Operations |
| 38 | + |
| 39 | + engine = create_engine(url, pool_pre_ping=True) |
| 40 | + results: tuple[str, ...] = () |
| 41 | + |
| 42 | + table = table_name("codex_idx_ut") |
| 43 | + index_name = f"ix_{table}_name" |
| 44 | + unique_name = f"uq_{table}_code" |
| 45 | + metadata = MetaData() |
| 46 | + Table( |
| 47 | + table, |
| 48 | + metadata, |
| 49 | + Column("id", Integer, primary_key=True), |
| 50 | + Column("code", String(32), nullable=False), |
| 51 | + Column("name", String(32), nullable=False), |
| 52 | + UniqueConstraint("code", name=unique_name), |
| 53 | + Index(index_name, "name"), |
| 54 | + ) |
| 55 | + try: |
| 56 | + metadata.create_all(engine) |
| 57 | + inspector = inspect(engine) |
| 58 | + pk = inspector.get_pk_constraint(table) |
| 59 | + assert_true(pk["constrained_columns"] == ["id"], f"unexpected pk: {pk}") |
| 60 | + |
| 61 | + uniques = inspector.get_unique_constraints(table) |
| 62 | + assert_true( |
| 63 | + any( |
| 64 | + constraint["name"] == unique_name |
| 65 | + and constraint["column_names"] == ["code"] |
| 66 | + for constraint in uniques |
| 67 | + ), |
| 68 | + f"unexpected unique constraints: {uniques}", |
| 69 | + ) |
| 70 | + |
| 71 | + indexes = inspector.get_indexes(table) |
| 72 | + assert_true( |
| 73 | + any( |
| 74 | + index["name"] == index_name and index["column_names"] == ["name"] |
| 75 | + for index in indexes |
| 76 | + ), |
| 77 | + f"unexpected indexes: {indexes}", |
| 78 | + ) |
| 79 | + finally: |
| 80 | + metadata.drop_all(engine) |
| 81 | + results += ("pk_unique_index",) |
| 82 | + |
| 83 | + table = table_name("codex_seq_ut") |
| 84 | + sequence = f"{table}_id_seq" |
| 85 | + with engine.begin() as conn: |
| 86 | + conn.execute(text(f"drop table if exists {table}")) |
| 87 | + conn.execute(text(f"drop sequence if exists {sequence}")) |
| 88 | + conn.execute(text(f"create sequence {sequence} start 1")) |
| 89 | + conn.execute( |
| 90 | + text( |
| 91 | + f"create table {table} (" |
| 92 | + f"id int primary key default nextval('{sequence}'), " |
| 93 | + "name varchar(32))" |
| 94 | + ) |
| 95 | + ) |
| 96 | + conn.execute(text(f"insert into {table} (name) values (:name)"), {"name": "a"}) |
| 97 | + conn.execute(text(f"insert into {table} (name) values (:name)"), {"name": "b"}) |
| 98 | + rows = conn.execute(text(f"select id, name from {table} order by id")).all() |
| 99 | + assert_true(rows == [(1, "a"), (2, "b")], f"unexpected rows: {rows}") |
| 100 | + |
| 101 | + columns = {column["name"]: column for column in inspect(conn).get_columns(table)} |
| 102 | + assert_true( |
| 103 | + "nextval" in columns["id"]["default"], |
| 104 | + f"unexpected default: {columns['id']}", |
| 105 | + ) |
| 106 | + conn.execute(text(f"drop table {table}")) |
| 107 | + conn.execute(text(f"drop sequence {sequence}")) |
| 108 | + results += ("sequence",) |
| 109 | + |
| 110 | + table = table_name("codex_alembic_ut") |
| 111 | + with engine.begin() as conn: |
| 112 | + context = MigrationContext.configure(conn) |
| 113 | + operations = Operations(context) |
| 114 | + operations.create_table( |
| 115 | + table, |
| 116 | + Column("id", Integer, primary_key=True), |
| 117 | + Column("name", String(32), nullable=False), |
| 118 | + ) |
| 119 | + operations.add_column(table, Column("remark", String(64))) |
| 120 | + conn.execute( |
| 121 | + text( |
| 122 | + f"insert into {table} (id, name, remark) " |
| 123 | + "values (:id, :name, :remark)" |
| 124 | + ), |
| 125 | + {"id": 1, "name": "created", "remark": "via alembic"}, |
| 126 | + ) |
| 127 | + row = conn.execute( |
| 128 | + text(f"select id, name, remark from {table} where id=:id"), |
| 129 | + {"id": 1}, |
| 130 | + ).one() |
| 131 | + assert_true( |
| 132 | + row == (1, "created", "via alembic"), |
| 133 | + f"unexpected alembic row: {row}", |
| 134 | + ) |
| 135 | + operations.drop_table(table) |
| 136 | + results += ("alembic",) |
| 137 | + |
| 138 | + return results |
| 139 | + |
| 140 | + |
| 141 | +def main() -> int: |
| 142 | + parser = argparse.ArgumentParser() |
| 143 | + parser.add_argument("--url", default=os.environ.get("GAUSSDB_TEST_URL")) |
| 144 | + args = parser.parse_args() |
| 145 | + if not args.url: |
| 146 | + parser.error("--url or GAUSSDB_TEST_URL is required") |
| 147 | + |
| 148 | + results = run(args.url) |
| 149 | + print("integration probe ok:", ",".join(results)) |
| 150 | + return 0 |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + raise SystemExit(main()) |
0 commit comments