新增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>
266 lines
9.7 KiB
Python
266 lines
9.7 KiB
Python
"""
|
||
原料管理功能测试模块
|
||
测试添加、删除、编辑原料功能
|
||
"""
|
||
|
||
import unittest
|
||
import os
|
||
import tempfile
|
||
from database import DatabaseManager, get_db_connection
|
||
|
||
|
||
class TestRawMaterial(unittest.TestCase):
|
||
"""原料管理测试类"""
|
||
|
||
def setUp(self):
|
||
"""测试前准备:创建临时数据库"""
|
||
self.temp_dir = tempfile.mkdtemp()
|
||
self.db_path = os.path.join(self.temp_dir, "test_fabric.db")
|
||
self.db_manager = DatabaseManager(self.db_path)
|
||
self.conn = None
|
||
|
||
def tearDown(self):
|
||
"""测试后清理:删除临时数据库"""
|
||
if self.conn:
|
||
try:
|
||
self.conn.close()
|
||
except:
|
||
pass
|
||
import gc
|
||
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 test_add_raw_material_basic(self):
|
||
"""测试基本添加原料功能"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, fabric_type, supplier, color, width, gsm, unit, retail_price, bulk_price, timestamp)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
|
||
''', ("TEST-001", "布料", "棉布", "供应商A", "白色", 150.0, 200.0, "米", 10.0, 8.0))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT * FROM fabrics WHERE model = ?", ("TEST-001",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertIsNotNone(row)
|
||
self.assertEqual(row[0], "TEST-001")
|
||
|
||
def test_add_raw_material_with_all_fields(self):
|
||
"""测试添加包含所有字段的原料"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, fabric_type, supplier, color, width, gsm, unit, retail_price, bulk_price, timestamp)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, datetime('now'))
|
||
''', ("TEST-002", "辅料", "拉链", "供应商B", "黑色", 0, 0, "条", 5.0, 4.0))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT category, fabric_type, unit FROM fabrics WHERE model = ?", ("TEST-002",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertEqual(row[0], "辅料")
|
||
self.assertEqual(row[1], "拉链")
|
||
self.assertEqual(row[2], "条")
|
||
|
||
def test_add_raw_material_duplicate_model(self):
|
||
"""测试添加重复型号原料(应拒绝,模拟GUI逻辑)"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, supplier, timestamp)
|
||
VALUES (?, ?, ?, datetime('now'))
|
||
''', ("TEST-003", "布料", "供应商A"))
|
||
conn.commit()
|
||
|
||
model = "TEST-003"
|
||
current_edit_model = None
|
||
|
||
should_reject = False
|
||
if not current_edit_model:
|
||
cursor = conn.execute("SELECT 1 FROM fabrics WHERE model = ?", (model,))
|
||
if cursor.fetchone():
|
||
should_reject = True
|
||
|
||
self.assertTrue(should_reject, "应拒绝添加重复型号")
|
||
|
||
cursor = conn.execute("SELECT category, supplier FROM fabrics WHERE model = ?", (model,))
|
||
row = cursor.fetchone()
|
||
self.assertEqual(row[0], "布料", "原数据不应被覆盖")
|
||
self.assertEqual(row[1], "供应商A", "原数据不应被覆盖")
|
||
|
||
def test_add_raw_material_empty_model(self):
|
||
"""测试添加空型号原料(应失败)"""
|
||
with self.get_conn() as conn:
|
||
try:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, timestamp)
|
||
VALUES (?, ?, datetime('now'))
|
||
''', ("", "布料"))
|
||
conn.commit()
|
||
cursor = conn.execute("SELECT COUNT(*) FROM fabrics WHERE model = ''")
|
||
count = cursor.fetchone()[0]
|
||
self.assertEqual(count, 1)
|
||
except Exception:
|
||
pass
|
||
|
||
# ========== 删除原料测试 ==========
|
||
|
||
def test_delete_raw_material_basic(self):
|
||
"""测试基本删除原料功能"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, timestamp)
|
||
VALUES (?, ?, datetime('now'))
|
||
''', ("DEL-001", "布料"))
|
||
conn.commit()
|
||
|
||
conn.execute("DELETE FROM fabrics WHERE model = ?", ("DEL-001",))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT * FROM fabrics WHERE model = ?", ("DEL-001",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertIsNone(row)
|
||
|
||
def test_delete_raw_material_not_exist(self):
|
||
"""测试删除不存在的原料"""
|
||
with self.get_conn() as conn:
|
||
cursor = conn.execute("DELETE FROM fabrics WHERE model = ?", ("NOT-EXIST",))
|
||
conn.commit()
|
||
self.assertEqual(cursor.rowcount, 0)
|
||
|
||
def test_delete_raw_material_with_stock(self):
|
||
"""测试删除有库存记录的原料"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, timestamp)
|
||
VALUES (?, ?, datetime('now'))
|
||
''', ("DEL-002", "布料"))
|
||
conn.execute('''
|
||
INSERT INTO fabric_stock_in (model, quantity, unit, purchase_date)
|
||
VALUES (?, ?, ?, ?)
|
||
''', ("DEL-002", 100.0, "米", "2024-01-01"))
|
||
conn.commit()
|
||
|
||
conn.execute("DELETE FROM fabrics WHERE model = ?", ("DEL-002",))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT * FROM fabrics WHERE model = ?", ("DEL-002",))
|
||
fabric_row = cursor.fetchone()
|
||
|
||
cursor = conn.execute("SELECT * FROM fabric_stock_in WHERE model = ?", ("DEL-002",))
|
||
stock_row = cursor.fetchone()
|
||
|
||
self.assertIsNone(fabric_row)
|
||
self.assertIsNotNone(stock_row)
|
||
|
||
# ========== 编辑原料测试 ==========
|
||
|
||
def test_edit_raw_material_basic(self):
|
||
"""测试基本编辑原料功能"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, supplier, timestamp)
|
||
VALUES (?, ?, ?, datetime('now'))
|
||
''', ("EDIT-001", "布料", "供应商A"))
|
||
conn.commit()
|
||
|
||
conn.execute('''
|
||
UPDATE fabrics SET supplier = ?, updated_at = CURRENT_TIMESTAMP
|
||
WHERE model = ?
|
||
''', ("供应商B", "EDIT-001"))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT supplier FROM fabrics WHERE model = ?", ("EDIT-001",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertEqual(row[0], "供应商B")
|
||
|
||
def test_edit_raw_material_category(self):
|
||
"""测试编辑原料类目"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, category, fabric_type, timestamp)
|
||
VALUES (?, ?, ?, datetime('now'))
|
||
''', ("EDIT-002", "布料", "棉布"))
|
||
conn.commit()
|
||
|
||
conn.execute('''
|
||
UPDATE fabrics SET category = ?, fabric_type = ?
|
||
WHERE model = ?
|
||
''', ("辅料", "纽扣", "EDIT-002"))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT category, fabric_type FROM fabrics WHERE model = ?", ("EDIT-002",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertEqual(row[0], "辅料")
|
||
self.assertEqual(row[1], "纽扣")
|
||
|
||
def test_edit_raw_material_price(self):
|
||
"""测试编辑原料价格"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, retail_price, bulk_price, timestamp)
|
||
VALUES (?, ?, ?, datetime('now'))
|
||
''', ("EDIT-003", 10.0, 8.0))
|
||
conn.commit()
|
||
|
||
conn.execute('''
|
||
UPDATE fabrics SET retail_price = ?, bulk_price = ?
|
||
WHERE model = ?
|
||
''', (15.0, 12.0, "EDIT-003"))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT retail_price, bulk_price FROM fabrics WHERE model = ?", ("EDIT-003",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertEqual(row[0], 15.0)
|
||
self.assertEqual(row[1], 12.0)
|
||
|
||
def test_edit_raw_material_specifications(self):
|
||
"""测试编辑原料规格(幅宽、克重)"""
|
||
with self.get_conn() as conn:
|
||
conn.execute('''
|
||
INSERT INTO fabrics (model, width, gsm, timestamp)
|
||
VALUES (?, ?, ?, datetime('now'))
|
||
''', ("EDIT-004", 150.0, 200.0))
|
||
conn.commit()
|
||
|
||
conn.execute('''
|
||
UPDATE fabrics SET width = ?, gsm = ?
|
||
WHERE model = ?
|
||
''', (160.0, 250.0, "EDIT-004"))
|
||
conn.commit()
|
||
|
||
cursor = conn.execute("SELECT width, gsm FROM fabrics WHERE model = ?", ("EDIT-004",))
|
||
row = cursor.fetchone()
|
||
|
||
self.assertEqual(row[0], 160.0)
|
||
self.assertEqual(row[1], 250.0)
|
||
|
||
def test_edit_raw_material_not_exist(self):
|
||
"""测试编辑不存在的原料"""
|
||
with self.get_conn() as conn:
|
||
cursor = conn.execute('''
|
||
UPDATE fabrics SET supplier = ?
|
||
WHERE model = ?
|
||
''', ("供应商X", "NOT-EXIST"))
|
||
conn.commit()
|
||
self.assertEqual(cursor.rowcount, 0)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|