Skip to content

Commit cb76dbb

Browse files
committed
refactor: 测试用例连接信息改为读环境变量,去除硬编码地址
- 新增 tests/test_config.py 集中管理 GaussDB 连接配置 - 新增 tests/.env.example 环境变量模板 - 8个ODBC测试文件(test_edge_cases5-12)改为 from tests.test_config import ODBC_URLS - scripts/live_odbc_test.py 改为读环境变量 - _local/manual_tests/ 4个JDBC测试文件改为读环境变量 - README.md 示例改为占位符 - 支持环境变量: GAUSSDB_HOST/GAUSSDB_PORT/GAUSSDB_USER/GAUSSDB_PASSWORD - 默认值保持向后兼容
1 parent 2aeb56f commit cb76dbb

12 files changed

Lines changed: 59 additions & 53 deletions

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pip install gaussdb_sqlalchemy_python_driver-0.2.0-py3-none-any.whl
4141
from sqlalchemy import create_engine, text
4242

4343
engine = create_engine(
44-
"gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995/postgres"
44+
"gaussdb+odbc://USER:PASSWORD@HOST:PORT/postgres" # 用你的实际连接信息
4545
"?driver=GaussDB+ODBC+Driver&sslmode=disable",
4646
pool_pre_ping=True,
4747
)
@@ -84,7 +84,7 @@ from sqlalchemy.orm import Session, declarative_base
8484
from datetime import datetime
8585

8686
engine = create_engine(
87-
"gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995/testm"
87+
"gaussdb+odbc://USER:PASSWORD@HOST:PORT/testm" # M兼容模式示例
8888
"?driver=GaussDB+ODBC+Driver&sslmode=disable",
8989
pool_pre_ping=True,
9090
)

‎scripts/live_odbc_test.py‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#!/usr/bin/env python3
22
"""Live GaussDB ODBC integration test via SQLAlchemy."""
33

4+
from sqlalchemy import create_engine, text
5+
6+
import os
47
import sys
8+
from urllib.parse import quote_plus
9+
510
sys.path.insert(0, "src")
611

7-
from sqlalchemy import create_engine, text
12+
_HOST = os.environ.get("GAUSSDB_HOST", "121.37.186.131")
13+
_PORT = os.environ.get("GAUSSDB_PORT", "19995")
14+
_USER = os.environ.get("GAUSSDB_USER", "sqlbuilder1")
15+
_PASS = quote_plus(os.environ.get("GAUSSDB_PASSWORD", "huawei@123"))
816

917
URL = (
10-
"gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995/postgres"
11-
"?driver=PostgreSQL&sslmode=disable"
18+
f"gaussdb+odbc://{_USER}:{_PASS}@{_HOST}:{_PORT}/postgres"
19+
f"?driver=PostgreSQL&sslmode=disable"
1220
)
1321

1422
def main():

‎tests/.env.example‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# GaussDB connection parameters for tests
2+
# Copy to .env or export these before running tests
3+
GAUSSDB_HOST=121.37.186.131
4+
GAUSSDB_PORT=19995
5+
GAUSSDB_USER=sqlbuilder1
6+
GAUSSDB_PASSWORD=huawei@123

‎tests/test_config.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""Centralized test configuration via environment variables.
2+
3+
Copy tests/.env.example to .env or export the variables before running tests.
4+
Defaults match the original hardcoded values for backwards compatibility.
5+
"""
6+
import os
7+
from urllib.parse import quote_plus
8+
9+
GAUSSDB_HOST = os.environ.get("GAUSSDB_HOST", "121.37.186.131")
10+
GAUSSDB_PORT = os.environ.get("GAUSSDB_PORT", "19995")
11+
GAUSSDB_USER = os.environ.get("GAUSSDB_USER", "sqlbuilder1")
12+
GAUSSDB_PASSWORD = os.environ.get("GAUSSDB_PASSWORD", "huawei@123")
13+
14+
_pwd = quote_plus(GAUSSDB_PASSWORD)
15+
16+
# ODBC connection base
17+
ODBC_BASE = f"gaussdb+odbc://{GAUSSDB_USER}:{_pwd}@{GAUSSDB_HOST}:{GAUSSDB_PORT}"
18+
19+
ODBC_URLS = {
20+
"A": f"{ODBC_BASE}/postgres?sslmode=disable",
21+
"B": f"{ODBC_BASE}/gdbdrv_b_compat?sslmode=disable",
22+
"M": f"{ODBC_BASE}/testm?sslmode=disable",
23+
}
24+
25+
# JDBC connection base (used by _local/manual_tests)
26+
JDBC_BASE = f"gaussdb+jdbc://{GAUSSDB_USER}:{_pwd}@{GAUSSDB_HOST}:{GAUSSDB_PORT}"
27+
28+
JDBC_URLS = {
29+
"A": f"{JDBC_BASE}/postgres?sslmode=disable",
30+
"B": f"{JDBC_BASE}/gdbdrv_b_compat?sslmode=disable",
31+
"M": f"{JDBC_BASE}/testm?sslmode=disable",
32+
}

‎tests/test_edge_cases10.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
)
1313
from sqlalchemy.orm import Session, declarative_base, relationship
1414

15-
BASE = "gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995"
16-
URLS = {
17-
"A": f"{BASE}/postgres?sslmode=disable",
18-
"B": f"{BASE}/gdbdrv_b_compat?sslmode=disable",
19-
"M": f"{BASE}/testm?sslmode=disable",
20-
}
15+
from tests.test_config import ODBC_URLS as URLS
2116

2217
def _engine(compat, **kw):
2318
return create_engine(URLS[compat], pool_pre_ping=True, **kw)

‎tests/test_edge_cases11.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
)
1414
from sqlalchemy.orm import Session, declarative_base, relationship
1515

16-
BASE = "gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995"
17-
URLS = {
18-
"A": f"{BASE}/postgres?sslmode=disable",
19-
"B": f"{BASE}/gdbdrv_b_compat?sslmode=disable",
20-
"M": f"{BASE}/testm?sslmode=disable",
21-
}
16+
from tests.test_config import ODBC_URLS as URLS
2217

2318
def _engine(compat, **kw):
2419
return create_engine(URLS[compat], pool_pre_ping=True, **kw)

‎tests/test_edge_cases12.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
)
1313
from sqlalchemy.orm import Session, declarative_base, relationship
1414

15-
BASE = "gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995"
16-
URLS = {
17-
"A": f"{BASE}/postgres?sslmode=disable",
18-
"B": f"{BASE}/gdbdrv_b_compat?sslmode=disable",
19-
"M": f"{BASE}/testm?sslmode=disable",
20-
}
15+
from tests.test_config import ODBC_URLS as URLS
2116

2217
def _engine(compat, **kw):
2318
return create_engine(URLS[compat], pool_pre_ping=True, **kw)

‎tests/test_edge_cases5.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,7 @@
1111
)
1212
from sqlalchemy.orm import Session, declarative_base
1313

14-
BASE = "gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995"
15-
URLS = {
16-
"A": f"{BASE}/postgres?sslmode=disable",
17-
"B": f"{BASE}/gdbdrv_b_compat?sslmode=disable",
18-
"M": f"{BASE}/testm?sslmode=disable",
19-
}
14+
from tests.test_config import ODBC_URLS as URLS
2015

2116
def _engine(compat, **kw):
2217
return create_engine(URLS[compat], pool_pre_ping=True, **kw)

‎tests/test_edge_cases6.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
from sqlalchemy.orm import Session, declarative_base, relationship, validates
1414
from sqlalchemy.schema import CreateTable
1515

16-
BASE = "gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995"
17-
URLS = {
18-
"A": f"{BASE}/postgres?sslmode=disable",
19-
"B": f"{BASE}/gdbdrv_b_compat?sslmode=disable",
20-
"M": f"{BASE}/testm?sslmode=disable",
21-
}
16+
from tests.test_config import ODBC_URLS as URLS
2217

2318
def _engine(compat, **kw):
2419
return create_engine(URLS[compat], pool_pre_ping=True, **kw)

‎tests/test_edge_cases7.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
from sqlalchemy.orm import Session, declarative_base
1414
from sqlalchemy.schema import CreateTable
1515

16-
BASE = "gaussdb+odbc://sqlbuilder1:huawei%40123@121.37.186.131:19995"
17-
URLS = {
18-
"A": f"{BASE}/postgres?sslmode=disable",
19-
"B": f"{BASE}/gdbdrv_b_compat?sslmode=disable",
20-
"M": f"{BASE}/testm?sslmode=disable",
21-
}
16+
from tests.test_config import ODBC_URLS as URLS
2217

2318
def _engine(compat, **kw):
2419
return create_engine(URLS[compat], pool_pre_ping=True, **kw)

0 commit comments

Comments
 (0)