CaiWuXiTong/tests/scripts/test_prepare_supplier_bookkeeping_shipping.py

65 lines
3.5 KiB
Python

import csv
import gzip
import importlib.util
import json
import tempfile
import unittest
import zipfile
from pathlib import Path
SPEC = importlib.util.spec_from_file_location('prepare_shipping', Path(__file__).resolve().parents[2] / 'scripts/prepare-supplier-bookkeeping-shipping.py')
MODULE = importlib.util.module_from_spec(SPEC)
SPEC.loader.exec_module(MODULE)
class PrepareShippingTest(unittest.TestCase):
def workbook(self, root, date='2026/10/1 0:09:50'):
source = root / 'source.xlsx'
ns = MODULE.NS[1:-1]
headers = ['快递单号', '交易单号', '条形码', '数量', '运费', '发货时间', '结算运费', '收货姓名']
strings = ''.join('<si><t>'+v+'</t></si>' for v in headers)
strings += '<si><r><t>SKU</t></r><r><t>+A</t></r></si>'
head = ''.join(f'<c r="{chr(65+i)}1" t="s"><v>{i}</v></c>' for i in range(8))
row = f'''<c r="A2" t="inlineStr"><is><t>00012345678901234567</t></is></c>
<c r="B2" t="inlineStr"><is><t>ORDER-1,ORDER-2</t></is></c>
<c r="C2" t="s"><v>8</v></c><c r="D2"><v>2</v></c>
<c r="E2"><f>1+2</f><v>3.8</v></c>
<c r="F2" t="inlineStr"><is><t>{date}</t></is></c>
<c r="G2"><v>900</v></c><c r="H2" t="inlineStr"><is><t>PRIVATE</t></is></c>'''
with zipfile.ZipFile(source,'w') as book:
book.writestr('xl/workbook.xml', f'<workbook xmlns="{ns}" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="导出结果" sheetId="1" r:id="rId1"/></sheets></workbook>')
book.writestr('xl/_rels/workbook.xml.rels','<Relationships><Relationship Id="rId1" Target="worksheets/export.xml"/></Relationships>')
book.writestr('xl/sharedStrings.xml', f'<sst xmlns="{ns}">{strings}</sst>')
book.writestr('xl/worksheets/export.xml',f'<worksheet xmlns="{ns}"><sheetData><row r="1">{head}</row><row r="2">{row}</row></sheetData></worksheet>')
return source
def test_preserves_long_identifiers_reads_formula_cache_and_omits_private_fields(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
source = self.workbook(root)
original = source.read_bytes()
output = root/'shipping.csv.gz'
result = MODULE.prepare(source,'2026-10',output,'运费')
self.assertEqual(1,result['rows'])
with gzip.open(output,'rt',encoding='utf-8',newline='') as stream:
row = next(csv.DictReader(stream))
self.assertEqual('00012345678901234567',row['express_no'])
self.assertEqual('SKU+A',row['barcode'])
self.assertEqual('3.8',row['freight'])
self.assertEqual('2026-10-01 00:09:50',row['ship_time'])
self.assertNotIn('PRIVATE',str(row))
meta = json.loads(Path(str(output)+'.json').read_text(encoding='utf-8'))
self.assertEqual(MODULE.digest(output),meta['data_sha256'])
self.assertEqual(original,source.read_bytes())
with self.assertRaises(ValueError): MODULE.prepare(source,'2026-10',output,'运费')
def test_rejects_wrong_month_and_wrong_freight_column(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
source = self.workbook(root)
with self.assertRaises(ValueError): MODULE.prepare(source,'2026-09',root/'wrong-month.csv.gz','运费')
with self.assertRaises(ValueError): MODULE.prepare(source,'2026-10',root/'wrong-column.csv.gz','系统运费')
if __name__ == '__main__': unittest.main()