feat: support psycopg (#425)

This commit is contained in:
Waket Zheng
2025-02-26 17:11:31 +08:00
committed by GitHub
parent 0364ae3f83
commit 49bfbf4e6b
10 changed files with 218 additions and 26 deletions

View File

@@ -5,6 +5,14 @@ from settings import TORTOISE_ORM
from tortoise import Tortoise
from tortoise.exceptions import OperationalError
try:
# This error does not translate to tortoise's OperationalError
from psycopg.errors import UndefinedColumn
except ImportError:
errors = (OperationalError,)
else:
errors = (OperationalError, UndefinedColumn)
@pytest.fixture(scope="session")
def anyio_backend() -> str:
@@ -34,29 +42,29 @@ async def test_init_db():
async def test_fake_field_1():
assert "field_1" in NewModel._meta.fields_map
assert "field_1" in Config._meta.fields_map
with pytest.raises(OperationalError):
with pytest.raises(errors):
await NewModel.create(name="", field_1=1)
with pytest.raises(OperationalError):
with pytest.raises(errors):
await Config.create(key="", label="", value={}, field_1=1)
obj1 = NewModel(name="", field_1=1)
with pytest.raises(OperationalError):
with pytest.raises(errors):
await obj1.save()
obj1 = NewModel(name="")
with pytest.raises(OperationalError):
with pytest.raises(errors):
await obj1.save()
with pytest.raises(OperationalError):
with pytest.raises(errors):
obj1 = await NewModel.first()
obj1 = await NewModel.all().first().values("id", "name")
assert obj1 and obj1["id"]
obj2 = Config(key="", label="", value={}, field_1=1)
with pytest.raises(OperationalError):
with pytest.raises(errors):
await obj2.save()
obj2 = Config(key="", label="", value={})
with pytest.raises(OperationalError):
with pytest.raises(errors):
await obj2.save()
with pytest.raises(OperationalError):
with pytest.raises(errors):
obj2 = await Config.first()
obj2 = await Config.all().first().values("id", "key")
assert obj2 and obj2["id"]
@@ -66,7 +74,7 @@ async def test_fake_field_1():
async def test_fake_field_2():
assert "field_2" in NewModel._meta.fields_map
assert "field_2" in Config._meta.fields_map
with pytest.raises(OperationalError):
with pytest.raises(errors):
await NewModel.create(name="")
with pytest.raises(OperationalError):
with pytest.raises(errors):
await Config.create(key="", label="", value={})