88fe4bbe56
固定 5m/15m/1h/4h 四槽截图上传与详情四宫格展示;自动 K 线默认关闭且与手动上传互斥。 Co-authored-by: Cursor <cursoragent@cursor.com>
92 lines
2.8 KiB
Python
92 lines
2.8 KiB
Python
"""journal_images_lib 单元测试。"""
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from io import BytesIO
|
|
|
|
from lib.instance.journal_images_lib import (
|
|
JOURNAL_UPLOAD_TFS,
|
|
enrich_journal_api_item,
|
|
images_json_dumps,
|
|
journal_image_paths,
|
|
journal_upload_field_name,
|
|
parse_images_json,
|
|
primary_journal_image,
|
|
save_journal_slot_uploads,
|
|
)
|
|
|
|
|
|
class _FakeFile:
|
|
def __init__(self, filename: str, data: bytes):
|
|
self.filename = filename
|
|
self._data = data
|
|
|
|
def save(self, path: str) -> None:
|
|
with open(path, "wb") as f:
|
|
f.write(self._data)
|
|
|
|
|
|
class _FakeFiles:
|
|
def __init__(self, mapping):
|
|
self._mapping = mapping
|
|
|
|
def get(self, key):
|
|
return self._mapping.get(key)
|
|
|
|
|
|
class JournalImagesLibTest(unittest.TestCase):
|
|
def test_field_names(self):
|
|
self.assertEqual(journal_upload_field_name("5m"), "screenshot_5m")
|
|
|
|
def test_save_slot_uploads_partial(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
files = _FakeFiles(
|
|
{
|
|
"screenshot_5m": _FakeFile("a.png", b"png5"),
|
|
"screenshot_1h": _FakeFile("b.jpg", b"jpg1"),
|
|
}
|
|
)
|
|
saved = save_journal_slot_uploads(
|
|
files,
|
|
"abc123",
|
|
tmp,
|
|
secure_filename_fn=lambda x: x,
|
|
)
|
|
self.assertEqual(len(saved), 2)
|
|
self.assertEqual(saved[0]["tf"], "5m")
|
|
self.assertTrue(os.path.isfile(os.path.join(tmp, saved[0]["file"])))
|
|
self.assertEqual(saved[1]["tf"], "1h")
|
|
|
|
def test_parse_and_enrich(self):
|
|
raw = images_json_dumps([{"tf": "5m", "file": "journal_x_5m.png"}])
|
|
item = enrich_journal_api_item({"images_json": raw, "image": "legacy.png"})
|
|
self.assertEqual(len(item["images"]), 1)
|
|
self.assertEqual(item["images"][0]["tf"], "5m")
|
|
|
|
legacy = enrich_journal_api_item({"image": "only.png"})
|
|
self.assertEqual(legacy["images"][0]["file"], "only.png")
|
|
|
|
def test_journal_image_paths_dedupe(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = os.path.join(tmp, "same.png")
|
|
with open(path, "wb") as f:
|
|
f.write(b"x")
|
|
row = {
|
|
"image": "same.png",
|
|
"images_json": json.dumps([{"tf": "5m", "file": "same.png"}]),
|
|
}
|
|
paths = journal_image_paths(row, tmp)
|
|
self.assertEqual(len(paths), 1)
|
|
|
|
def test_primary_journal_image(self):
|
|
self.assertEqual(
|
|
primary_journal_image([{"tf": "5m", "file": "a.png"}]),
|
|
"a.png",
|
|
)
|
|
self.assertIsNone(primary_journal_image([]))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|