這個目錄包含了 src/notify.py 中 get_notifies 函數的完整單元測試。測試覆蓋了各種邊界情況和錯誤處理。
test_notify.py- 主要的測試文件run_notify_tests.py- 測試運行腳本
- test_get_notifies_without_gql_endpoint: 測試沒有設置
MESH_GQL_ENDPOINT環境變數的情況
- test_get_notifies_mongodb_connection_failed: 測試 MongoDB 連接失敗的情況
- test_get_notifies_no_record_found: 測試沒有找到通知記錄的情況
- test_get_notifies_with_payment_notifications: 測試支付通知的處理
- test_get_notifies_with_single_notification: 測試單個通知的處理
- test_get_notifies_with_aggregate_notification: 測試聚合通知的處理
- test_get_notifies_with_missing_member: 測試成員信息缺失的情況
- test_get_notifies_with_content_field: 測試包含 content 字段的通知
- test_get_notifies_with_pagination: 測試分頁功能
- test_get_notifies_gql_query_error: 測試 GQL 查詢錯誤的情況
- test_get_notifies_exception_handling: 測試異常處理
python -m unittest test_notify.py -vpython run_notify_tests.py測試使用了以下 Python 模組:
unittest- Python 內建的測試框架unittest.mock- 用於模擬對象和函數os- 環境變數操作copy- 深拷貝操作
{
"_id": "member_id",
"lrt": 1234567890, # 最後讀取時間
"notifies": [
{
"uuid": "notification_uuid",
"action": "follow|like|comment|notify_transaction",
"from": "member_id" | ["member_id1", "member_id2"],
"aggregate": True|False,
"objective": "member|story|payment",
"targetId": "target_id",
"read": True|False,
"ts": 1234567890,
"content": "optional_content" # 可選字段
}
]
}{
"members": [
{
"id": "member_id",
"customId": "custom_id",
"name": "member_name",
"avatar": "avatar_url"
}
]
}每個測試都會驗證:
- 返回值結構: 確保返回的通知結構正確
- 數據完整性: 確保通知數據沒有丟失
- 錯誤處理: 確保異常情況被正確處理
- 函數調用: 確保相關函數被正確調用
- 邊界條件: 確保極端情況被正確處理
- 測試使用了 Mock 對象來模擬外部依賴(MongoDB、GQL)
- 測試會自動清理環境變數
- 所有測試都是獨立的,不會相互影響
- 測試覆蓋了
get_notifies函數的所有主要分支
如果需要添加新的測試案例,請:
- 在
TestGetNotifies類中添加新的測試方法 - 使用描述性的方法名稱(以
test_開頭) - 添加適當的文檔字符串
- 確保測試覆蓋新的功能或邊界情況