diff --git a/Test/include/database.mock.ps1 b/Test/include/database.mock.ps1 index 0f5e8be..3932a9f 100644 --- a/Test/include/database.mock.ps1 +++ b/Test/include/database.mock.ps1 @@ -32,10 +32,9 @@ function Reset-DatabaseStore{ [CmdletBinding()] param() + # Get actual store path $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD - - Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue - - New-Item -Path $databaseRoot -ItemType Directory + # Remove the database root directory + Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue } \ No newline at end of file diff --git a/Test/public/databasev2.test.ps1 b/Test/public/databasev2.test.ps1 index 79e7f87..66e2a76 100644 --- a/Test/public/databasev2.test.ps1 +++ b/Test/public/databasev2.test.ps1 @@ -15,8 +15,7 @@ function Test_Database{ # Get actual store path $StorePath = Invoke-MyCommand -Command "Invoke-IncludeHelperGetDbRootPath" Assert-AreEqual -Expected "test_database_path" -Presented $StorePath - $items = Get-ChildItem -Path $StorePath - Assert-Count -Expected 0 -Presented $items + Assert-IsFalse -Condition (Test-Path -Path $StorePath) # GetDatabaseFile $result = GetDatabaseFile -Key "test" @@ -46,6 +45,144 @@ function Test_Database{ Assert-IsFalse -Condition $result } +function Test_Database_JSON{ + + Reset-InvokeCommandMock + Mock_Database -ResetDatabase + + # Load include files needed to test database + . $(Get-Ps1FullPath -Name "databaseV2.ps1" -FolderName "Include" -ModuleRootPath $MODULE_ROOT_PATH) + + # Get Default Database Root Path + $result = GetDatabaseRootPath + $expected = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $MODULE_NAME, "databaseCache" + Assert-AreEqual -Expected $expected -Presented $result + + # Get actual store path + $StorePath = Invoke-MyCommand -Command "Invoke-IncludeHelperGetDbRootPath" + Assert-AreEqual -Expected "test_database_path" -Presented $StorePath + Assert-IsFalse -Condition (Test-Path -Path $StorePath) + + # GetDatabaseFile + $result = GetDatabaseFile -Key "test" -DBFormat "JSON" + Assert-AreEqual -Expected "test_database_path/test.json" -Presented $result + + # Test if database is empty + $result = Test-DatabaseKey -Key "test" -DBFormat "JSON" + Assert-IsFalse -Condition $result + $result = Get-DatabaseKey -Key "test" -DBFormat "JSON" + Assert-IsNull -Object $result + + # Save content to database + Save-DatabaseKey -Key "test" -Value "dummy content" -DBFormat "JSON" + $result = Test-DatabaseKey -Key "test" -DBFormat "JSON" + Assert-IsTrue -Condition $result + + $result = Get-DatabaseKey -Key "test" -DBFormat "JSON" + Assert-AreEqual -Expected "dummy content" -Presented $result + + # Check the number of files in store + $items = Get-ChildItem -Path $StorePath + Assert-Count -Expected 1 -Presented $items + + # Reset Database + Reset-DatabaseKey -Key "test" -DBFormat "JSON" + $result = Test-DatabaseKey -Key "test" -DBFormat "JSON" + Assert-IsFalse -Condition $result +} + +function Test_Database_XML{ + + Reset-InvokeCommandMock + Mock_Database -ResetDatabase + + # Load include files needed to test database + . $(Get-Ps1FullPath -Name "databaseV2.ps1" -FolderName "Include" -ModuleRootPath $MODULE_ROOT_PATH) + + # Get Default Database Root Path + $result = GetDatabaseRootPath + $expected = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $MODULE_NAME, "databaseCache" + Assert-AreEqual -Expected $expected -Presented $result + + # Get actual store path + $StorePath = Invoke-MyCommand -Command "Invoke-IncludeHelperGetDbRootPath" + Assert-AreEqual -Expected "test_database_path" -Presented $StorePath + Assert-IsFalse -Condition (Test-Path -Path $StorePath) + + # GetDatabaseFile + $result = GetDatabaseFile -Key "test" -DBFormat "XML" + Assert-AreEqual -Expected "test_database_path/test.xml" -Presented $result + + # Test if database is empty + $result = Test-DatabaseKey -Key "test" -DBFormat "XML" + Assert-IsFalse -Condition $result + $result = Get-DatabaseKey -Key "test" -DBFormat "XML" + Assert-IsNull -Object $result + + # Save content to database + Save-DatabaseKey -Key "test" -Value "dummy content" -DBFormat "XML" + $result = Test-DatabaseKey -Key "test" -DBFormat "XML" + Assert-IsTrue -Condition $result + + $result = Get-DatabaseKey -Key "test" -DBFormat "XML" + Assert-AreEqual -Expected "dummy content" -Presented $result + + # Check the number of files in store + $items = Get-ChildItem -Path $StorePath + Assert-Count -Expected 1 -Presented $items + + # Reset Database + Reset-DatabaseKey -Key "test" -DBFormat "XML" + $result = Test-DatabaseKey -Key "test" -DBFormat "XML" + Assert-IsFalse -Condition $result +} + +function Test_Database_TXT{ + + Reset-InvokeCommandMock + Mock_Database -ResetDatabase + + # Load include files needed to test database + . $(Get-Ps1FullPath -Name "databaseV2.ps1" -FolderName "Include" -ModuleRootPath $MODULE_ROOT_PATH) + + # Get Default Database Root Path + $result = GetDatabaseRootPath + $expected = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $MODULE_NAME, "databaseCache" + Assert-AreEqual -Expected $expected -Presented $result + + # Get actual store path + $StorePath = Invoke-MyCommand -Command "Invoke-IncludeHelperGetDbRootPath" + Assert-AreEqual -Expected "test_database_path" -Presented $StorePath + Assert-IsFalse -Condition (Test-Path -Path $StorePath) + + # GetDatabaseFile + $result = GetDatabaseFile -Key "test" -DBFormat "TXT" + Assert-AreEqual -Expected "test_database_path/test.txt" -Presented $result + + # Test if database is empty + $result = Test-DatabaseKey -Key "test" -DBFormat "TXT" + Assert-IsFalse -Condition $result + $result = Get-DatabaseKey -Key "test" -DBFormat "TXT" + Assert-IsNull -Object $result + + # Save content to database + Save-DatabaseKey -Key "test" -Value "dummy content" -DBFormat "TXT" + $result = Test-DatabaseKey -Key "test" -DBFormat "TXT" + Assert-IsTrue -Condition $result + + $result = Get-DatabaseKey -Key "test" -DBFormat "TXT" + Assert-AreEqual -Expected "dummy content" -Presented $result + + # Check the number of files in store + $items = Get-ChildItem -Path $StorePath + Assert-Count -Expected 1 -Presented $items + + # Reset Database + Reset-DatabaseKey -Key "test" -DBFormat "TXT" + $result = Test-DatabaseKey -Key "test" -DBFormat "TXT" + Assert-IsFalse -Condition $result +} + function Test_Database_MultyKey{ Reset-InvokeCommandMock diff --git a/include/databaseV2.ps1 b/include/databaseV2.ps1 index 2622f99..3fb68f2 100644 --- a/include/databaseV2.ps1 +++ b/include/databaseV2.ps1 @@ -66,29 +66,57 @@ function GetDatabaseRootPath { function GetDatabaseFile{ [CmdletBinding()] param( - [Parameter(Position = 0)][string]$Key + [Parameter(Mandatory, Position = 0)][string]$Key, + [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" ) $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS - $path = $databaseRoot | Join-Path -ChildPath "$Key.json" + if(-not (Test-Path -Path $databaseRoot)){ + New-Item -Path $databaseRoot -ItemType Directory -Force | Out-Null + } + + $ext = GetFileExtension -DbFormat $DBFormat + + $path = $databaseRoot | Join-Path -ChildPath "$Key$ext" return $path } +function GetFileExtension{ + [CmdletBinding()] + param( + [Parameter(Mandatory)][string]$DbFormat + ) + + switch ($DbFormat.ToUpper()){ + "JSON" { $ret = ".json" ; Break } + "XML" { $ret = ".xml" ; Break } + "TXT" { $ret = ".txt" ; Break } + default { throw "Unsupported database format $DbFormat" } + } + return $ret +} + function Get-DatabaseKey{ [CmdletBinding()] param( - [Parameter(Position = 0)][string]$Key + [Parameter(Mandatory, Position = 0)][string]$Key, + [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" ) - if(-Not (Test-DatabaseKey $Key)){ + if(-Not (Test-DatabaseKey $Key -DBFormat $DBFormat)){ return $null } - $path = GetDatabaseFile $Key + $path = GetDatabaseFile $Key -DBFormat $DBFormat - $ret = Get-Content $path | ConvertFrom-Json -Depth 10 -AsHashtable + switch ($DBFormat) { + "JSON" { $ret = Get-Content $path | ConvertFrom-Json ; Break } + "XML" { $ret = Import-Clixml -Path $path ; Break } + "TXT" { $ret = Get-Content $path ; Break } + default { throw "Unsupported database format $DbFormat" } + } return $ret } @@ -96,9 +124,10 @@ function Get-DatabaseKey{ function Reset-DatabaseKey{ [CmdletBinding()] param( - [Parameter(Position = 0)][string]$Key + [Parameter(Mandatory, Position = 0)][string]$Key, + [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" ) - $path = GetDatabaseFile -Key $Key + $path = GetDatabaseFile -Key $Key -DBFormat $DBFormat Remove-Item -Path $path -Force -ErrorAction SilentlyContinue return } @@ -106,22 +135,30 @@ function Reset-DatabaseKey{ function Save-DatabaseKey{ [CmdletBinding()] param( - [Parameter(Position = 0)][string]$Key, - [Parameter(Position = 2)][Object]$Value + [Parameter(Mandatory, Position = 0)][string]$Key, + [Parameter(Mandatory, Position = 2)][Object]$Value, + [Parameter(Position = 3)][ValidateSet("JSON","XML","TXT")][string]$DbFormat = "JSON" ) - $path = GetDatabaseFile -Key $Key + $path = GetDatabaseFile -Key $Key -DBFormat $DbFormat - $Value | ConvertTo-Json -Depth 10 | Set-Content $path + switch ($DbFormat) { + "JSON" { $Value | ConvertTo-Json -Depth 10 | Set-Content $path -Encoding UTF8 -Force ; Break } + "XML" { $Value | Export-Clixml -Path $path -Force ; Break } + "TXT" { $Value | Set-Content -Path $path -Encoding UTF8 -Force ; Break } + default { throw "Unsupported database format $DbFormat" + } + } } function Test-DatabaseKey{ [CmdletBinding()] param( - [Parameter(Position = 0)][string]$Key + [Parameter(Mandatory, Position = 0)][string]$Key, + [Parameter(Position = 1)][ValidateSet("JSON","XML","TXT")][string]$DBFormat = "JSON" ) - $path = GetDatabaseFile -Key $Key + $path = GetDatabaseFile -Key $Key -DBFormat $DBFormat # Key file not exists if(-Not (Test-Path $path)){ @@ -131,4 +168,6 @@ function Test-DatabaseKey{ # TODO: Return $false if cache has expired return $true -} \ No newline at end of file +} + + diff --git a/include/openFilesUrls.ps1 b/include/openFilesUrls.ps1 index ae60ff1..08ec43b 100644 --- a/include/openFilesUrls.ps1 +++ b/include/openFilesUrls.ps1 @@ -48,7 +48,7 @@ function Open-Url { Write-Error "Failed to open URL: $_" } } -} Export-ModuleMember -Function 'Open-Url' +} function Open-File { [CmdletBinding()] @@ -108,4 +108,4 @@ function Open-File { Write-Error "Failed to open file: $_" } } -} Export-ModuleMember -Function 'Open-File' \ No newline at end of file +} \ No newline at end of file