FastAPI / Celery / Redis / WebSocket を用いた在庫アラート通知システムです。
在庫が閾値を下回った商品を検知し、アラートを生成、WebSocketを通じてリアルタイム通知を行います。
- FastAPIによる REST API
- Celery + Redis による在庫チェックの非同期タスク
- WebSocket によるリアルタイムアラート配信
- SQLAlchemy によるDB管理
- pytest + coverage による自動テスト
- AsyncMock / mocker を用いた非同期処理・WebSocket のモックテスト
- coverage による100%テストカバレッジ
- Docker Compose による開発環境構築
- FastAPI
- Celery
- Redis
- SQLAlchemy
- WebSocket
- pytest / coverage
- AsyncMock / unittest.mock / pytest-mocker
- Docker / Docker Compose
- poetry
app/
├── main.py
├── core/
├── celery_app.py
└── config.py
├── db
├── base_class.py
└── session.py
├── routers/
├── alerts.py
└── items.py
├── models/
├── alert.py
└── item.py
├── schemas/
├── alert.py
└── item.py
├── services/
├── alert_service.py
└── inventory_service.py
├── tasks/
└── check_stock.py
├── ws/
├── alerts_ws.py
└── manager.py
└── tests/
├── conftest.py
├── test_alerts.py
├── test_celery_task.py
├── test_inventory_service.py
├── test_items.py
└── test_websocket.py
docker-compose.yml
Dockerfile
pyproject.toml
README.md
POST /items/ # 商品登録
GET /items/ # 商品一覧
PUT /items/{item_id} # 在庫・閾値の更新
GET /alerts/ # 全アラート一覧
GET /alerts/{item_id} # 商品ごとのアラート一覧
/ws/alerts # 在庫アラートのリアルタイム配信
Celery Worker が Redis を介して定期的に在庫をチェックし、閾値以下の商品を検知すると、Alert を生成、WebSocket 経由で通知します。
poetry run pytest -v
poetry run coverage run -m pytest -v poetry run coverage report
poetry run coverage html
htmlcov/index.html をブラウザで開くと詳細が確認できます。
- AsyncMock を用いた非同期関数のモック
- pytest-mocker による関数・オブジェクトの差し替え
- WebSocket の send_text / receive_text をモックして動作を検証
- Celery タスク内の SessionLocal をモックし、テスト用 DB を強制使用
- 例外系テスト(check_item_stock の強制例外送出)
- ビジネスロジックを持たないインフラ層を除外(pragma: no cover)した、意味のある100%テストカバレッジ
このプロジェクトは coverage 100% を達成しています。

以下のコマンドで Docker を起動します。(FastAPI / Redis / Celery Worker が起動)
docker-compose up --build
This project is licensed under the MIT License.


