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
4 changes: 4 additions & 0 deletions Test/include/database.mock.config.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# This file is part of the Database Mock Include.

$DB_INVOKE_GET_ROOT_PATH_ALIAS = "CsvHelperGetDbRootPath"

Check warning

Code scanning / PSScriptAnalyzer

The variable 'DB_INVOKE_GET_ROOT_PATH_ALIAS' is assigned but never used. Warning

The variable 'DB_INVOKE_GET_ROOT_PATH_ALIAS' is assigned but never used.
48 changes: 48 additions & 0 deletions Test/include/database.mock.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# DATABASE MOCK

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
#
# This file is used to mock the database path and the database file
# for the tests. It creates a mock database path and a mock database file
# and sets the database path to the mock database path.
# We need to define variables for this include to work
# $MOCK_DATABASE_PATH : The path used as the mock database folder
# $DB_INVOKE_GET_ROOT_PATH_CMD : Invoke command that is needed to be mocked
# $DB_INVOKE_GET_ROOT_PATH_ALIAS : Invoke function to retreive the root path of the database
#
# Sample file
# # DATABASE MOCK VARIABLES
# # This file is required for DATABASE MOCK to work
# $DB_INVOKE_GET_ROOT_PATH_ALIAS = "MyModuleGetDbRootPath"
#

$DB_INVOKE_GET_ROOT_PATH_CMD = "Invoke-$DB_INVOKE_GET_ROOT_PATH_ALIAS"
$MOCK_DATABASE_PATH = "test_database_path"

Check warning

Code scanning / PSScriptAnalyzer

The variable 'MOCK_DATABASE_PATH' is assigned but never used. Warning

The variable 'MOCK_DATABASE_PATH' is assigned but never used.

function Mock_Database([switch]$ResetDatabase){

MockCallToString $DB_INVOKE_GET_ROOT_PATH_CMD -OutString "test_database_path"

$dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD
Assert-AreEqual -Expected "test_database_path" -Presented $dbstore

if($ResetDatabase){
Reset-DatabaseStore
}

}

function Get-Mock_DatabaseStore{
$dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD
return $dbstore
}

function Reset-DatabaseStore{

Check warning

Code scanning / PSScriptAnalyzer

Function 'Reset-DatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'Reset-DatabaseStore' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
[CmdletBinding()]
param()

$databaseRoot = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_ALIAS

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
Remove-Item -Path $databaseRoot -Recurse -Force -ErrorAction SilentlyContinue

New-Item -Path $databaseRoot -ItemType Directory

}
188 changes: 188 additions & 0 deletions Test/include/invokeCommand.Mock.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@

# INVOKE COMMAND MOCK
#
# This includes help commands to mock invokes in a test module
# You need to set the following variables
# $MODULE_INVOKATION_TAG : name of the module that you are testing. This needs to match with the Tag used in the module you are testing.
# $MODULE_INVOKATION_TAG_MOCK : Tag for the mock functions on the testing moodule you are loading this include in
# MOCK_PATH : path to the mocks folder. This is where the mock files will be saved and loaded from.
#
# Sample:
# $MODULE_INVOKATION_TAG = "SfHelperModule"
# $MODULE_INVOKATION_TAG_MOCK = "SfHelperModule-Mock"
# $MOCK_PATH = $PSScriptRoot | Split-Path -Parent | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks'


function Set-InvokeCommandMock{

Check warning

Code scanning / PSScriptAnalyzer

Function 'Set-InvokeCommandMock' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'Set-InvokeCommandMock' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)][string]$Alias,
[Parameter(Mandatory,Position=1)][string]$Command
)

InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG_MOCK
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

function Reset-InvokeCommandMock{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Reset-InvokeCommandMock' does not have a help comment. Note

The cmdlet 'Reset-InvokeCommandMock' does not have a help comment.

Check warning

Code scanning / PSScriptAnalyzer

Function 'Reset-InvokeCommandMock' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'Reset-InvokeCommandMock' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
[CmdletBinding()]
param()

# Remove all mocks
InvokeHelper\Reset-InvokeCommandAlias -Tag $MODULE_INVOKATION_TAG_MOCK

# Disable all dependecies of the library
Disable-InvokeCommandAlias -Tag $MODULE_INVOKATION_TAG
} Export-ModuleMember -Function Reset-InvokeCommandMock

function Enable-InvokeCommandAliasModule{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Enable-InvokeCommandAliasModule' does not have a help comment. Note

The cmdlet 'Enable-InvokeCommandAliasModule' does not have a help comment.
[CmdletBinding()]
param()

Enable-InvokeCommandAlias -Tag $MODULE_INVOKATION_TAG
} Export-ModuleMember -Function Enable-InvokeCommandAliasModule

function MockCall{
param(
[Parameter(Position=0)][string] $command,
[Parameter(Position=1)][string] $filename
)

Assert-MockFileNotfound $fileName

Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContent -filename $filename"
}

function MockCallJson{
param(
[Parameter(Position=0)][string] $command,
[Parameter(Position=1)][string] $filename

)

Assert-MockFileNotfound $fileName

Set-InvokeCommandMock -Alias $command -Command "Get-MockFileContentJson -filename $filename"
}

function Get-MockFileFullPath{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-MockFileFullPath' does not have a help comment. Note

The cmdlet 'Get-MockFileFullPath' does not have a help comment.
param(
[parameter(Mandatory,Position=0)][string] $fileName
)

$filePath = $MOCK_PATH | Join-Path -ChildPath $fileName

return $filePath
} Export-ModuleMember -Function Get-MockFileFullPath

function Get-MockFileContent{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-MockFileContent' does not have a help comment. Note

The cmdlet 'Get-MockFileContent' does not have a help comment.
param(
[parameter(Mandatory,Position=0)][string] $fileName
)

Assert-MockFileNotfound $FileName

$filePath = Get-MockFileFullPath -fileName $fileName

$content = Get-Content -Path $filePath | Out-String

return $content
} Export-ModuleMember -Function Get-MockFileContent

function Get-MockFileContentJson{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Get-MockFileContentJson' does not have a help comment. Note

The cmdlet 'Get-MockFileContentJson' does not have a help comment.
param(
[parameter(Mandatory,Position=0)][string] $fileName
)

Assert-MockFileNotfound $FileName

$content = Get-MockFileContent -fileName $filename | ConvertFrom-Json

return $content
} Export-ModuleMember -Function Get-MockFileContentJson

function MockCallToString{
param(
[Parameter(Position=0)][string] $command,
[Parameter(Position=1)][string] $OutString
)

$outputstring = 'echo "{output}"'
$outputstring = $outputstring -replace "{output}", $OutString

Set-InvokeCommandMock -Alias $command -Command $outputstring
}

function MockCallToNull{
param(
[Parameter(Position=0)][string] $command
)

Set-InvokeCommandMock -Alias $command -Command 'return $null'
}

function MockCallThrow{
param(
[Parameter(Position=0)][string] $command,
[Parameter(Position=1)][string] $ExceptionMessage

)

$mockCommand = 'throw "{message}"'
$mockCommand = $mockCommand -replace "{message}", $exceptionMessage

Set-InvokeCommandMock -Alias $command -Command $mockCommand
}

function Save-InvokeAsMockFile{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Save-InvokeAsMockFile' does not have a help comment. Note

The cmdlet 'Save-InvokeAsMockFile' does not have a help comment.
param(
[Parameter(Mandatory=$true)] [string]$Command,
[Parameter(Mandatory=$true)] [string]$FileName,
[Parameter(Mandatory=$false)] [switch]$Force

Check warning

Code scanning / PSScriptAnalyzer

The parameter 'Force' has been declared but not used. Warning

The parameter 'Force' has been declared but not used.
)

$filePath = Get-MockFileFullPath -fileName $fileName

$result = Invoke-Expression -Command $Command

Check warning

Code scanning / PSScriptAnalyzer

Invoke-Expression is used. Please remove Invoke-Expression from script and find other options instead. Warning

Invoke-Expression is used. Please remove Invoke-Expression from script and find other options instead.

$json = $result | ConvertTo-Json -Depth 100

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$json | Out-File -FilePath $filePath

Write-Host $FileName

Check warning

Code scanning / PSScriptAnalyzer

File 'invokeCommand.Mock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'invokeCommand.Mock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
} Export-ModuleMember -Function Save-InvokeAsMockFile

function Save-InvokeAsMockFileJson{

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Save-InvokeAsMockFileJson' does not have a help comment. Note

The cmdlet 'Save-InvokeAsMockFileJson' does not have a help comment.
param(
[Parameter(Mandatory=$true)] [string]$Command,
[Parameter(Mandatory=$true)] [string]$FileName
)

$filePath = Get-MockFileFullPath -fileName $fileName

$result = Invoke-Expression -Command $Command

Check warning

Code scanning / PSScriptAnalyzer

Invoke-Expression is used. Please remove Invoke-Expression from script and find other options instead. Warning

Invoke-Expression is used. Please remove Invoke-Expression from script and find other options instead.

$result | Out-File -FilePath $filePath

Write-Host $FileName

Check warning

Code scanning / PSScriptAnalyzer

File 'invokeCommand.Mock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'invokeCommand.Mock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
} Export-ModuleMember -Function Save-InvokeAsMockFileJson

function Assert-MockFileNotfound{
param(
[Parameter(Mandatory=$true,Position=0)] [string]$FileName
)

$filePath = Get-MockFileFullPath -fileName $fileName

if(-Not (Test-Path -Path $filePath)){
throw "File not found: $fileName"
}

# Throw if $file.name and the $filename parameter have different case
# We need to check this to avoid test bugs for mock files not found on linux that the FS is case sensitive
$file = Get-ChildItem -Path $MOCK_PATH | Where-Object { $_.Name.ToLower() -eq $fileName.ToLower() }
if($file.name -cne $fileName){
Write-host "Wait-Debugger - File not found or wrong case - $($file.name)"

Check warning

Code scanning / PSScriptAnalyzer

File 'invokeCommand.Mock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'invokeCommand.Mock.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
Wait-Debugger
throw "File not found or wrong case name. Expected[ $filename ] - Found[$( $file.name )]"
}
}
21 changes: 21 additions & 0 deletions Test/private/NewCsvTestFile.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function New-TestCsvFile {

Check warning

Code scanning / PSScriptAnalyzer

Function 'New-TestCsvFile' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'New-TestCsvFile' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
param (
[int]$NumRows = 10,
[string]$Path = "test.csv"
)

$data = @()

for ($i = 1; $i -le $numRows; $i++) {
$data += [PSCustomObject]@{
Id = "ValueId_$i"
Column1 = "Value1_$i"
Column2 = "Value2_$i"
Column3 = "Value3_$i"
}
}

$data | Export-Csv -Path $Path -NoTypeInformation

return $Path
}
49 changes: 49 additions & 0 deletions Test/public/CsvFile.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

function Test_GetCsvFile{

Mock_Database -resetDatabase
$databaseRoot = Get-Mock_DatabaseStore
$csvPath = New-TestCsvFile

# Act
$key = Import-CsvFile -Path $csvPath -KeyColumn "Column1"

# Assert
Assert-IsNotNull -Object $key
Assert-Count -Expected 1 -Presented (Get-ChildItem -Path $databaseRoot)
Assert-ItemExist -Path "$databaseRoot/$key.json"

# read database file from json
$data = Get-Content -Path "$databaseRoot/$key.json" | ConvertFrom-Json -Depth 10 -AsHashtable

# Random record
$i = 5
$record2 = $data["Value1_$i"]

Assert-AreEqual -Expected "Value1_$i" -Presented $record2.Column1
Assert-AreEqual -Expected "Value2_$i" -Presented $record2.Column2
Assert-AreEqual -Expected "Value3_$i" -Presented $record2.Column3
}

function Test_GetCsvFile_Default_Id{

Mock_Database -resetDatabase
$databaseRoot = Get-Mock_DatabaseStore
$csvPath = New-TestCsvFile

# Act
$key = Import-CsvFile -Path $csvPath

# Assert
# read database file from json
$data = Get-Content -Path "$databaseRoot/$key.json" | ConvertFrom-Json -Depth 10 -AsHashtable

# Random record
$i = 5
$record2 = $data["ValueId_$i"]

Assert-AreEqual -Expected "Value1_$i" -Presented $record2.Column1
Assert-AreEqual -Expected "Value2_$i" -Presented $record2.Column2
Assert-AreEqual -Expected "Value3_$i" -Presented $record2.Column3
}

53 changes: 53 additions & 0 deletions Test/public/CsvRecord.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

function Test_GetCsvRecord{

Mock_Database -resetDatabase

$csvPath = New-TestCsvFile

$key = Import-CsvFile -Path $csvPath -KeyColumn "Column1"

Assert-IsNotNull -Object $key

# Act get record with csv with path
$record = Get-CsvRecord -Path $csvPath -KeyColumn "Column1" -Id "Value1_2"

Assert-AreEqual -Expected "Value1_2" -Presented $record.Column1
Assert-AreEqual -Expected "Value2_2" -Presented $record.Column2
Assert-AreEqual -Expected "Value3_2" -Presented $record.Column3

# Call with Key
$record = Get-CsvRecord -Key $key -Id "Value1_3"

Assert-AreEqual -Expected "Value1_3" -Presented $record.Column1
Assert-AreEqual -Expected "Value2_3" -Presented $record.Column2
Assert-AreEqual -Expected "Value3_3" -Presented $record.Column3
}

function Test_GetCsvRecord_Integration{

Mock_Database -resetDatabase

$csvPath = New-TestCsvFile

# Act get record with csv with path
$record = Get-CsvRecord -Path $csvPath -KeyColumn "Column1" -Id "Value1_2"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

Assert-AreEqual -Expected "Value1_2" -Presented $record.Column1
Assert-AreEqual -Expected "Value2_2" -Presented $record.Column2
Assert-AreEqual -Expected "Value3_2" -Presented $record.Column3
}

function Test_GetCsvRecord_Integration_Id{

Mock_Database -resetDatabase

$csvPath = New-TestCsvFile

# Act get record with csv with path
$record = Get-CsvRecord -Path $csvPath -Id "ValueId_2"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace

Assert-AreEqual -Expected "Value1_2" -Presented $record.Column1
Assert-AreEqual -Expected "Value2_2" -Presented $record.Column2
Assert-AreEqual -Expected "Value3_2" -Presented $record.Column3
}
Loading