Files
crypto_monitor/tests/test_hub_system_logs_lib.py
T

48 lines
1.7 KiB
Python

"""hub_system_logs_lib 单元测试."""
from __future__ import annotations
import tempfile
import unittest
from pathlib import Path
from lib.hub.hub_system_logs_lib import (
load_system_logs,
log_file_paths,
system_logs_meta,
tail_lines,
)
class HubSystemLogsLibTest(unittest.TestCase):
def test_system_logs_meta(self):
meta = system_logs_meta()
self.assertTrue(meta["ok"])
keys = [t["key"] for t in meta["targets"]]
self.assertEqual(keys, ["binance", "gate", "okx", "hub"])
def test_tail_lines_reads_last_lines(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "demo-out.log"
path.write_text("\n".join(f"line-{i}" for i in range(1, 11)), encoding="utf-8")
out = tail_lines(path, lines=3)
self.assertEqual(out.splitlines(), ["line-8", "line-9", "line-10"])
def test_load_system_logs_unknown(self):
with self.assertRaises(KeyError):
load_system_logs("unknown")
def test_load_system_logs_missing_files(self):
with tempfile.TemporaryDirectory() as tmp:
logs_dir = Path(tmp)
out_path, err_path = log_file_paths("crypto_gate")
# monkeypatch by writing into expected structure under temp is hard;
# just verify shape with real call (files may be absent on dev machine).
payload = load_system_logs("gate", lines=50)
self.assertTrue(payload["ok"])
self.assertEqual(payload["key"], "gate")
self.assertIn("out", payload)
self.assertIn("err", payload)
self.assertIsInstance(payload["out"], str)
self.assertIsInstance(payload["err"], str)
_ = out_path, err_path