83 lines
3.6 KiB
Python
83 lines
3.6 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest import mock
|
|
|
|
from rsi_loop import config, review_mining
|
|
|
|
# 2026-11-02 是周一(北京时间),2026-11-03 是周二
|
|
MON = 1793548800000 + 3600_000 * 4 # 2026-11-02 04:00 北京
|
|
TUE = MON + 86_400_000
|
|
|
|
|
|
class LabelTest(unittest.TestCase):
|
|
def test_batches_posted_and_failures_isolated(self):
|
|
pending = {"total": 3, "batches": [{"keys": ["a"], "prompt": "p1"}, {"keys": ["b"], "prompt": "p2"}]}
|
|
answers = [RuntimeError("boom"), {"labels": [{"key": "b", "cat": "ops"}]}]
|
|
posted = []
|
|
|
|
def ask_json(prompt, timeout=0):
|
|
a = answers.pop(0)
|
|
if isinstance(a, Exception):
|
|
raise a
|
|
return a
|
|
|
|
with mock.patch.object(review_mining.api, "get", return_value=pending), \
|
|
mock.patch.object(review_mining.llm, "ask_json", side_effect=ask_json), \
|
|
mock.patch.object(review_mining.api, "post", side_effect=lambda path, body, **k: posted.append((path, body)) or {"saved": 1, "dropped": 0}):
|
|
out = review_mining.label_pending()
|
|
self.assertEqual(out["failedBatches"], 1)
|
|
self.assertEqual(out["saved"], 1)
|
|
self.assertEqual(posted[0][0], "/review-mining/labels")
|
|
self.assertEqual(posted[0][1]["labeler"], review_mining.LABELER)
|
|
|
|
def test_days_query(self):
|
|
with mock.patch.object(review_mining.api, "get", return_value={"batches": []}) as g:
|
|
review_mining.label_pending(days=180)
|
|
g.assert_called_with("/review-mining/pending?days=180", timeout=300)
|
|
|
|
|
|
class ClusterTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.marker = Path(self.tmp.name) / "last"
|
|
self.patch = mock.patch.object(review_mining, "LAST_CLUSTER", self.marker)
|
|
self.patch.start()
|
|
|
|
def tearDown(self):
|
|
self.patch.stop()
|
|
self.tmp.cleanup()
|
|
|
|
def test_only_on_monday_once(self):
|
|
self.assertFalse(review_mining._cluster_due(TUE))
|
|
self.assertTrue(review_mining._cluster_due(MON))
|
|
inp = {"rows": 2, "prompt": "p", "window": {"from": 1, "to": 2}}
|
|
with mock.patch.object(review_mining.api, "get", return_value=inp), \
|
|
mock.patch.object(review_mining.llm, "ask_json", return_value={"candidates": [{"family": "x"}]}), \
|
|
mock.patch.object(review_mining.api, "post", return_value={"saved": 1, "rejected": []}) as post, \
|
|
mock.patch.object(config, "DRY_RUN", False):
|
|
out = review_mining.cluster(MON)
|
|
self.assertEqual(out["saved"], 1)
|
|
self.assertEqual(post.call_args[0][1]["window"], {"from": 1, "to": 2})
|
|
self.assertFalse(review_mining._cluster_due(MON)) # 同一天不再跑
|
|
|
|
def test_force_and_empty_rows(self):
|
|
with mock.patch.object(review_mining.api, "get", return_value={"rows": 0, "window": {}}), \
|
|
mock.patch.object(review_mining.llm, "ask_json") as ask, \
|
|
mock.patch.object(config, "DRY_RUN", True):
|
|
out = review_mining.cluster(TUE, force=True)
|
|
ask.assert_not_called()
|
|
self.assertEqual(out["rows"], 0)
|
|
self.assertFalse(self.marker.exists()) # 试运行不落标记
|
|
|
|
def test_run_isolates_errors(self):
|
|
with mock.patch.object(review_mining, "label_pending", side_effect=RuntimeError("down")), \
|
|
mock.patch.object(review_mining, "cluster", return_value=None):
|
|
out = review_mining.run(TUE)
|
|
self.assertIn("error", out["label"])
|
|
self.assertIn("打标签失败", review_mining.summary_line(out))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|