0
0
FastAPIframework~10 mins

Testing with database in FastAPI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a test client for FastAPI.

FastAPI
from fastapi.testclient import TestClient
from main import app

client = [1](app)
Drag options to blanks, or click blank then click option'
AAppClient
BClient
CFastClient
DTestClient
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong client class name.
Forgetting to import TestClient.
2fill in blank
medium

Complete the code to override the database dependency in FastAPI for testing.

FastAPI
from fastapi import Depends

def override_get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

app.dependency_overrides[[1]] = override_get_db
Drag options to blanks, or click blank then click option'
Adatabase
Bdb_session
Cget_db
Dget_database
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong dependency name.
Not overriding the dependency before tests.
3fill in blank
hard

Fix the error in the test function to correctly use the test client and database session.

FastAPI
def test_create_item():
    response = client.post('/items/', json={'name': 'Test Item'})
    assert response.status_code == [1]
    data = response.json()
    assert data['name'] == 'Test Item'
Drag options to blanks, or click blank then click option'
A201
B404
C400
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Expecting 200 instead of 201.
Confusing client errors with success codes.
4fill in blank
hard

Fill both blanks to create a test database session and override the app dependency.

FastAPI
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args=[1])
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

def override_get_db():
    db = TestingSessionLocal()
    try:
        yield db
    finally:
        db.close()

app.dependency_overrides[[2]] = override_get_db
Drag options to blanks, or click blank then click option'
A{"check_same_thread": False}
B{"check_same_thread": True}
Cget_db
Ddatabase_session
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong connect_args for SQLite.
Overriding the wrong dependency.
5fill in blank
hard

Fill all three blanks to write a test that creates an item and checks the database directly.

FastAPI
def test_create_and_check_db():
    response = client.post('/items/', json=[1])
    assert response.status_code == 201
    data = response.json()
    with TestingSessionLocal() as db:
        item = db.query(Item).filter(Item.name == [2]).first()
        assert item is not None
        assert item.name == [3]
Drag options to blanks, or click blank then click option'
A{'name': 'Test Item'}
B"Test Item"
C'Test Item'
D{'name': 'Another Item'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong data type for JSON payload.
Mismatching quotes in string literals.
Not querying the database correctly.