99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""XQ0085 按值贴入:日期列必须落成 Excel 序列号 + 日期格式,不能退化成文本。
|
|
|
|
回归点:CRM「团购统计」的消费日期原先被写成 inlineStr,透视按日期分组失效;
|
|
新增行还需沿用上一行同列样式,否则日期显示成裸数字。
|
|
运行:.venv\\Scripts\\python.exe -m unittest tests.unit.test_xq0085_paste_date -v
|
|
"""
|
|
import importlib
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import types
|
|
import unittest
|
|
from datetime import date, datetime
|
|
|
|
import openpyxl
|
|
|
|
PLUGIN_DIR = os.path.join(
|
|
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))),
|
|
'plugins', 'XQ0001-0100', 'XQ0085 团购数据核对')
|
|
PKG = 'xq85_paste_test'
|
|
SHEET = '团购统计'
|
|
FMT = 'yyyy\\-mm\\-dd'
|
|
|
|
|
|
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')
|
|
paste = importlib.import_module(PKG + '.logic.paste_to_master')
|
|
return cfg, paste
|
|
|
|
|
|
class PasteDateTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.cfg, self.paste = _load_modules()
|
|
self.path = os.path.join(self.tmp.name, '总表.xlsx')
|
|
self._make_book()
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def _make_book(self):
|
|
wb = openpyxl.Workbook()
|
|
ws = wb.active
|
|
ws.title = SHEET
|
|
for c, title in enumerate(['团购类型', '消费日期', '门店', '项目', '个数', '总价'], start=1):
|
|
ws.cell(1, c, title)
|
|
for r in range(2, 5):
|
|
ws.cell(r, 1, '美团')
|
|
cell = ws.cell(r, 2, datetime(2026, 9, 29))
|
|
cell.number_format = FMT
|
|
ws.cell(r, 3, '门店{}'.format(r))
|
|
ws.cell(r, 4, '项目')
|
|
ws.cell(r, 5, 1)
|
|
ws.cell(r, 6, 100)
|
|
wb.save(self.path)
|
|
|
|
def test_datetime_written_as_serial(self):
|
|
serial = self.paste._make_cell(9, 2, datetime(2026, 9, 30))
|
|
self.assertNotIn('inlineStr', serial)
|
|
self.assertIn('<v>', serial)
|
|
|
|
plain = self.paste._make_cell(9, 3, '美团')
|
|
self.assertIn('inlineStr', plain)
|
|
|
|
def test_patch_keeps_date_type_and_format(self):
|
|
rows = [['美团', datetime(2026, 9, 30), '门店A', '项目', 2, 258]] * 5
|
|
self.paste._write_master(self.path, [(SHEET, rows, 1, 2)])
|
|
|
|
wb = openpyxl.load_workbook(self.path, read_only=True)
|
|
try:
|
|
ws = wb[SHEET]
|
|
for r in (2, 5, 6):
|
|
cell = ws.cell(row=r, column=2)
|
|
self.assertIsInstance(cell.value, datetime, '第{}行日期应为 datetime'.format(r))
|
|
self.assertEqual(cell.value.date(), date(2026, 9, 30))
|
|
self.assertEqual(cell.number_format, FMT, '第{}行日期格式丢失'.format(r))
|
|
self.assertEqual(ws.cell(row=6, column=1).value, '美团')
|
|
self.assertEqual(ws.cell(row=6, column=5).value, 2)
|
|
finally:
|
|
wb.close()
|
|
|
|
def test_empty_date_stays_empty(self):
|
|
rows = [['美团', None, '门店A', '项目', 1, 1]]
|
|
self.paste._write_master(self.path, [(SHEET, rows, 1, 2)])
|
|
wb = openpyxl.load_workbook(self.path, read_only=True)
|
|
try:
|
|
self.assertIsNone(wb[SHEET].cell(row=2, column=2).value)
|
|
finally:
|
|
wb.close()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|