Tests: Pester-Tests für PolicyIO-Payload-Transformationen (#2)
Deckt die Array-Normalisierung (Repair-SettingsCatalogArrays) inkl. choiceSettingCollectionValue sowie Remove-PolicyNullProps ab — genau die Bug-Klasse (PS-5.1-JSON-Roundtrip skalarisiert Ein-Element-Arrays), die uns mehrfach getroffen hat. 9 Tests, klassische Pester-3/4-Syntax. Ausführen: Invoke-Pester .\tests Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
|||||||
|
# Pester-Tests fuer die Payload-Transformationen in PolicyIO.ps1.
|
||||||
|
# Diese reinen Funktionen sind genau die Bug-Klasse, die uns mehrfach getroffen
|
||||||
|
# hat: der JSON-Roundtrip unter Windows PowerShell 5.1 entpackt Ein-Element-Arrays
|
||||||
|
# zu Einzelobjekten -> Graph antwortet mit 400 "... does not match schema".
|
||||||
|
#
|
||||||
|
# Ausfuehren: Invoke-Pester .\tests
|
||||||
|
# (klassische Pester-3/4-Syntax, passend zur mit Windows gelieferten Version.)
|
||||||
|
|
||||||
|
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
|
. (Join-Path $here '..\src\PolicyIO.ps1')
|
||||||
|
|
||||||
|
Describe 'Repair-SettingsCatalogArrays' {
|
||||||
|
|
||||||
|
It 'macht aus einem skalaren choiceSettingCollectionValue ein Array' {
|
||||||
|
# Genau der Fall aus dem gefixten Defender-Antivirus-Import.
|
||||||
|
$node = @{
|
||||||
|
settingInstance = @{
|
||||||
|
'@odata.type' = '#microsoft.graph.deviceManagementConfigurationChoiceSettingCollectionInstance'
|
||||||
|
choiceSettingCollectionValue = @{ value = 'x'; children = @() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
$ccv = $fixed.settingInstance.choiceSettingCollectionValue
|
||||||
|
($ccv -is [System.Collections.IList]) | Should Be $true
|
||||||
|
@($ccv).Count | Should Be 1
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'erzwingt auch group- und simpleSettingCollectionValue als Array' {
|
||||||
|
$node = @{
|
||||||
|
settingInstance = @{
|
||||||
|
groupSettingCollectionValue = @{ children = @() }
|
||||||
|
simpleSettingCollectionValue = @{ value = 1 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
($fixed.settingInstance.groupSettingCollectionValue -is [System.Collections.IList]) | Should Be $true
|
||||||
|
($fixed.settingInstance.simpleSettingCollectionValue -is [System.Collections.IList]) | Should Be $true
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'macht aus einem einzelnen settings-Objekt ein Array' {
|
||||||
|
$node = @{ settings = @{ id = '0'; settingInstance = @{} } }
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
($fixed.settings -is [System.Collections.IList]) | Should Be $true
|
||||||
|
@($fixed.settings).Count | Should Be 1
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'ersetzt null-Collections durch ein leeres Array (nicht {})' {
|
||||||
|
$node = @{ settingInstance = @{ children = $null } }
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
($fixed.settingInstance.children -is [System.Collections.IList]) | Should Be $true
|
||||||
|
@($fixed.settingInstance.children).Count | Should Be 0
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'ist idempotent: ein bereits korrektes Array bleibt ein Array gleicher Laenge' {
|
||||||
|
$node = @{ choiceSettingCollectionValue = @( @{ value = 'a' }, @{ value = 'b' } ) }
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
($fixed.choiceSettingCollectionValue -is [System.Collections.IList]) | Should Be $true
|
||||||
|
@($fixed.choiceSettingCollectionValue).Count | Should Be 2
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'filtert leere String-Elemente aus einer Collection heraus' {
|
||||||
|
$node = @{ children = @('', @{ value = 'x' }, ' ') }
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
@($fixed.children).Count | Should Be 1
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'setzt einen zu String stringifizierten *TemplateReference auf null' {
|
||||||
|
$node = @{ settingInstance = @{ settingInstanceTemplateReference = 'System.Collections.Hashtable' } }
|
||||||
|
$fixed = Repair-SettingsCatalogArrays $node
|
||||||
|
$fixed.settingInstance.settingInstanceTemplateReference | Should Be $null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Describe 'Remove-PolicyNullProps' {
|
||||||
|
|
||||||
|
It 'entfernt Properties mit null-Wert' {
|
||||||
|
$node = @{ keep = 'a'; drop = $null }
|
||||||
|
$fixed = Remove-PolicyNullProps $node
|
||||||
|
$fixed.ContainsKey('keep') | Should Be $true
|
||||||
|
$fixed.ContainsKey('drop') | Should Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'behaelt Array-Properties als Array' {
|
||||||
|
$node = @{ values = @( @{ a = 1 } ) }
|
||||||
|
$fixed = Remove-PolicyNullProps $node
|
||||||
|
($fixed.values -is [System.Collections.IList]) | Should Be $true
|
||||||
|
@($fixed.values).Count | Should Be 1
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user