Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions Test/public/databasev2.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,46 @@ function Test_Database_JSON{
Assert-IsFalse -Condition $result
}

function Test_Database_JSON_ComplexObject{

Reset-InvokeCommandMock
Mock_Database -ResetDatabase

# Load include files needed to test database
. $(Get-Ps1FullPath -Name "databaseV2.ps1" -FolderName "Include" -ModuleRootPath $MODULE_ROOT_PATH)

$complexObject = @{
Name = "Test Object"
Value = 123
Nested = @{
Property1 = "Value1"
Property2 = "Value2"
}
List = @(1, 2, 3, 4, 5)
}

# Save complex object to database
Save-DatabaseKey -Key "testComplex" -Value $complexObject -DBFormat "JSON"

# Get complex object from database
$result = Get-DatabaseKey -Key "testComplex" -DBFormat "JSON"
Assert-AreEqual -Expected "PSCustomObject" -Presented $result.GetType().Name
Assert-AreEqual -Expected $complexObject.Name -Presented $result.Name
Assert-AreEqual -Expected $complexObject.Value -Presented $result.Value
Assert-AreEqual -Expected $complexObject.Nested.Property1 -Presented $result.Nested.Property1
Assert-AreEqual -Expected $complexObject.Nested.Property2 -Presented $result.Nested.Property2
Assert-AreEqual -Expected $complexObject.List.Count -Presented $result.List.Count

# get complex object as hashtable AsHashtable
$resultHashtable = Get-DatabaseKey -Key "testComplex" -DBFormat "JSON" -AsHashtable
Assert-AreEqual -Expected "OrderedHashtable" -Presented $resultHashtable.GetType().Name
Assert-AreEqual -Expected $complexObject.Name -Presented $resultHashtable["Name"]
Assert-AreEqual -Expected $complexObject.Value -Presented $resultHashtable["Value"]
Assert-AreEqual -Expected $complexObject.Nested.Property1 -Presented $resultHashtable["Nested"]["Property1"]
Assert-AreEqual -Expected $complexObject.Nested.Property2 -Presented $resultHashtable["Nested"]["Property2"]
Assert-AreEqual -Expected $complexObject.List.Count -Presented $resultHashtable["List"].Count
}

function Test_Database_XML{

Reset-InvokeCommandMock
Expand Down
5 changes: 3 additions & 2 deletions include/databaseV2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ function Get-DatabaseKey{
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)][string]$Key,
[Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON"
[Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON",
[Parameter()][switch]$AsHashtable
)

if(-Not (Test-DatabaseKey $Key -DBFormat $DBFormat)){
Expand All @@ -112,7 +113,7 @@ function Get-DatabaseKey{
$path = GetDatabaseFile $Key -DBFormat $DBFormat

switch ($DBFormat) {
"JSON" { $ret = Get-Content $path | ConvertFrom-Json ; Break }
"JSON" { $ret = Get-Content $path | ConvertFrom-Json -AsHashtable:$AsHashtable ; Break }
"XML" { $ret = Import-Clixml -Path $path ; Break }
"TXT" { $ret = Get-Content $path ; Break }
default { throw "Unsupported database format $DbFormat" }
Expand Down
Loading