Last active
June 15, 2025 16:16
-
-
Save up1/6be0c1e1ff99db6532678a529a0c24a8 to your computer and use it in GitHub Desktop.
PGLite + FastAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ติดตั้ง | |
$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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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