From 476437bd90b7f4ea73998a58a3410fb3b9dc370d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Tue, 23 Sep 2025 18:05:39 +0200 Subject: [PATCH 1/4] feat(database): add support for multiple database formats (JSON, XML, TXT) --- Test/public/databasev2.test.ps1 | 141 ++++++++++++++++++++++++++++++++ include/databaseV2.ps1 | 63 ++++++++++---- 2 files changed, 189 insertions(+), 15 deletions(-) diff --git a/Test/public/databasev2.test.ps1 b/Test/public/databasev2.test.ps1 index 79e7f87..7da8e34 100644 --- a/Test/public/databasev2.test.ps1 +++ b/Test/public/databasev2.test.ps1 @@ -46,6 +46,147 @@ 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 + $items = Get-ChildItem -Path $StorePath + Assert-Count -Expected 0 -Presented $items + + # 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 + $items = Get-ChildItem -Path $StorePath + Assert-Count -Expected 0 -Presented $items + + # 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 + $items = Get-ChildItem -Path $StorePath + Assert-Count -Expected 0 -Presented $items + + # 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..bb95c54 100644 --- a/include/databaseV2.ps1 +++ b/include/databaseV2.ps1 @@ -66,29 +66,53 @@ 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" + $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 +120,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 +131,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 +164,4 @@ function Test-DatabaseKey{ # TODO: Return $false if cache has expired return $true -} \ No newline at end of file +} From af5bb5b97fed201f2d5ae757962c6a8b41b2256c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Fri, 26 Sep 2025 16:56:41 +0200 Subject: [PATCH 2/4] style(openFilesUrls): remove unnecessary Export-ModuleMember statements --- include/openFilesUrls.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 From 52be21978c69a78d368507af17da0042fa3a1d8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Fri, 10 Oct 2025 19:27:06 +0200 Subject: [PATCH 3/4] fix(database): ensure database root directory is created if it doesn't exist --- Test/include/database.mock.ps1 | 7 +++---- include/databaseV2.ps1 | 6 ++++++ 2 files changed, 9 insertions(+), 4 deletions(-) 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/include/databaseV2.ps1 b/include/databaseV2.ps1 index bb95c54..3fb68f2 100644 --- a/include/databaseV2.ps1 +++ b/include/databaseV2.ps1 @@ -72,6 +72,10 @@ function GetDatabaseFile{ $databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS + 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" @@ -165,3 +169,5 @@ function Test-DatabaseKey{ return $true } + + From 0b9c2d1ef96c7616c69c53f388660aa200f64a30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Fri, 10 Oct 2025 19:31:37 +0200 Subject: [PATCH 4/4] test(database): assert store path does not exist after reset --- Test/public/databasev2.test.ps1 | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Test/public/databasev2.test.ps1 b/Test/public/databasev2.test.ps1 index 7da8e34..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" @@ -62,8 +61,7 @@ function Test_Database_JSON{ # 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" -DBFormat "JSON" @@ -109,8 +107,7 @@ function Test_Database_XML{ # 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" -DBFormat "XML" @@ -156,8 +153,7 @@ function Test_Database_TXT{ # 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" -DBFormat "TXT"