Add BOM repository module and enhance code formatting

- Add new mBOMRepository.bas module for data access layer with LoadAll/SaveAll/InsertNewBOMRow functions
- Refactor cBOMRow.cls property methods for better readability and formatting
- Add comprehensive unit tests for BOM repository functionality in mTestEngine

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka_Company
2026-03-19 12:45:04 +08:00
parent f344cbe206
commit 76a75c9a05
3 changed files with 249 additions and 13 deletions

View File

@@ -17,9 +17,12 @@ Public Sub RunBOMForgeTests()
' 执行条件引擎测试
Call Test_ConditionEngine
' 执行数据模型测试 (新增)
' 执行数据模型测试
Call Test_cBOMRow
' 执行存储库数据层测试 (新增)
Call Test_BOMRepository
' ---- 输出测试汇总 ----
Debug.Print "-----------------------------------------------"
Debug.Print "测试汇总: 共 " & (passCount + failCount) & " 个用例"
@@ -113,6 +116,70 @@ Private Sub Test_cBOMRow()
End Sub
' ==============================================================================
' 新增:针对 BOMRepository 存取机制的单元测试 (沙盒模式)
' ==============================================================================
Private Sub Test_BOMRepository()
Debug.Print ""
Debug.Print "========== 3. 开始测试: mBOMRepository 数据访问层 =========="
Dim wb As Workbook
Dim wsTemp As Worksheet
Set wb = ThisWorkbook
' 1. 创建沙盒工作表
Application.DisplayAlerts = False
On Error Resume Next
wb.Worksheets("BOMForge_Test_Sandbox").Delete
On Error GoTo 0
Set wsTemp = wb.Worksheets.Add
wsTemp.Name = "BOMForge_Test_Sandbox"
' 2. 模拟初始化表头(第3行)和原始数据(第4,5行)
wsTemp.Cells(3, 1).value = "行号"
wsTemp.Cells(4, 1).value = 10
wsTemp.Cells(4, 2).value = "接头"
wsTemp.Cells(4, 6).value = "lcfw=M01" ' 选择条件
wsTemp.Cells(5, 1).value = 20
wsTemp.Cells(5, 2).value = "盘止钉"
wsTemp.Cells(5, 6).value = "lcfw=M02"
' 3. 测试加载功能
Dim bomColl As Collection
Set bomColl = LoadAllBOMs(wsTemp)
AssertEqual "加载集合数量", "2", CStr(bomColl.count)
AssertEqual "验证读取第一行", "接头", bomColl(1).ModuleType
AssertEqual "验证加载后脏标记为空", "False", CStr(bomColl(1).IsDirty)
' 4. 测试修改和保存功能 (仅修改第二行)
bomColl(2).Condition = "lcfw=M02 OR lcfw=M19"
SaveAll wsTemp, bomColl
' 验证 Excel 表格内容是否被正确写回
AssertEqual "验证Excel未被错误修改", "lcfw=M01", wsTemp.Cells(4, 6).value
AssertEqual "验证Excel正确更新", "lcfw=M02 OR lcfw=M19", wsTemp.Cells(5, 6).value
AssertEqual "验证保存后脏标记重置", "False", CStr(bomColl(2).IsDirty)
' 5. 测试新增行功能 (基于第一行克隆)
Dim newRow As cBOMRow
Set newRow = InsertNewBOMRow(bomColl, bomColl(1))
newRow.Condition = "lcfw=M19"
SaveAll wsTemp, bomColl
' 验证新增行是否保存到了第6行
AssertEqual "验证新增行集合追加", "3", CStr(bomColl.count)
AssertEqual "验证新增行Excel保存", "lcfw=M19", wsTemp.Cells(6, 6).value
AssertEqual "验证新增行Excel映射行号", "6", CStr(newRow.ExcelRowIndex)
' 6. 清理沙盒工作表
wsTemp.Delete
Application.DisplayAlerts = True
End Sub
' ==============================================================================
' 内部辅助方法:断言测试结果
' 将期望值与实际值进行比对,并输出标准化的日志信息