add: PostgreSQL 支持(asyncpg 驱动,from_url/from_config + 集成测试)

This commit is contained in:
2026-08-17 17:26:50 +08:00
parent da7a47ed2f
commit e19575424e
7 changed files with 524 additions and 46 deletions
+39
View File
@@ -0,0 +1,39 @@
"""Database 构造与 from_config 的纯 API 校验(无 IO)。"""
import pytest
from chongming_database import Database
async def test_constructor_requires_exactly_one_arg():
# 两个都不给
with pytest.raises(ValueError):
Database()
# 两个都给
with pytest.raises(ValueError):
Database("x.db", url="postgresql+asyncpg://localhost/db")
async def test_from_config_sqlite_requires_path():
with pytest.raises(ValueError):
Database.from_config({"type": "sqlite"})
async def test_from_config_postgres_requires_url():
with pytest.raises(ValueError):
Database.from_config({"type": "postgresql"})
async def test_from_config_unsupported_type():
with pytest.raises(NotImplementedError):
Database.from_config({"type": "mysql", "url": "mysql://localhost/db"})
async def test_postgres_url_normalized_to_asyncpg():
db = Database.from_url("postgresql://user:pwd@127.0.0.1:5432/mydb")
assert db._build_url() == "postgresql+asyncpg://user:pwd@127.0.0.1:5432/mydb"
async def test_explicit_driver_url_untouched():
db = Database(url="postgresql+asyncpg://user:pwd@127.0.0.1:5432/mydb")
assert db._build_url() == "postgresql+asyncpg://user:pwd@127.0.0.1:5432/mydb"