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
7 changes: 3 additions & 4 deletions Test/include/database.mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
141 changes: 139 additions & 2 deletions Test/public/databasev2.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
69 changes: 54 additions & 15 deletions include/databaseV2.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,62 +66,99 @@ 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
}

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
}

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)){
Expand All @@ -131,4 +168,6 @@ function Test-DatabaseKey{
# TODO: Return $false if cache has expired

return $true
}
}


4 changes: 2 additions & 2 deletions include/openFilesUrls.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function Open-Url {
Write-Error "Failed to open URL: $_"
}
}
} Export-ModuleMember -Function 'Open-Url'
}

function Open-File {
[CmdletBinding()]
Expand Down Expand Up @@ -108,4 +108,4 @@ function Open-File {
Write-Error "Failed to open file: $_"
}
}
} Export-ModuleMember -Function 'Open-File'
}