Baseline merge head e5ff26a: 28144 stmts, 2807 missing, exact 90.0263%, margin +7. After thickening (full run): 2350 missing, exact 91.6501%, margin +464. Full suite 3516 items: 3509 pass, 1 failure (pre-existing November date failure, untouched), 6 skipped. No production code changed.
86 lines
3.3 KiB
Python
86 lines
3.3 KiB
Python
"""Coverage batch v4-D4: cloud/server.py dispatch-safe/upload branches
|
|
and cloud/ledger_outbox.py replay rewrite.
|
|
|
|
Stubbing discipline: the method under test is never stubbed. Patched
|
|
collaborators are: _dispatch / _send_json / _read_body / parse_multipart
|
|
/ parse_paths_field (sinks and data providers). Every assertion reads a
|
|
real return value, a real captured response, or a real file.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(REPO_ROOT))
|
|
|
|
from cloud import server as server_mod
|
|
from cloud.ledger_outbox import replay_failures
|
|
|
|
|
|
class DispatchSafeTestCase(unittest.TestCase):
|
|
def test_dispatch_and_send_both_raise_swallowed(self):
|
|
# 2310-2311: _dispatch raising and the 500 fallback also raising.
|
|
handler = object.__new__(server_mod.CloudControlPlaneHandler)
|
|
handler._responded = False
|
|
handler.path = "/x"
|
|
handler._dispatch = mock.Mock(
|
|
side_effect=RuntimeError("dispatch down"))
|
|
handler._send_json = mock.Mock(
|
|
side_effect=RuntimeError("send down"))
|
|
handler._dispatch_safe("GET")
|
|
handler._send_json.assert_called_once_with(
|
|
500, {"error": "internal_error"})
|
|
|
|
|
|
class UploadMismatchTestCase(unittest.TestCase):
|
|
def test_paths_files_count_mismatch(self):
|
|
# 3149
|
|
handler = object.__new__(server_mod.CloudControlPlaneHandler)
|
|
handler.headers = {"Content-Type": "multipart/form-data",
|
|
"Content-Length": "10"}
|
|
handler._read_body = mock.Mock(return_value=b"body")
|
|
sent = []
|
|
handler._send_json = lambda code, payload: sent.append(
|
|
(code, payload))
|
|
with mock.patch.object(
|
|
server_mod, "parse_multipart",
|
|
return_value=({}, [("a", b"x"), ("b", b"y")])):
|
|
with mock.patch.object(
|
|
server_mod, "parse_paths_field",
|
|
return_value=["only-one"]):
|
|
handler._create_upload("u1")
|
|
self.assertEqual(sent, [(400, {"error": "附件路径与文件数量不一致"})])
|
|
|
|
|
|
class OutboxReplayTestCase(unittest.TestCase):
|
|
def test_failed_record_rewritten_back(self):
|
|
# 170: a failed replay row is rewritten into the outbox file.
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = os.path.join(tmp, "outbox.jsonl")
|
|
good = {"task_id": "t1", "outcome": "success"}
|
|
bad = {"task_id": "t2", "outcome": "failure"}
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
f.write(json.dumps(good) + "\n")
|
|
f.write(json.dumps(bad) + "\n")
|
|
|
|
def _record(conn, task_id, outcome, is_defect=False):
|
|
if task_id == "t2":
|
|
raise RuntimeError("ledger down")
|
|
|
|
stats = replay_failures(path, None, record_fn=_record)
|
|
self.assertEqual(stats["replayed"], 1)
|
|
self.assertEqual(stats["failed"], 1)
|
|
self.assertEqual(stats["remaining"], 1)
|
|
with open(path, encoding="utf-8") as f:
|
|
rows = [json.loads(line) for line in f if line.strip()]
|
|
self.assertEqual(rows, [bad])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|