samchun-gemini / tests /test_job_manager.py
JHyeok5's picture
Upload folder using huggingface_hub
01057f3 verified
"""
Tests for job_manager module
Note: These tests require SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY to be set
"""
import pytest
import asyncio
import os
from datetime import datetime
from lib.job_manager import update_job_progress, complete_job, fail_job, get_job_status
from lib.supabase_client import get_supabase_client, reset_supabase_client
@pytest.fixture
def test_job_id():
"""Create a test job and return its ID"""
# Skip if Supabase credentials not available
if not os.getenv("SUPABASE_URL") or not os.getenv("SUPABASE_SERVICE_ROLE_KEY"):
pytest.skip("Supabase credentials not configured")
supabase = get_supabase_client()
# Create test job
result = supabase.table("course_generation_jobs").insert({
"user_id": "00000000-0000-0000-0000-000000000000", # Test user
"status": "pending",
"progress": 0,
"progress_message": "Test job"
}).execute()
job_id = result.data[0]["id"]
yield job_id
# Cleanup
supabase.table("course_generation_jobs").delete().eq("id", job_id).execute()
@pytest.mark.asyncio
async def test_update_job_progress(test_job_id):
"""Test updating job progress"""
success = await update_job_progress(test_job_id, 50, "ν…ŒμŠ€νŠΈ μ§„ν–‰ 쀑")
assert success is True
# Verify update
status = await get_job_status(test_job_id)
assert status["progress"] == 50
assert status["progress_message"] == "ν…ŒμŠ€νŠΈ μ§„ν–‰ 쀑"
@pytest.mark.asyncio
async def test_complete_job(test_job_id):
"""Test completing a job"""
result_data = {
"courses": [{"title": "ν…ŒμŠ€νŠΈ μ½”μŠ€"}],
"success": True
}
success = await complete_job(test_job_id, result_data)
assert success is True
# Verify completion
status = await get_job_status(test_job_id)
assert status["status"] == "completed"
assert status["progress"] == 100
assert status["result"] == result_data
assert status["completed_at"] is not None
@pytest.mark.asyncio
async def test_fail_job(test_job_id):
"""Test failing a job"""
error_message = "ν…ŒμŠ€νŠΈ μ—λŸ¬"
success = await fail_job(test_job_id, error_message)
assert success is True
# Verify failure
status = await get_job_status(test_job_id)
assert status["status"] == "failed"
assert status["error"] == error_message
assert status["completed_at"] is not None
@pytest.mark.asyncio
async def test_job_progress_sequence(test_job_id):
"""Test a complete job lifecycle"""
# Start
await update_job_progress(test_job_id, 10, "μ‹œμž‘")
# Progress
await update_job_progress(test_job_id, 50, "μ§„ν–‰ 쀑")
# Complete
await complete_job(test_job_id, {"test": "μ™„λ£Œ"})
# Verify final state
status = await get_job_status(test_job_id)
assert status["status"] == "completed"
assert status["progress"] == 100
@pytest.mark.asyncio
async def test_nonexistent_job():
"""Test handling of nonexistent job ID"""
fake_job_id = "00000000-0000-0000-0000-000000000001"
# Should return False but not raise exception
success = await update_job_progress(fake_job_id, 50, "ν…ŒμŠ€νŠΈ")
assert success is False
status = await get_job_status(fake_job_id)
assert status is None
def test_missing_credentials():
"""Test handling of missing Supabase credentials"""
# Clear env vars
original_url = os.getenv("SUPABASE_URL")
original_key = os.getenv("SUPABASE_SERVICE_ROLE_KEY")
os.environ.pop("SUPABASE_URL", None)
os.environ.pop("SUPABASE_SERVICE_ROLE_KEY", None)
reset_supabase_client()
with pytest.raises(ValueError, match="SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY must be set"):
get_supabase_client()
# Restore
if original_url:
os.environ["SUPABASE_URL"] = original_url
if original_key:
os.environ["SUPABASE_SERVICE_ROLE_KEY"] = original_key
reset_supabase_client()