-
-
Notifications
You must be signed in to change notification settings - Fork 808
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d3fe10
commit 78cb317
Showing
85 changed files
with
4,751 additions
and
2,436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,146 @@ | ||
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "") | ||
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan | ||
. "$PSScriptRoot\constants.ps1" | ||
param($ModuleName = 'dbatools') | ||
|
||
Describe "$CommandName Unit Tests" -Tag 'UnitTests' { | ||
Context "Validate parameters" { | ||
[array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys | ||
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'MessageID', 'Severity', 'MessageText', 'Language', 'WithLog', 'EnableException' | ||
It "Should only contain our specific parameters" { | ||
Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty | ||
} | ||
} | ||
} | ||
|
||
Describe "$commandname Integration Tests" -Tags "IntegrationTests" { | ||
Describe "New-DbaCustomError" { | ||
BeforeAll { | ||
$server = Connect-DbaInstance -SqlInstance $script:instance1 | ||
$server2 = Connect-DbaInstance -SqlInstance $script:instance2 | ||
. "$PSScriptRoot\constants.ps1" | ||
} | ||
AfterAll { | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70000) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70000, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70001) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70001, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70002) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70002, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70003) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70003, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70004) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70004, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70005) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70005, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70006) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70006, @lang = 'all'; END") | ||
$server2.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70006) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70006, @lang = 'all'; END") | ||
} | ||
|
||
Context "Validate params" { | ||
|
||
It "Message ID" { | ||
{ $results = New-DbaCustomError -SqlInstance $server -MessageID 1 -Severity 1 -MessageText "test 1" -Language English } | Should -Throw | ||
{ $results = New-DbaCustomError -SqlInstance $server -MessageID 2147483648 -Severity 1 -MessageText "test 1" -Language English } | Should -Throw | ||
Context "Validate parameters" { | ||
BeforeAll { | ||
$CommandUnderTest = Get-Command New-DbaCustomError | ||
} | ||
It "Should have SqlInstance as a non-mandatory parameter of type DbaInstanceParameter[]" { | ||
$CommandUnderTest | Should -HaveParameter SqlInstance -Type DbaInstanceParameter[] -Not -Mandatory | ||
} | ||
It "Should have SqlCredential as a non-mandatory parameter of type PSCredential" { | ||
$CommandUnderTest | Should -HaveParameter SqlCredential -Type PSCredential -Not -Mandatory | ||
} | ||
It "Should have MessageID as a non-mandatory parameter of type Int32" { | ||
$CommandUnderTest | Should -HaveParameter MessageID -Type Int32 -Not -Mandatory | ||
} | ||
It "Should have Severity as a non-mandatory parameter of type Int32" { | ||
$CommandUnderTest | Should -HaveParameter Severity -Type Int32 -Not -Mandatory | ||
} | ||
It "Should have MessageText as a non-mandatory parameter of type String" { | ||
$CommandUnderTest | Should -HaveParameter MessageText -Type String -Not -Mandatory | ||
} | ||
It "Should have Language as a non-mandatory parameter of type String" { | ||
$CommandUnderTest | Should -HaveParameter Language -Type String -Not -Mandatory | ||
} | ||
It "Should have WithLog as a non-mandatory switch parameter" { | ||
$CommandUnderTest | Should -HaveParameter WithLog -Type Switch -Not -Mandatory | ||
} | ||
It "Should have EnableException as a non-mandatory switch parameter" { | ||
$CommandUnderTest | Should -HaveParameter EnableException -Type Switch -Not -Mandatory | ||
} | ||
} | ||
|
||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70000 -Severity 16 -MessageText "test_70000" | ||
$results.Count | Should -Be 1 | ||
$results.ID | Should -Be 70000 | ||
Context "Command usage" { | ||
BeforeAll { | ||
$server = Connect-DbaInstance -SqlInstance $script:instance1 | ||
$server2 = Connect-DbaInstance -SqlInstance $script:instance2 | ||
} | ||
AfterAll { | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70000) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70000, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70001) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70001, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70002) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70002, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70003) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70003, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70004) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70004, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70005) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70005, @lang = 'all'; END") | ||
$server.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70006) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70006, @lang = 'all'; END") | ||
$server2.Query("IF EXISTS (SELECT 1 FROM master.sys.messages WHERE message_id = 70006) BEGIN EXEC msdb.dbo.sp_dropmessage @msgnum = 70006, @lang = 'all'; END") | ||
} | ||
|
||
Context "Validate Message ID" { | ||
It "Should throw an error for invalid Message ID" { | ||
{ New-DbaCustomError -SqlInstance $server -MessageID 1 -Severity 1 -MessageText "test 1" -Language English } | Should -Throw | ||
{ New-DbaCustomError -SqlInstance $server -MessageID 2147483648 -Severity 1 -MessageText "test 1" -Language English } | Should -Throw | ||
} | ||
|
||
It "Severity" { | ||
{ $results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 0 -MessageText "test 1" -Language English } | Should -Throw | ||
{ $results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 26 -MessageText "test 1" -Language English } | Should -Throw | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 16 -MessageText "test_70001" | ||
$results.Count | Should -Be 1 | ||
$results.Severity | Should -Be 16 | ||
It "Should create a custom error with valid Message ID" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70000 -Severity 16 -MessageText "test_70000" | ||
$results.Count | Should -Be 1 | ||
$results.ID | Should -Be 70000 | ||
} | ||
} | ||
|
||
It "MessageText" { | ||
{ $results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 1 -MessageText "test message that has a string length greater than 255 characters. test message that has a string length greater than 255 characters. test message that has a string length greater than 255 characters. test message that has a string length greater than 255 characters" -Language English } | Should -Throw | ||
Context "Validate Severity" { | ||
It "Should throw an error for invalid Severity" { | ||
{ New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 0 -MessageText "test 1" -Language English } | Should -Throw | ||
{ New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 26 -MessageText "test 1" -Language English } | Should -Throw | ||
} | ||
|
||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70002 -Severity 1 -MessageText "test_70002" | ||
$results.Count | Should -Be 1 | ||
$results.Text | Should -Be "test_70002" | ||
It "Should create a custom error with valid Severity" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 16 -MessageText "test_70001" | ||
$results.Count | Should -Be 1 | ||
$results.Severity | Should -Be 16 | ||
} | ||
} | ||
|
||
It "Language" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 1 -MessageText "test" -Language "InvalidLanguage" | ||
$results | Should -BeNullOrEmpty | ||
Context "Validate MessageText" { | ||
It "Should throw an error for MessageText longer than 255 characters" { | ||
{ New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 1 -MessageText "test message that has a string length greater than 255 characters. test message that has a string length greater than 255 characters. test message that has a string length greater than 255 characters. test message that has a string length greater than 255 characters" -Language English } | Should -Throw | ||
} | ||
|
||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70003 -Severity 1 -MessageText "test_70003" -Language "English" | ||
$results.Count | Should -Be 1 | ||
$results.Language | Should -Match "English" | ||
$results.Text | Should -Be "test_70003" | ||
$results.ID | Should -Be 70003 | ||
$results.Severity | Should -Be 1 | ||
It "Should create a custom error with valid MessageText" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70002 -Severity 1 -MessageText "test_70002" | ||
$results.Count | Should -Be 1 | ||
$results.Text | Should -Be "test_70002" | ||
} | ||
} | ||
|
||
# add other languages available now that the english message is added | ||
$languages = $server.Query("SELECT alias FROM sys.syslanguages WHERE alias NOT LIKE '%English%'") | ||
Context "Validate Language" { | ||
It "Should return null for invalid Language" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70001 -Severity 1 -MessageText "test" -Language "InvalidLanguage" | ||
$results | Should -BeNullOrEmpty | ||
} | ||
|
||
foreach ($lang in $languages) { | ||
$languageName = $lang.alias | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70003 -Severity 1 -MessageText "test_70003_$languageName" -Language "$languageName" | ||
It "Should create a custom error with valid Language" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70003 -Severity 1 -MessageText "test_70003" -Language "English" | ||
$results.Count | Should -Be 1 | ||
$results.Language | Should -Match "$languageName" | ||
$results.Text | Should -Be "test_70003_$languageName" | ||
$results.Language | Should -Match "English" | ||
$results.Text | Should -Be "test_70003" | ||
$results.ID | Should -Be 70003 | ||
$results.Severity | Should -Be 1 | ||
} | ||
} | ||
|
||
It "WithLog" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70005 -Severity 25 -MessageText "test_70005" -WithLog | ||
$results.Count | Should -Be 1 | ||
$results.Text | Should -Be "test_70005" | ||
$results.Severity | Should -Be 25 | ||
$results.ID | Should -Be 70005 | ||
$results.IsLogged | Should -Be $true | ||
It "Should create custom errors for multiple languages" { | ||
$languages = $server.Query("SELECT alias FROM sys.syslanguages WHERE alias NOT LIKE '%English%'") | ||
|
||
foreach ($lang in $languages) { | ||
$languageName = $lang.alias | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70003 -Severity 1 -MessageText "test_70003_$languageName" -Language "$languageName" | ||
$results.Count | Should -Be 1 | ||
$results.Language | Should -Match "$languageName" | ||
$results.Text | Should -Be "test_70003_$languageName" | ||
$results.ID | Should -Be 70003 | ||
$results.Severity | Should -Be 1 | ||
} | ||
} | ||
} | ||
} | ||
|
||
Context "Supports multiple server inputs" { | ||
Context "Validate WithLog" { | ||
It "Should create a custom error with WithLog" { | ||
$results = New-DbaCustomError -SqlInstance $server -MessageID 70005 -Severity 25 -MessageText "test_70005" -WithLog | ||
$results.Count | Should -Be 1 | ||
$results.Text | Should -Be "test_70005" | ||
$results.Severity | Should -Be 25 | ||
$results.ID | Should -Be 70005 | ||
$results.IsLogged | Should -Be $true | ||
} | ||
} | ||
|
||
It "Add messages to preconnected servers" { | ||
$results = ([DbaInstanceParameter[]]$server, $server2 | New-DbaCustomError -MessageID 70006 -Severity 20 -MessageText "test_70006") | ||
$results.Count | Should -Be 2 | ||
$results[0].Text | Should -Be "test_70006" | ||
$results[1].Text | Should -Be "test_70006" | ||
$results[0].Severity | Should -Be 20 | ||
$results[1].Severity | Should -Be 20 | ||
$results[0].ID | Should -Be 70006 | ||
$results[1].ID | Should -Be 70006 | ||
Context "Supports multiple server inputs" { | ||
It "Should add messages to preconnected servers" { | ||
$results = ([DbaInstanceParameter[]]$server, $server2 | New-DbaCustomError -MessageID 70006 -Severity 20 -MessageText "test_70006") | ||
$results.Count | Should -Be 2 | ||
$results[0].Text | Should -Be "test_70006" | ||
$results[1].Text | Should -Be "test_70006" | ||
$results[0].Severity | Should -Be 20 | ||
$results[1].Severity | Should -Be 20 | ||
$results[0].ID | Should -Be 70006 | ||
$results[1].ID | Should -Be 70006 | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.