Files
cangku/test/test_purchase_order.py
liangweihao 8aa1a5ac91 添加PyQt GUI自动化测试套件
新增5个GUI测试模块,覆盖所有主要功能:
- test_login_gui.py: 登录和密码管理测试(7个测试)
- test_stock_gui.py: 库存管理测试(4个测试)
- test_raw_material_gui.py: 原料管理测试(7个测试)
- test_garment_gui.py: 款式管理测试(2个测试)
- test_purchase_order_gui.py: 采购单生成测试(2个测试)

测试特点:
- 真实GUI交互测试(填写表单、点击按钮、搜索过滤)
- 业务逻辑验证(重复数据拒绝、空值验证、计算正确性)
- 独立测试环境(临时数据库,自动清理)
- 自动化消息框(Mock QMessageBox)

总计22个GUI测试,全部通过 ✓

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-27 16:52:30 +08:00

112 lines
3.5 KiB
Python

"""
采购单生成模块测试 - 测试采购单生成和计算功能
"""
import unittest
import os
import tempfile
import gc
from database import DatabaseManager, get_db_connection
class TestPurchaseOrder(unittest.TestCase):
"""采购单测试类"""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.db_path = os.path.join(self.temp_dir, "test_po.db")
self.db_manager = DatabaseManager(self.db_path)
self.conn = None
self._setup_test_data()
def tearDown(self):
if self.conn:
try:
self.conn.close()
except:
pass
gc.collect()
try:
if os.path.exists(self.db_path):
os.remove(self.db_path)
if os.path.exists(self.temp_dir):
os.rmdir(self.temp_dir)
except:
pass
def get_conn(self):
self.conn = get_db_connection(self.db_path)
return self.conn
def _setup_test_data(self):
"""准备测试数据"""
with get_db_connection(self.db_path) as conn:
conn.execute(
"INSERT INTO garments (style_number) VALUES (?)",
("PO-001",)
)
conn.execute('''
INSERT INTO garment_materials
(style_number, category, model, usage_per_piece, unit)
VALUES (?, ?, ?, ?, ?)
''', ("PO-001", "A料", "M001", 0.5, ""))
conn.execute('''
INSERT INTO garment_materials
(style_number, category, model, usage_per_piece, unit)
VALUES (?, ?, ?, ?, ?)
''', ("PO-001", "B料", "M002", 0.3, ""))
conn.commit()
def test_get_materials_for_style(self):
"""测试获取款式材料"""
with self.get_conn() as conn:
cursor = conn.execute('''
SELECT category, model, usage_per_piece, unit
FROM garment_materials
WHERE style_number = ? AND usage_per_piece > 0
ORDER BY id
''', ("PO-001",))
rows = cursor.fetchall()
self.assertEqual(len(rows), 2)
self.assertEqual(rows[0][0], "A料")
self.assertEqual(rows[1][0], "B料")
def test_calculate_total_usage(self):
"""测试计算总用量"""
usage_per_piece = 0.5
quantity = 100
loss_rate = 0.05
total = usage_per_piece * quantity * (1 + loss_rate)
self.assertEqual(total, 52.5)
def test_calculate_total_usage_no_loss(self):
"""测试无损耗计算"""
usage_per_piece = 0.5
quantity = 100
loss_rate = 0.0
total = usage_per_piece * quantity * (1 + loss_rate)
self.assertEqual(total, 50.0)
def test_calculate_total_usage_high_loss(self):
"""测试高损耗计算"""
usage_per_piece = 0.5
quantity = 100
loss_rate = 0.10
total = usage_per_piece * quantity * (1 + loss_rate)
self.assertAlmostEqual(total, 55.0, places=2)
def test_empty_style_materials(self):
"""测试空款式材料"""
with self.get_conn() as conn:
cursor = conn.execute('''
SELECT category, model, usage_per_piece, unit
FROM garment_materials
WHERE style_number = ? AND usage_per_piece > 0
''', ("NOT-EXIST",))
rows = cursor.fetchall()
self.assertEqual(len(rows), 0)
if __name__ == "__main__":
unittest.main()