Skip to content

Instantly share code, notes, and snippets.

@up1
Last active June 15, 2025 16:16
Show Gist options
  • Save up1/6be0c1e1ff99db6532678a529a0c24a8 to your computer and use it in GitHub Desktop.
Save up1/6be0c1e1ff99db6532678a529a0c24a8 to your computer and use it in GitHub Desktop.
PGLite + FastAPI
# ติดตั้ง
$pip install fastapi
$pip install py-pglite
$pip install sqlmodel
$pip install pytest
$pip install coverage
# Run test and coverage
$coverage run -m pytest
$coverage report -m
from fastapi.testclient import TestClient
from sqlmodel import SQLModel, Field, Session
from api import app
class User(SQLModel, table=True):
__table_args__ = {"extend_existing": True}
id: int | None = Field(default=None, primary_key=True)
name: str
client = TestClient(app)
def test_api_success_to_get_user(pglite_session: Session):
# Create a user
user = User(name="Alice")
pglite_session.add(user)
pglite_session.commit()
pglite_session.refresh(user)
response = client.get(f"/users/{user.id}")
assert response.status_code == 200
assert response.json() == {"id": user.id, "name": "Alice"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment