-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest-ZP12Validation.ps1
More file actions
302 lines (249 loc) · 10.4 KB
/
Test-ZP12Validation.ps1
File metadata and controls
302 lines (249 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
[CmdletBinding()]
param(
[string]$WorkbookPath = ".\CreateOrder.xlsm",
[string]$ModulePath = ".\CreateOrder.xlsm.modules\mdlZP12Validation.bas",
[string]$RibbonHandlersPath = ".\CreateOrder.xlsm.modules\mdlRibbonHandlers.bas",
[string]$SourceTemplatePath = "",
[string]$RunTemplatePath = ".\ZP12_TEST_RUN.xlsx",
[string]$Pass1SnapshotPath = ".\ZP12_TEST_RUN_PASS1.xlsx"
)
$ErrorActionPreference = "Stop"
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
function Resolve-ProjectPath {
param([string]$PathValue)
if ([string]::IsNullOrWhiteSpace($PathValue)) {
return $null
}
if ([System.IO.Path]::IsPathRooted($PathValue)) {
return [System.IO.Path]::GetFullPath($PathValue)
}
return [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $PathValue))
}
function Invoke-Python {
param([string]$ScriptText)
$tempFile = Join-Path $env:TEMP ("zpt12_" + [System.Guid]::NewGuid().ToString("N") + ".py")
try {
Set-Content -Path $tempFile -Value $ScriptText -Encoding UTF8
$output = & python $tempFile 2>&1
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
throw ($output -join [Environment]::NewLine)
}
return ($output -join [Environment]::NewLine)
}
finally {
Remove-Item -Path $tempFile -Force -ErrorAction SilentlyContinue
}
}
function Get-DefaultSourceTemplate {
$candidate = Get-ChildItem -Path $PSScriptRoot -File | Where-Object {
$_.Name -like "*_TEST.xlsx" -and
$_.Name -notlike "*RUN*" -and
$_.Name -notlike "*PASS1*"
} | Sort-Object Name | Select-Object -First 1
if ($null -eq $candidate) {
throw "Не найден исходный тестовый шаблон (*_TEST.xlsx)."
}
return $candidate.FullName
}
function Invoke-ZP12MacroRun {
param(
[string]$WorkbookFullPath,
[string]$ModuleFullPath,
[string]$RibbonHandlersFullPath,
[string]$TemplateFullPath
)
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$excel.DisplayAlerts = $false
$excel.ScreenUpdating = $false
try {
$macroWb = $excel.Workbooks.Open($WorkbookFullPath)
$vbProject = $macroWb.VBProject
try {
$component = $vbProject.VBComponents.Item("mdlZP12Validation")
$vbProject.VBComponents.Remove($component)
}
catch {
}
try {
$component = $vbProject.VBComponents.Item("mdlRibbonHandlers")
$vbProject.VBComponents.Remove($component)
}
catch {
}
$null = $vbProject.VBComponents.Import($ModuleFullPath)
$null = $vbProject.VBComponents.Import($RibbonHandlersFullPath)
$macroWb.Save()
$excel.Run("CreateOrder.xlsm!mdlZP12Validation.ValidateZP12Template", $TemplateFullPath, $true)
$templateWb = $null
foreach ($wb in $excel.Workbooks) {
if ($wb.FullName -eq $TemplateFullPath) {
$templateWb = $wb
break
}
}
if ($null -ne $templateWb) {
$templateWb.Save()
$templateWb.Close($false)
}
$macroWb.Close($true)
}
finally {
$excel.Quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($excel) | Out-Null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
}
function Get-ZP12State {
param([string]$WorkbookFullPath)
$python = @"
import json
from pathlib import Path
from openpyxl import load_workbook
path = Path(r'''$WorkbookFullPath''')
wb = load_workbook(path)
history_name = 'История_проверок_Д89'
if history_name not in wb.sheetnames:
raise SystemExit('History sheet not found')
main_ws = wb[wb.sheetnames[0]]
history_ws = wb[history_name]
highlights = []
for row in range(4, 40):
for col in range(2, 12):
cell = main_ws.cell(row, col)
fill = cell.fill
color = None
if fill and fill.fill_type == 'solid':
color = fill.fgColor.rgb or fill.start_color.rgb
if color or cell.comment:
highlights.append({
'cell': cell.coordinate,
'color': color,
'comment': cell.comment.text.strip() if cell.comment else ''
})
headers = [history_ws.cell(1, c).value for c in range(1, history_ws.max_column + 1)]
rows = []
status_by_run = {}
for r in range(2, history_ws.max_row + 1):
values = [history_ws.cell(r, c).value for c in range(1, history_ws.max_column + 1)]
if any(v is not None and v != '' for v in values):
item = dict(zip(headers, values))
rows.append(item)
run_id = str(item['RunID'])
status_by_run.setdefault(run_id, {})
status = item['Статус']
status_by_run[run_id][status] = status_by_run[run_id].get(status, 0) + 1
print(json.dumps({
'highlightedCells': [item['cell'] for item in highlights],
'highlights': highlights,
'historyRowCount': len(rows),
'statusByRun': status_by_run
}, ensure_ascii=False))
"@
return (Invoke-Python -ScriptText $python | ConvertFrom-Json)
}
function Apply-RegressionFixes {
param([string]$WorkbookFullPath)
$python = @"
from datetime import datetime
from pathlib import Path
from openpyxl import load_workbook
path = Path(r'''$WorkbookFullPath''')
wb = load_workbook(path)
ws = wb[wb.sheetnames[0]]
ws['B5'] = 21463
ws['G7'] = 'Владимирович'
ws['I11'] = datetime(2023, 10, 21)
ws['J11'] = datetime(2023, 10, 25)
ws['I11'].number_format = 'dd.mm.yyyy'
ws['J11'].number_format = 'dd.mm.yyyy'
wb.save(path)
print('ok')
"@
Invoke-Python -ScriptText $python | Out-Null
}
function Get-StatusCount {
param(
[object]$State,
[string]$RunId,
[string]$StatusName
)
$run = $State.statusByRun.PSObject.Properties[$RunId]
if ($null -eq $run) {
return 0
}
$status = $run.Value.PSObject.Properties[$StatusName]
if ($null -eq $status) {
return 0
}
return [int]$status.Value
}
function Assert-Equal {
param(
[string]$Label,
[object]$Expected,
[object]$Actual
)
if ($Expected -ne $Actual) {
throw "${Label}: ожидалось '$Expected', получено '$Actual'."
}
}
function Assert-SetEqual {
param(
[string]$Label,
[string[]]$Expected,
[object[]]$Actual
)
$expectedSet = [System.Collections.Generic.HashSet[string]]::new([string[]]$Expected)
$actualSet = [System.Collections.Generic.HashSet[string]]::new([string[]]$Actual)
if (-not $expectedSet.SetEquals($actualSet)) {
$expectedText = ($Expected | Sort-Object) -join ", "
$actualText = ([string[]]$Actual | Sort-Object) -join ", "
throw "${Label}: ожидалось [$expectedText], получено [$actualText]."
}
}
$workbookFullPath = Resolve-ProjectPath $WorkbookPath
$moduleFullPath = Resolve-ProjectPath $ModulePath
$ribbonHandlersFullPath = Resolve-ProjectPath $RibbonHandlersPath
$sourceTemplateFullPath = if ([string]::IsNullOrWhiteSpace($SourceTemplatePath)) { Get-DefaultSourceTemplate } else { Resolve-ProjectPath $SourceTemplatePath }
$runTemplateFullPath = Resolve-ProjectPath $RunTemplatePath
$pass1SnapshotFullPath = Resolve-ProjectPath $Pass1SnapshotPath
if (-not (Test-Path $workbookFullPath)) { throw "Не найден workbook: $workbookFullPath" }
if (-not (Test-Path $moduleFullPath)) { throw "Не найден модуль: $moduleFullPath" }
if (-not (Test-Path $ribbonHandlersFullPath)) { throw "Не найден ribbon-модуль: $ribbonHandlersFullPath" }
if (-not (Test-Path $sourceTemplateFullPath)) { throw "Не найден шаблон: $sourceTemplateFullPath" }
Write-Host "=== D89 regression ===" -ForegroundColor Cyan
Write-Host "Workbook: $workbookFullPath"
Write-Host "Module: $moduleFullPath"
Write-Host "Ribbon: $ribbonHandlersFullPath"
Write-Host "Fixture: $sourceTemplateFullPath"
Copy-Item -Path $sourceTemplateFullPath -Destination $runTemplateFullPath -Force
if (Test-Path $pass1SnapshotFullPath) {
Remove-Item -Path $pass1SnapshotFullPath -Force
}
Write-Host "[1/5] First validation run..." -ForegroundColor Yellow
Invoke-ZP12MacroRun -WorkbookFullPath $workbookFullPath -ModuleFullPath $moduleFullPath -RibbonHandlersFullPath $ribbonHandlersFullPath -TemplateFullPath $runTemplateFullPath
$pass1State = Get-ZP12State -WorkbookFullPath $runTemplateFullPath
Copy-Item -Path $runTemplateFullPath -Destination $pass1SnapshotFullPath -Force
Write-Host "[2/5] Apply controlled fixes..." -ForegroundColor Yellow
Apply-RegressionFixes -WorkbookFullPath $runTemplateFullPath
Write-Host "[3/5] Second validation run..." -ForegroundColor Yellow
Invoke-ZP12MacroRun -WorkbookFullPath $workbookFullPath -ModuleFullPath $moduleFullPath -RibbonHandlersFullPath $ribbonHandlersFullPath -TemplateFullPath $runTemplateFullPath
$pass2State = Get-ZP12State -WorkbookFullPath $runTemplateFullPath
Write-Host "[4/5] Assert expected status transitions..." -ForegroundColor Yellow
Assert-Equal -Label "Pass1 NEW" -Expected 7 -Actual (Get-StatusCount -State $pass1State -RunId "1" -StatusName "NEW")
Assert-Equal -Label "Pass1 history rows" -Expected 7 -Actual $pass1State.historyRowCount
Assert-Equal -Label "Pass2 OPEN" -Expected 4 -Actual (Get-StatusCount -State $pass2State -RunId "2" -StatusName "OPEN")
Assert-Equal -Label "Pass2 RESOLVED" -Expected 3 -Actual (Get-StatusCount -State $pass2State -RunId "2" -StatusName "RESOLVED")
Assert-Equal -Label "Pass2 history rows" -Expected 14 -Actual $pass2State.historyRowCount
$expectedPass2Highlights = @("C6", "I8", "J8", "I9", "J9", "B10", "C10")
Assert-SetEqual -Label "Pass2 highlighted cells" -Expected $expectedPass2Highlights -Actual $pass2State.highlightedCells
Write-Host "[5/5] Summary" -ForegroundColor Yellow
Write-Host "Pass1: NEW=$(Get-StatusCount -State $pass1State -RunId '1' -StatusName 'NEW'), rows=$($pass1State.historyRowCount)" -ForegroundColor Green
Write-Host "Pass2: OPEN=$(Get-StatusCount -State $pass2State -RunId '2' -StatusName 'OPEN'), RESOLVED=$(Get-StatusCount -State $pass2State -RunId '2' -StatusName 'RESOLVED'), rows=$($pass2State.historyRowCount)" -ForegroundColor Green
Write-Host "Remaining highlighted cells: $(([string[]]$pass2State.highlightedCells | Sort-Object) -join ', ')" -ForegroundColor Green
Write-Host "Run file: $runTemplateFullPath" -ForegroundColor Green
Write-Host "Pass1 snapshot: $pass1SnapshotFullPath" -ForegroundColor Green
Write-Host "RESULT: PASS" -ForegroundColor Cyan