M2 (Backend): - Add tests/ folder with pytest test files - Add pytest.ini for test configuration and coverage - Update scoring to require unit tests (Score 4+) M3 (Frontend): - Add tests/ folder with Vitest component unit tests - Add playwright/e2e/ folder with E2E Playwright tests - Add @playwright/test dependency and configuration - Update scoring to require both unit and E2E tests (Score 5) README.md updated with all milestone links and test requirements
306 lines
9.0 KiB
Markdown
306 lines
9.0 KiB
Markdown
# M2: Setup Backend
|
|
|
|
## Prompt
|
|
|
|
Create the backend infrastructure for **newzy** news web application using Python FastAPI. Follow these steps:
|
|
|
|
1. Set up project structure in `backend/` folder:
|
|
```
|
|
newzy/
|
|
├── backend/
|
|
│ ├── api/
|
|
│ │ └── v1/
|
|
│ │ └── __init__.py
|
|
│ ├── core/
|
|
│ │ └── __init__.py
|
|
│ ├── db/
|
|
│ │ └── session.py
|
|
│ ├── models/
|
|
│ │ └── __init__.py
|
|
│ ├── schemas/
|
|
│ │ └── __init__.py
|
|
│ ├── tests/
|
|
│ │ ├── conftest.py
|
|
│ │ ├── test_health.py
|
|
│ │ ├── test_version.py
|
|
│ │ └── test_models.py
|
|
│ ├── main.py
|
|
│ ├── config.py
|
|
│ ├── requirements.txt
|
|
│ ├── Dockerfile
|
|
│ ├── .gitignore
|
|
│ ├── pytest.ini
|
|
│ └── README.md
|
|
```
|
|
|
|
2. Create FastAPI application in `backend/main.py` with:
|
|
- Health check endpoint (`GET /health`)
|
|
- Version endpoint (`GET /api/v1/version`)
|
|
|
|
3. Set up SQLAlchemy database session using SQLite (configured for Docker volume persistence)
|
|
|
|
4. Define Pydantic schemas for basic models (Article, Author, Category)
|
|
|
|
5. Configure `requirements.txt` with:
|
|
- FastAPI
|
|
- uvicorn
|
|
- SQLAlchemy
|
|
- pydantic
|
|
- python-multipart
|
|
- pytest and pytest-asyncio for testing
|
|
|
|
6. Create initial database models for:
|
|
- Article (title, content, author_id, category_id, created_at, published)
|
|
- Author (name, email, bio)
|
|
- Category (name, slug)
|
|
|
|
7. Add proper exception handling and error responses
|
|
|
|
8. Document API endpoints in `backend/README.md` with examples
|
|
|
|
9. Create `.gitignore` to exclude Python artifacts, env files, and IDE configs
|
|
|
|
10. Create `Dockerfile` for containerization with:
|
|
- Base Python image
|
|
- Install requirements
|
|
- Copy application code
|
|
- Expose port 8000
|
|
- CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
|
|
11. Add `pytest.ini` with test configuration and coverage settings
|
|
|
|
12. Create unit tests in `backend/tests/`:
|
|
- Test health endpoint returns 200 OK
|
|
- Test version endpoint returns correct format
|
|
- Test model instantiation and database operations
|
|
- Use pytest fixtures for database setup/teardown
|
|
|
|
13. Add docker-compose.yml at project root with backend service
|
|
|
|
Remember: Focus ONLY on backend infrastructure. No frontend implementation required.
|
|
|
|
## Description
|
|
|
|
This milestone sets up the FastAPI backend for newzy news application using Python. The scope includes database configuration, API structure, core models, and basic endpoints—no complex business logic or integration with other services.
|
|
|
|
**Purpose**: Create a functional REST API backbone ready for feature development in subsequent milestones.
|
|
|
|
**Target Audience**: Backend developers joining the project who need to understand and extend the API.
|
|
|
|
**Expected Outcomes**: A containerized FastAPI server with database connectivity, core models defined, documented endpoints, and comprehensive unit tests demonstrating proper implementation.
|
|
|
|
## Acceptance Criteria
|
|
|
|
- [ ] FastAPI application created in `backend/main.py`
|
|
- [ ] SQLAlchemy session configured with SQLite backend
|
|
- [ ] Database models defined: Article, Author, Category
|
|
- [ ] Pydantic schemas implemented for all models
|
|
- [ ] Health check endpoint (`GET /health`) returns 200 OK
|
|
- [ ] Version endpoint (`GET /api/v1/version`) returns application version
|
|
- [ ] `requirements.txt` lists all Python dependencies including testing libs
|
|
- [ ] Proper error handling with standardized response format
|
|
- [ ] `backend/README.md` documents API endpoints with examples
|
|
- [ ] `.gitignore` file created with appropriate exclusions
|
|
- [ ] `Dockerfile` creates runnable container image
|
|
- [ ] Unit tests in `backend/tests/` covering health, version, and models
|
|
- [ ] pytest.ini configured for test discovery and coverage
|
|
- [ ] Code follows FastAPI and SQLAlchemy best practices
|
|
|
|
## Technical Details
|
|
|
|
### Project Root Structure
|
|
```
|
|
project-root/
|
|
├── backend/
|
|
│ ├── api/
|
|
│ │ └── v1/
|
|
│ │ └── __init__.py
|
|
│ ├── core/
|
|
│ ├── db/
|
|
│ ├── models/
|
|
│ ├── schemas/
|
|
│ ├── tests/
|
|
│ │ ├── conftest.py
|
|
│ │ ├── test_health.py
|
|
│ │ ├── test_version.py
|
|
│ │ └── test_models.py
|
|
│ ├── main.py
|
|
│ ├── config.py
|
|
│ ├── requirements.txt
|
|
│ ├── Dockerfile
|
|
│ ├── .gitignore
|
|
│ ├── pytest.ini
|
|
│ └── README.md
|
|
├── LICENSE # MIT License
|
|
├── README.md
|
|
└── CONTRIBUTING.md
|
|
```
|
|
|
|
### Database Schema (SQLite)
|
|
|
|
**Article Model:**
|
|
- `id`: Integer, primary key
|
|
- `title`: String(200), unique
|
|
- `content`: Text
|
|
- `author_id`: Integer, foreign key to Author
|
|
- `category_id`: Integer, foreign key to Category
|
|
- `created_at`: DateTime
|
|
- `published`: Boolean, default False
|
|
|
|
**Author Model:**
|
|
- `id`: Integer, primary key
|
|
- `name`: String(100), unique
|
|
- `email`: String(255), unique
|
|
- `bio`: Text (nullable)
|
|
|
|
**Category Model:**
|
|
- `id`: Integer, primary key
|
|
- `name`: String(100), unique
|
|
- `slug`: String(100), unique
|
|
|
|
### Backend API Endpoints
|
|
|
|
| Method | Path | Description |
|
|
|--------|------|-------------|
|
|
| GET | /health | Health check endpoint |
|
|
| GET | /api/v1/version | Returns application version |
|
|
| GET | /api/v1/health | Alternative health check path |
|
|
|
|
### .gitignore Contents for backend/
|
|
```python
|
|
__pycache__/
|
|
*.py[cod]
|
|
*.egg-info/
|
|
.env
|
|
.venv/
|
|
venv/
|
|
*.db
|
|
*.sqlite
|
|
Dockerfile
|
|
.docker/
|
|
.DS_Store
|
|
.idea/
|
|
.vscode/
|
|
*~
|
|
.coverage
|
|
htmlcov/
|
|
.pytest_cache/
|
|
```
|
|
|
|
### Dockerfile
|
|
```dockerfile
|
|
FROM python:3.11-slim
|
|
|
|
WORKDIR /app
|
|
|
|
# Install dependencies
|
|
COPY requirements.txt .
|
|
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
# Copy application code
|
|
COPY . .
|
|
|
|
# Expose port
|
|
EXPOSE 8000
|
|
|
|
# Run application
|
|
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
|
```
|
|
|
|
### pytest.ini Configuration
|
|
```ini
|
|
[pytest]
|
|
testpaths = tests
|
|
python_files = test_*.py
|
|
python_classes = Test*
|
|
python_functions = test_*
|
|
asyncio_mode = auto
|
|
addopts = -v --cov=. --cov-report=term-missing
|
|
filterwarnings = ignore::DeprecationWarning
|
|
```
|
|
|
|
### docker-compose.yml (project root)
|
|
```yaml
|
|
version: '3.8'
|
|
|
|
services:
|
|
backend:
|
|
build: ./backend
|
|
ports:
|
|
- "8000:8000"
|
|
volumes:
|
|
- ./data/db:/app/data/db
|
|
environment:
|
|
- DATABASE_URL=sqlite:///./data/db/newzy.db
|
|
```
|
|
|
|
### Unit Tests Overview
|
|
|
|
**test_health.py:**
|
|
- Test health endpoint returns 200 status code
|
|
- Verify response contains expected JSON structure
|
|
- Test alternative /api/v1/health path
|
|
|
|
**test_version.py:**
|
|
- Test version endpoint returns valid version string
|
|
- Response format validation (e.g., "v0.1.0")
|
|
|
|
**test_models.py:**
|
|
- Test Article model creation and commit to database
|
|
- Test Author model creation with unique constraints
|
|
- Test Category model slug generation
|
|
- Verify foreign key relationships work correctly
|
|
|
|
### Configuration
|
|
- Environment variables for database URL (DATABASE_URL)
|
|
- Docker volume configuration: `/data/db` mapped to SQLite file
|
|
- CORS enabled for frontend development
|
|
- Uvicorn workers configured for production deployment
|
|
- Pytest configured for coverage reporting
|
|
|
|
## QA Notes
|
|
|
|
### Testing Checklist
|
|
- [ ] Application starts without errors
|
|
- [ ] Health endpoint returns 200 OK with JSON response
|
|
- [ ] Version endpoint returns correct format (version string)
|
|
- [ ] Database connection successful
|
|
- [ ] Models can be instantiated and committed
|
|
- [ ] Error responses follow standardized format
|
|
- [ ] No unhandled exceptions in basic operations
|
|
- [ ] Docker container builds successfully
|
|
- [ ] Container runs and serves endpoints
|
|
- [ ] pytest discovers and runs all test files
|
|
- [ ] All unit tests pass (100% expected coverage)
|
|
|
|
### Performance Considerations
|
|
- Single-threaded async mode for development (uvicorn workers = 1)
|
|
- SQLite file-based storage (no connection pool needed yet)
|
|
- Lazy database session management
|
|
- Docker Compose enables easy local development setup
|
|
- Tests use fast in-memory or temporary database files
|
|
|
|
### Security Notes
|
|
- Password hashing to be implemented in future milestones
|
|
- Authentication mechanism TBD
|
|
- CORS configuration should be validated before production
|
|
- Environment variables for secrets should use .env files
|
|
- pytest cache directories excluded from git
|
|
|
|
### Known Issues / Limitations
|
|
- No input validation beyond Pydantic schemas
|
|
- No rate limiting implemented
|
|
- Database schema not yet optimized for performance
|
|
- Missing pagination and filtering on article endpoints
|
|
- SQLite not suitable for high-concurrency production workloads
|
|
- Tests don't cover edge cases or error scenarios extensively
|
|
|
|
### Success Metrics
|
|
- Application starts in under 10 seconds locally
|
|
- Health check responds within 100ms
|
|
- All models query successfully without errors
|
|
- Docker image builds and runs cleanly
|
|
- README examples work as documented
|
|
- Unit tests achieve >=70% code coverage on core modules
|
|
- All tests complete in under 30 seconds
|