第四章练习答案
练习 1:完整测试套件
python
import pytest
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
class TestUserAPI:
def test_create_user(self):
response = client.post("/users/", json={
"email": "test@example.com",
"username": "testuser",
"password": "password123"
})
assert response.status_code == 201
data = response.json()
assert data["email"] == "test@example.com"
assert "hashed_password" not in data
def test_list_users(self):
response = client.get("/users/")
assert response.status_code == 200
assert isinstance(response.json(), list)
def test_get_user_not_found(self):
response = client.get("/users/99999")
assert response.status_code == 404
@pytest.mark.parametrize("email,username,expected_status", [
("valid@example.com", "validuser", 201),
("invalid", "user", 422),
("a@b.com", "ab", 422), # username too short
])
def test_create_user_validation(self, email, username, expected_status):
response = client.post("/users/", json={
"email": email,
"username": username,
"password": "password123"
})
assert response.status_code == expected_status练习 2:CI/CD 配置
.github/workflows/test.yml:
yaml
name: Tests
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pytest pytest-cov
- name: Run tests
run: |
pytest --cov=. --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v3练习 3:Docker Compose 完整栈
yaml
version: '3.8'
services:
web:
build:
context: .
dockerfile: Dockerfile
command: gunicorn main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
ports:
- "8000:8000"
environment:
- DATABASE_URL=postgresql://postgres:postgres@db:5432/fastapi
- REDIS_URL=redis://redis:6379
- SECRET_KEY=${SECRET_KEY}
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
interval: 30s
timeout: 10s
retries: 3
db:
image: postgres:15
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=fastapi
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./ssl:/etc/nginx/ssl:ro
depends_on:
- web
volumes:
postgres_data:
redis_data:返回第四章概览