from job import run_batch from persist import KIND_OK, STAMP_CONTRACT, STAMP_DONE, AgreementOutcome from repo import MemoryRebateRepo def _t4(**overrides) -> dict: row = { "fid": 1, "oms_id": 2026100501, "action_code": 1, "contract_status": 1, "contract_fail_reason": None, "contract_no": "HT1", "agreement_status": None, "agreement_fail_reason": None, "agreement_id": None, "agreement_dtl_id": None, "org_code": "901", "push_status": 0, "create_user_name": STAMP_CONTRACT, "update_user_name": STAMP_CONTRACT, } row.update(overrides) return row def _t3(**overrides) -> dict: row = { "fid": 10, "oms_id": 2026100503, "contract_no": "HT-OLD", "contract_entry_status": 0, "process_status": 0, "process_result": "", "org_code": "901", } row.update(overrides) return row def test_job_finds_contract_and_writes_agreement(): repo = MemoryRebateRepo( table4=[ _t4(), _t4(fid=2, oms_id=2026100502), _t4(fid=3, contract_status=2, contract_no="HT-DONE", update_user_name=STAMP_DONE), ] ) looked: list[str] = [] recorded: list[str] = [] def finder(contract_no: str) -> bool: looked.append(contract_no) return True def recorder(contract_no: str) -> AgreementOutcome: recorded.append(contract_no) return AgreementOutcome(KIND_OK, "AGR1", "AGRD1", None) summary = run_batch(repo, finder=finder, recorder=recorder) assert summary.claimed == 2 assert summary.ok == 1 assert looked == ["HT1"] assert recorded == ["HT1"] drafts = [row for row in repo.table4.values() if row["fid"] in (1, 2)] assert {row["contract_status"] for row in drafts} == {2} assert {row["action_code"] for row in drafts} == {1} assert {row["update_user_name"] for row in drafts} == {STAMP_DONE} assert {row["push_status"] for row in drafts} == {0} agreements = [row for row in repo.table4.values() if row["action_code"] == 2] assert sorted(row["oms_id"] for row in agreements) == [2026100501, 2026100502] assert {row["contract_no"] for row in agreements} == {"HT1"} assert {row["agreement_status"] for row in agreements} == {1} assert {row["agreement_id"] for row in agreements} == {"AGR1"} assert {row["agreement_dtl_id"] for row in agreements} == {"AGRD1"} assert {row["push_status"] for row in agreements} == {0} assert repo.table4[3]["contract_status"] == 2 assert repo.table4[3]["action_code"] == 1 def test_job_leaves_not_found_for_next_shift(): repo = MemoryRebateRepo(table4=[_t4()]) recorded = {"n": 0} def recorder(contract_no: str) -> AgreementOutcome: recorded["n"] += 1 return AgreementOutcome(KIND_OK, "AGR1", "AGRD1", None) run_batch(repo, finder=lambda _no: False, recorder=recorder) assert recorded["n"] == 0 row = repo.table4[1] assert row["contract_status"] == 1 assert row["action_code"] == 1 assert row["update_user_name"] == STAMP_CONTRACT assert [item["action_code"] for item in repo.table4.values()] == [1] def test_job_claims_protocol_only_and_writes_table4(): repo = MemoryRebateRepo(table3=[_t3(), _t3(fid=11, process_status=1, contract_no="HT-SKIP")]) recorded: list[str] = [] def recorder(contract_no: str) -> AgreementOutcome: recorded.append(contract_no) return AgreementOutcome(KIND_OK, "AGR-OLD", "AGRD-OLD", None) run_batch(repo, finder=lambda _no: True, recorder=recorder) assert recorded == ["HT-OLD"] assert repo.table3[10]["process_status"] == 1 assert repo.table3[11]["process_status"] == 1 success = [row for row in repo.table4.values() if row["action_code"] == 1] agreements = [row for row in repo.table4.values() if row["action_code"] == 2] assert len(success) == 1 assert success[0]["oms_id"] == 2026100503 assert success[0]["contract_status"] == 2 assert success[0]["contract_no"] == "HT-OLD" assert success[0]["push_status"] == 0 assert len(agreements) == 1 assert agreements[0]["agreement_status"] == 1 assert agreements[0]["agreement_id"] == "AGR-OLD" assert agreements[0]["agreement_dtl_id"] == "AGRD-OLD"