Public/InstallHelpers/Install.Database.Functions.Tests.ps1
#Requires -Version 5.0 #Requires -Modules @{ ModuleName="Pester"; ModuleVersion="3.4.0" } $ErrorActionPreference = "Stop"; Set-StrictMode -Version 'Latest' $sut = (Split-Path -Leaf $MyInvocation.MyCommand.Path) -replace '\.Tests\.ps1', '.psm1' Import-Module "$PSScriptRoot\$sut" -Force # DANGER: This test will drop a specified database. # NOTE: This test requires an unnamed SqlServer instance running locally # on the standard port(1433) and SSMS (for Invoke-Sqlcmd). # Describe 'RunSchemaBuilder' -Tags 'Integration' { # # Arrange # $dbName = 'TrainingNotifier_Test' # $exeArgs = "-v true -h localhost -d $dbName -i true" # $dropDbCmd = "ALTER DATABASE [$dbName] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [$dbName]" # Invoke-Sqlcmd -Query $dropDbCmd -HostName 'Localhost' | Out-Null # $pathToExe = "$PSScriptRoot\Fixtures\TN.SchemaBuilder\App.SchemaBuilder.exe" # # Act # RunSchemaBuilder -pathToExe $pathToExe -argsString $exeArgs # $actual = Invoke-Sqlcmd -Query "SELECT DB_ID('$dbName')" -HostName 'Localhost' # # Assert # It "returns expected" { # $actual.Column1 | Should Not Be $null # } # } Describe 'RunSql' -Tags 'Integration' { It 'returns hashtable with ExitCode prop set to 0 on success' { # Arrange [System.Collections.Hashtable] $dbaQueryParams = @{ SqlInstance = $Env:ComputerName Query = 'SELECT 1 UNION SELECT 2' EnableException = $true } # Act [System.Collections.Hashtable] $actual = RunSql $dbaQueryParams # Assert $actual['ExitCode'] | Should Be 0 } It 'returns hashtable with ExitCode prop set to 1 on error' { # Arrange [System.Collections.Hashtable] $dbaQueryParams = @{ SqlInstance = $Env:ComputerName Query = 'SELECT FakeColumn From NonExistentTable' EnableException = $true } # Act [System.Collections.Hashtable] $actual = RunSql $dbaQueryParams # Assert $actual['ExitCode'] | Should Be 1 } It 'can run non-parameterized SQL' { # Arrange [System.Collections.Hashtable] $dbaQueryParams = @{ SqlInstance = $Env:ComputerName Query = 'SELECT 1 UNION SELECT 2' EnableException = $true } # Act [System.Collections.HashTable] $actual = RunSql $dbaQueryParams # Assert $actual['QueryResult'].Length | Should Be 2 $actual['QueryResult'][0]['Column1'] | Should Be 1 } It 'can run parameterized SQL' { # Arrange $expected = 'Bob' $testColumnName = 'Name' [System.Collections.Hashtable] $dbaQueryParams = @{ SqlInstance = $Env:ComputerName # TODO: Kind of subverting the reason for using parameterization here... Query = "SELECT * FROM (SELECT $testColumnName = 'Bob') AS FauxTable WHERE $testColumnName = @Name" SqlParameters = @{ Name = "$expected" } EnableException = $true } # Act [System.Collections.HashTable] $actual = RunSql $dbaQueryParams # Assert $actual['QueryResult']["$testColumnName"] | Should Be $expected } } Describe 'EncryptData' -Tags 'Unit' { It 'returns expected' { # Arrange $expected = 'DXdHfp8jLYdyqUPskaBRc+GMck6FDa50' $data = 'diaz-bao' # Act $actual = EncryptData $data # Assert $actual | Should Be $expected } } |