import importlib import os import sys import unittest from pathlib import Path from tempfile import TemporaryDirectory from unittest.mock import patch from openpyxl import load_workbook ROOT = Path(__file__).resolve().parent sys.path.insert(0, str(ROOT)) from tools.im_approval_creation import ( # noqa: E402 DECISION_COL_LETTER, generate_approval_xlsx, ) import tools.im_approval_creation as approval_module # noqa: E402 def _sample_record() -> dict: return { "approval_date": "2026-07-08", "account_id": 86197363, "audience_tier": "回流330以上人群", "adgroup_id": 115273005955, "adgroup_name": "回流330以上人群-20260708-关键页面-targeted", "bid_amount_yuan": "0.40", "site_set": "微信公众号,朋友圈", "age_range": "45-66", "landing_video_id": 63747053, "landing_video_url": "https://example.com/video.mp4", "landing_title": "测试落地视频", "landing_risk_level": 1, "landing_risk_tag_ids": "85856", "landing_risk_reason": "risk level 1 <= allowed 5", "material_source": "ai_generated", "material_cover_url": "https://rescdn.yishihui.com/auto_put_tencent/image/test.jpg", "creative_name": "touliu_tencent_20260708_63747053_test", "_description_contents": ["打开看看"], } class CreativeApprovalSheetTest(unittest.TestCase): def test_embeds_material_preview_images_by_default(self): with patch.dict(os.environ, {}, clear=False): os.environ.pop("CREATION_APPROVAL_EMBED_IMAGES", None) reloaded = importlib.reload(approval_module) self.assertTrue(reloaded.EMBED_MATERIAL_PREVIEW_IMAGES) def test_keeps_dedicated_material_link_column(self): with TemporaryDirectory() as tmp: output = Path(tmp) / "approval.xlsx" generate_approval_xlsx([_sample_record()], output) wb = load_workbook(output, data_only=False) ws = wb.active headers = [ws.cell(1, col).value for col in range(1, ws.max_column + 1)] self.assertIn("素材预览", headers) self.assertIn("素材链接", headers) self.assertEqual("决策", headers[-1]) self.assertEqual("AD", DECISION_COL_LETTER) material_link_col = headers.index("素材链接") + 1 self.assertEqual( '=HYPERLINK("https://rescdn.yishihui.com/auto_put_tencent/image/test.jpg","打开素材")', ws.cell(2, material_link_col).value, ) if __name__ == "__main__": unittest.main()