添加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>
This commit is contained in:
2025-12-27 16:52:30 +08:00
parent c52d360cbb
commit 8aa1a5ac91
14 changed files with 2599 additions and 0 deletions

216
test/test_garment.py Normal file
View File

@@ -0,0 +1,216 @@
"""
服装管理模块测试 - 测试服装款式和材料用量管理
"""
import unittest
import os
import tempfile
import gc
from database import DatabaseManager, get_db_connection
class TestGarment(unittest.TestCase):
"""服装管理测试类"""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.db_path = os.path.join(self.temp_dir, "test_garment.db")
self.db_manager = DatabaseManager(self.db_path)
self.conn = None
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 test_add_garment_basic(self):
"""测试添加服装款式"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number, image_path) VALUES (?, ?)",
("G001", None)
)
conn.commit()
cursor = conn.execute(
"SELECT style_number FROM garments WHERE style_number = ?",
("G001",)
)
row = cursor.fetchone()
self.assertEqual(row[0], "G001")
def test_add_garment_with_image(self):
"""测试添加带图片的服装款式"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number, image_path) VALUES (?, ?)",
("G002", "images/g002.jpg")
)
conn.commit()
cursor = conn.execute(
"SELECT image_path FROM garments WHERE style_number = ?",
("G002",)
)
row = cursor.fetchone()
self.assertEqual(row[0], "images/g002.jpg")
def test_update_garment(self):
"""测试更新服装款式"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number, image_path) VALUES (?, ?)",
("G003", None)
)
conn.commit()
conn.execute(
"INSERT OR REPLACE INTO garments (style_number, image_path) VALUES (?, ?)",
("G003", "images/g003_new.jpg")
)
conn.commit()
cursor = conn.execute(
"SELECT image_path FROM garments WHERE style_number = ?",
("G003",)
)
row = cursor.fetchone()
self.assertEqual(row[0], "images/g003_new.jpg")
def test_delete_garment(self):
"""测试删除服装款式"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number) VALUES (?)",
("G004",)
)
conn.commit()
conn.execute(
"DELETE FROM garments WHERE style_number = ?",
("G004",)
)
conn.commit()
cursor = conn.execute(
"SELECT * FROM garments WHERE style_number = ?",
("G004",)
)
row = cursor.fetchone()
self.assertIsNone(row)
# ========== 材料用量测试 ==========
def test_add_material_basic(self):
"""测试添加材料用量"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number) VALUES (?)",
("G005",)
)
conn.execute('''
INSERT INTO garment_materials
(style_number, category, fabric_type, model, usage_per_piece, unit)
VALUES (?, ?, ?, ?, ?, ?)
''', ("G005", "布料", "棉布", "M001", 0.5, ""))
conn.commit()
cursor = conn.execute(
"SELECT usage_per_piece, unit FROM garment_materials WHERE style_number = ?",
("G005",)
)
row = cursor.fetchone()
self.assertEqual(row[0], 0.5)
self.assertEqual(row[1], "")
def test_add_multiple_materials(self):
"""测试添加多个材料"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number) VALUES (?)",
("G006",)
)
conn.execute('''
INSERT INTO garment_materials
(style_number, category, usage_per_piece, unit)
VALUES (?, ?, ?, ?)
''', ("G006", "A料", 0.3, ""))
conn.execute('''
INSERT INTO garment_materials
(style_number, category, usage_per_piece, unit)
VALUES (?, ?, ?, ?)
''', ("G006", "B料", 0.2, ""))
conn.commit()
cursor = conn.execute(
"SELECT COUNT(*) FROM garment_materials WHERE style_number = ?",
("G006",)
)
count = cursor.fetchone()[0]
self.assertEqual(count, 2)
def test_update_material_usage(self):
"""测试更新材料用量"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number) VALUES (?)",
("G007",)
)
conn.execute('''
INSERT INTO garment_materials
(style_number, category, usage_per_piece, unit)
VALUES (?, ?, ?, ?)
''', ("G007", "A料", 0.5, ""))
conn.commit()
conn.execute('''
UPDATE garment_materials SET usage_per_piece = ?
WHERE style_number = ? AND category = ?
''', (0.8, "G007", "A料"))
conn.commit()
cursor = conn.execute(
"SELECT usage_per_piece FROM garment_materials WHERE style_number = ? AND category = ?",
("G007", "A料")
)
row = cursor.fetchone()
self.assertEqual(row[0], 0.8)
def test_delete_garment_cascade_materials(self):
"""测试删除款式时材料记录处理"""
with self.get_conn() as conn:
conn.execute(
"INSERT INTO garments (style_number) VALUES (?)",
("G008",)
)
conn.execute('''
INSERT INTO garment_materials
(style_number, category, usage_per_piece, unit)
VALUES (?, ?, ?, ?)
''', ("G008", "A料", 0.5, ""))
conn.commit()
conn.execute(
"DELETE FROM garment_materials WHERE style_number = ?",
("G008",)
)
conn.execute(
"DELETE FROM garments WHERE style_number = ?",
("G008",)
)
conn.commit()
cursor = conn.execute(
"SELECT COUNT(*) FROM garment_materials WHERE style_number = ?",
("G008",)
)
count = cursor.fetchone()[0]
self.assertEqual(count, 0)
if __name__ == "__main__":
unittest.main()