Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

在庫アラート通知システム

概要

FastAPI / Celery / Redis / WebSocket を用いた在庫アラート通知システムです。
在庫が閾値を下回った商品を検知し、アラートを生成、WebSocketを通じてリアルタイム通知を行います。

Swagger UI

Swagger UI

商品一覧

商品一覧

在庫アラート一覧

在庫アラート一覧

特徴

  • 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

API エンドポイント

items (商品の登録・更新)

POST /items/            # 商品登録
GET  /items/            # 商品一覧
PUT  /items/{item_id}   # 在庫・閾値の更新

Alert (在庫通知履歴)

GET  /alerts/           # 全アラート一覧
GET  /alerts/{item_id}  # 商品ごとのアラート一覧

WebSocket (リアルタイム通知)

/ws/alerts      # 在庫アラートのリアルタイム配信

在庫チェックタスク

Celery Worker が Redis を介して定期的に在庫をチェックし、閾値以下の商品を検知すると、Alert を生成、WebSocket 経由で通知します。

テストについて

全テスト実行

poetry run pytest -v

coverage 計測

poetry run coverage run -m pytest -v poetry run coverage report

HTMLレポート生成

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

このプロジェクトは coverage 100% を達成しています。 coverage

Docker

以下のコマンドで Docker を起動します。(FastAPI / Redis / Celery Worker が起動)

docker-compose up --build

License

This project is licensed under the MIT License.

About

FastAPI / Celery / Redis / WebSocket を用いた在庫アラート通知システム

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages