Files
37duAutomation/tests/unit/test_xq0085_preflight.py
T

116 lines
4.4 KiB
Python

# -*- coding: utf-8 -*-
"""XQ0085 核对前置预检:缺明细 / 日期不符 / 同类多份必须拦截,且不能动表。
运行:.venv\\Scripts\\python.exe -m unittest tests.unit.test_xq0085_preflight -v
"""
import importlib
import os
import sys
import tempfile
import types
import unittest
from datetime import date
PLUGIN_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
'plugins', 'XQ0001-0100', 'XQ0085 团购数据核对')
PKG = 'xq85_preflight_test'
DAY = date(2026, 9, 30)
def _load_modules():
if PKG not in sys.modules:
pkg = types.ModuleType(PKG)
pkg.__path__ = [PLUGIN_DIR]
sys.modules[PKG] = pkg
cfg = importlib.import_module(PKG + '.config')
pf = importlib.import_module(PKG + '.logic.preflight')
return cfg, pf
class PreflightTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.root = self.tmp.name
self.cfg, self.pf = _load_modules()
self.cfg.set_work_dir(self.root, 'check_meituan')
for d in (self.cfg.WORK_DIR, self.cfg.OUT_DIR, os.path.join(self.root, '抖音'),
os.path.join(self.root, 'crm')):
os.makedirs(d, exist_ok=True)
self._touch(os.path.join(self.cfg.WORK_DIR, '美团9.30.xlsx'))
self._touch(os.path.join(self.cfg.WORK_DIR, '抖音9.30.xlsx'))
self._touch(os.path.join(self.cfg.WORK_DIR, '马敏团购总表9.30.xlsx'))
self._touch(os.path.join(self.root, 'crm', '团购支付数据-20260930.xlsx'))
self.crm_dir = os.path.join(self.root, 'crm')
def tearDown(self):
self.tmp.cleanup()
def _touch(self, path):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, 'w', encoding='utf-8') as f:
f.write('x')
def _detail(self, name, folder=None):
path = os.path.join(folder or self.cfg.OUT_DIR, name)
self._touch(path)
return path
def _run(self, action='check_meituan'):
self.cfg.set_work_dir(self.root, action)
return self.pf.run(day=DAY, action=action, crm_dir=self.crm_dir)
def test_all_ready(self):
self._detail('团购收益明细_20260930_20260930.xlsx')
self._detail('预订收益明细_20260930_20260930.xlsx')
res = self._run()
self.assertTrue(res['ok'], res['message'])
self.assertEqual(res['errors'], [])
def test_missing_group_detail_blocks(self):
res = self._run()
self.assertFalse(res['ok'])
self.assertTrue(any('团购收益明细' in c['name'] for c in res['errors']))
def test_booking_detail_optional(self):
self._detail('团购收益明细_20260930_20260930.xlsx')
res = self._run()
self.assertTrue(res['ok'], res['message'])
self.assertTrue(any('预订收益明细' in c['name'] for c in res['warns']))
def test_wrong_day_blocks(self):
self._detail('团购收益明细_20260928_20260929.xlsx')
res = self._run()
self.assertFalse(res['ok'])
self.assertTrue(any('不含营业日' in c['message'] for c in res['errors']))
def test_multiple_files_blocks(self):
self._detail('团购收益明细_20260930_20260930.xlsx')
self._detail('团购收益明细_20260929_20260929.xlsx')
res = self._run()
self.assertFalse(res['ok'])
self.assertTrue(any('串天' in c['message'] for c in res['errors']))
def test_missing_master_blocks(self):
self._detail('团购收益明细_20260930_20260930.xlsx')
os.remove(os.path.join(self.cfg.WORK_DIR, '马敏团购总表9.30.xlsx'))
res = self._run()
self.assertFalse(res['ok'])
self.assertTrue(any('总表' in c['name'] for c in res['errors']))
def test_douyin_branch_needs_detail_only(self):
self._detail('核销明细_2026-09-30_2026-09-30.xlsx',
os.path.join(self.root, '抖音'))
res = self._run('check_douyin')
self.assertTrue(res['ok'], res['message'])
def test_dates_in_name(self):
self.assertEqual(self.pf.dates_in_name('团购收益明细_20260930_20260930.xlsx'), [DAY])
self.assertEqual(self.pf.dates_in_name('核销明细_2026-09-29_2026-09-30.xlsx'),
[date(2026, 9, 29), DAY])
self.assertEqual(self.pf.dates_in_name('团购收益明细.xlsx'), [])
if __name__ == '__main__':
unittest.main()