Skip to content

๐Ÿ”ฌ ๐Ÿ’ฝ

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿ”— ๐Ÿ” โšช๏ธโžก๏ธ ๐Ÿ”ฌ ๐Ÿ”— โฎ๏ธ ๐Ÿ” ๐Ÿ“‰ ๐Ÿ’ฝ ๐Ÿ”ฌ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’š โš’ ๐Ÿ†™ ๐ŸŽ ๐Ÿ’ฝ ๐Ÿ”ฌ, ๐Ÿ’พ ๐Ÿ’ฝ โฎ๏ธ ๐Ÿ’ฏ, ๐Ÿค-๐Ÿฅง โšซ๏ธ โฎ๏ธ ๐Ÿ”ฌ ๐Ÿ’ฝ, โ™’๏ธ.

๐Ÿ‘‘ ๐Ÿ’ญ โšซ๏ธโ” ๐ŸŽ ๐Ÿ‘† ๐Ÿ‘€ ๐Ÿ‘ˆ โฎ๏ธ ๐Ÿ“ƒ.

๐Ÿšฎ ๐Ÿ’ฏ ๐Ÿ—„ ๐Ÿ“ฑ

โžก๏ธ โ„น ๐Ÿ–ผ โšช๏ธโžก๏ธ ๐Ÿ—„ (๐Ÿ”—) ๐Ÿ’ฝ โš™๏ธ ๐Ÿ”ฌ ๐Ÿ’ฝ.

๐ŸŒ ๐Ÿ“ฑ ๐Ÿ“Ÿ ๐ŸŽ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿšถ ๐Ÿ”™ ๐Ÿ‘ˆ ๐Ÿ“ƒ โœ… โ” โšซ๏ธ.

๐Ÿ•ด ๐Ÿ”€ ๐Ÿ“ฅ ๐Ÿ†• ๐Ÿ”ฌ ๐Ÿ“.

๐Ÿ‘† ๐Ÿ˜ ๐Ÿ”— 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.