Files
sqlmodel-pg-kit/tests/test_smoke.py
2025-08-17 22:18:45 +08:00

28 lines
933 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from typing import Optional
from sqlmodel import SQLModel, Field
from sqlmodel_pg_kit import create_all, Repository
def test_smoke(tmp_path, monkeypatch):
# 用 SQLite 临时库跑一遍(替换 cfg 同步 URL
from sqlmodel_pg_kit import db
from sqlmodel_pg_kit.db import get_session
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str = Field(index=True)
age: Optional[int] = None
monkeypatch.setattr(db, "cfg", db.DatabaseConfig(
host="", port=0, user="", password="", database=":memory:", sslmode="disable"
))
monkeypatch.setattr(db, "engine", db.create_engine("sqlite:///:memory:", echo=False))
create_all()
repo = Repository(Hero)
with get_session() as s:
repo.create(s, {"name": "Iron Man", "age": 45})
names = [h.name for h in repo.list(s)]
assert names == ["Iron Man"]