""" 服装管理模块测试 - 测试服装款式和材料用量管理 """ 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()