๐ฌ ๐ฝ¶
๐ ๐ช โ๏ธ ๐ ๐ ๐ โช๏ธโก๏ธ ๐ฌ ๐ โฎ๏ธ ๐ ๐ ๐ฝ ๐ฌ.
๐ ๐ช ๐ โ ๐ ๐ ๐ฝ ๐ฌ, ๐พ ๐ฝ โฎ๏ธ ๐ฏ, ๐ค-๐ฅง โซ๏ธ โฎ๏ธ ๐ฌ ๐ฝ, โ๏ธ.
๐ ๐ญ โซ๏ธโ ๐ ๐ ๐ ๐ โฎ๏ธ ๐.
๐ฎ ๐ฏ ๐ ๐ฑ¶
โก๏ธ โน ๐ผ โช๏ธโก๏ธ ๐ (๐) ๐ฝ โ๏ธ ๐ฌ ๐ฝ.
๐ ๐ฑ ๐ ๐, ๐ ๐ช ๐ถ ๐ ๐ ๐ โ โ โซ๏ธ.
๐ด ๐ ๐ฅ ๐ ๐ฌ ๐.
๐ ๐ ๐ get_db()
๐ ๐จ ๐ฝ ๐.
๐ฏ, ๐ ๐ช โ๏ธ ๐ ๐ ๐จ ๐ ๐ ๐ฝ ๐ โฉ๏ธ 1๏ธโฃ ๐ ๐ โ๏ธ ๐.
๐ ๐ผ ๐ฅ ๐ โ ๐ ๐ฝ ๐ด ๐ฏ.
๐ ๐¶
๐ฅ โ ๐ ๐ sql_app/tests/test_sql_app.py
.
๐ ๐ ๐ ๐ ๐:
.
โโโ sql_app
โโโ __init__.py
โโโ crud.py
โโโ database.py
โโโ main.py
โโโ models.py
โโโ schemas.py
โโโ tests
โโโ __init__.py
โโโ test_sql_app.py
โ ๐ ๐ฝ ๐¶
๐ฅ, ๐ฅ โ ๐ ๐ฝ ๐ โฎ๏ธ ๐ ๐ฝ.
๐ฏ ๐ฅ ๐ โ๏ธ ๐ test.db
โฉ๏ธ sql_app.db
.
โ๏ธ ๐ ๐ ๐ ๐ โ๏ธ ๐ ๐, ๐ฅ ๐ โซ๏ธ.
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from ..database import Base
from ..main import app, get_db
SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
def test_create_user():
response = client.post(
"/users/",
json={"email": "deadpool@example.com", "password": "chimichangas4life"},
)
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert "id" in data
user_id = data["id"]
response = client.get(f"/users/{user_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert data["id"] == user_id
Tip
๐ ๐ช ๐ โ ๐ ๐ ๐ฎ โซ๏ธ ๐ข & โ๏ธ โซ๏ธ โช๏ธโก๏ธ ๐ฏโโ๏ธ database.py
& tests/test_sql_app.py
.
๐ฆ & ๐ฏ ๐ ๐ฏ ๐ฌ ๐, ๐ฅ ๐จ โซ๏ธ.
โ ๐ฝ¶
โฉ๏ธ ๐ ๐ฅ ๐ โ๏ธ ๐ ๐ฝ ๐ ๐, ๐ฅ ๐ช โ ๐ญ ๐ฅ โ ๐ฝ โฎ๏ธ:
Base.metadata.create_all(bind=engine)
๐ ๐ ๐ค main.py
, โ๏ธ โธ main.py
โ๏ธ ๐ฝ ๐ sql_app.db
, & ๐ฅ ๐ช โ ๐ญ ๐ฅ โ test.db
๐ฏ.
๐ฅ ๐ฎ ๐ โธ ๐ฅ, โฎ๏ธ ๐ ๐.
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from ..database import Base
from ..main import app, get_db
SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
def test_create_user():
response = client.post(
"/users/",
json={"email": "deadpool@example.com", "password": "chimichangas4life"},
)
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert "id" in data
user_id = data["id"]
response = client.get(f"/users/{user_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert data["id"] == user_id
๐ ๐¶
๐ ๐ฅ โ ๐ ๐ & ๐ฎ โซ๏ธ ๐ ๐ ๐ฑ.
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from ..database import Base
from ..main import app, get_db
SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
def test_create_user():
response = client.post(
"/users/",
json={"email": "deadpool@example.com", "password": "chimichangas4life"},
)
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert "id" in data
user_id = data["id"]
response = client.get(f"/users/{user_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert data["id"] == user_id
Tip
๐ override_get_db()
๐ โซ๏ธโ ๐ get_db()
, โ๏ธ override_get_db()
๐ฅ โ๏ธ TestingSessionLocal
๐ฌ ๐ฝ โฉ๏ธ.
๐ฏ ๐ฑ¶
โคด๏ธ ๐ฅ ๐ช ๐ฏ ๐ฑ ๐.
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from ..database import Base
from ..main import app, get_db
SQLALCHEMY_DATABASE_URL = "sqlite://"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base.metadata.create_all(bind=engine)
def override_get_db():
try:
db = TestingSessionLocal()
yield db
finally:
db.close()
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
def test_create_user():
response = client.post(
"/users/",
json={"email": "deadpool@example.com", "password": "chimichangas4life"},
)
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert "id" in data
user_id = data["id"]
response = client.get(f"/users/{user_id}")
assert response.status_code == 200, response.text
data = response.json()
assert data["email"] == "deadpool@example.com"
assert data["id"] == user_id
& ๐ ๐ ๏ธ ๐ฅ โ ๐ฝ โฎ๏ธ ๐ฏ ๐ test.db
๐ฝ โฉ๏ธ ๐ sql_app.db
.