Files

40 lines
1.2 KiB
Python

"""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"