119 lines
3.7 KiB
Python
119 lines
3.7 KiB
Python
"""
|
|
tests/jobs/test_store.py
|
|
|
|
用途:验证 store.claim_next 归一化与空队列行为。
|
|
"""
|
|
from app.jobs.store import _normalize_generic_job, _normalize_kb_job
|
|
from app.jobs.types import JobType
|
|
from app.models import Job, KbJob
|
|
|
|
|
|
def test_normalize_kb_job_clean(app):
|
|
with app.app_context():
|
|
job = KbJob(
|
|
job_type='clean',
|
|
status='running',
|
|
progress=1,
|
|
payload_json={'document_version_id': 42},
|
|
)
|
|
normalized = _normalize_kb_job(job)
|
|
assert normalized['id'] == job.id
|
|
assert normalized['job_type'] == JobType.KB_CLEAN
|
|
assert normalized['raw_job_type'] == 'clean'
|
|
assert normalized['payload']['job_id'] == job.id
|
|
assert normalized['payload']['document_version_id'] == 42
|
|
|
|
|
|
def test_normalize_kb_job_sync_dify(app):
|
|
with app.app_context():
|
|
job = KbJob(job_type='sync_dify', status='running', progress=1)
|
|
normalized = _normalize_kb_job(job)
|
|
assert normalized['job_type'] == JobType.KB_DIFY_SYNC
|
|
|
|
|
|
def test_claim_next_returns_none_when_empty(app):
|
|
from app.jobs import store
|
|
|
|
with app.app_context():
|
|
assert store.claim_next(queues=['kb']) is None
|
|
|
|
|
|
def test_claim_next_shipping_recovers_stale_layout_jobs(app, monkeypatch):
|
|
"""Shipping worker 认领前必须回收卡住的版面解析任务。"""
|
|
from app.jobs import store
|
|
from app.services.shipping_notice import queue_service
|
|
|
|
recovered = []
|
|
monkeypatch.setattr(
|
|
queue_service,
|
|
'recover_stale_shipping_notice_jobs',
|
|
lambda: recovered.append(True) or (0, ''),
|
|
)
|
|
|
|
with app.app_context():
|
|
assert store.claim_next(queues=['shipping']) is None
|
|
|
|
assert recovered == [True]
|
|
|
|
|
|
def test_claim_next_shipping_claims_inventory_callback_process_job(app):
|
|
"""库存 RPA 回调收件 Job 必须由 shipping worker 消费,不能永久停在 queued。"""
|
|
from app import db
|
|
from app.jobs import store
|
|
|
|
with app.app_context():
|
|
job = Job(
|
|
domain='shipping_notice',
|
|
job_type=JobType.SHIPPING_INVENTORY_CALLBACK_PROCESS.value,
|
|
status='queued',
|
|
payload_json={'callback_id': 'callback-test'},
|
|
)
|
|
db.session.add(job)
|
|
db.session.commit()
|
|
|
|
claimed = store.claim_next(queues=['shipping'])
|
|
|
|
assert claimed is not None
|
|
assert claimed['id'] == job.id
|
|
assert claimed['job_type'] == JobType.SHIPPING_INVENTORY_CALLBACK_PROCESS
|
|
|
|
|
|
def test_find_shipping_notice_job_id_matches_task_no(app):
|
|
from app import db
|
|
from app.jobs.queue import _find_shipping_notice_job_id
|
|
|
|
with app.app_context():
|
|
target = KbJob(
|
|
job_type=JobType.SHIPPING_NOTICE_RUN.value,
|
|
status='queued',
|
|
progress=0,
|
|
payload_json={'task_no': 'SHIP-A'},
|
|
)
|
|
newer_other = KbJob(
|
|
job_type=JobType.SHIPPING_NOTICE_RUN.value,
|
|
status='queued',
|
|
progress=0,
|
|
payload_json={'task_no': 'SHIP-B'},
|
|
)
|
|
db.session.add_all([target, newer_other])
|
|
db.session.commit()
|
|
|
|
assert _find_shipping_notice_job_id('ship-a') == target.id
|
|
|
|
|
|
def test_normalize_schedule_generic_job(app):
|
|
"""断言 schedule 通用 Job 会携带 job_id/run_id 交给 handler。"""
|
|
with app.app_context():
|
|
job = Job(
|
|
id=99,
|
|
domain='schedule',
|
|
job_type=JobType.SCHEDULE_RUN.value,
|
|
status='running',
|
|
attempts=1,
|
|
payload_json={'schedule_run_id': 12},
|
|
)
|
|
normalized = _normalize_generic_job(job)
|
|
assert normalized['job_type'] == JobType.SCHEDULE_RUN
|
|
assert normalized['attempts'] == 1
|
|
assert normalized['payload'] == {'job_id': 99, 'schedule_run_id': 12}
|