Skip to content

Commit bdb4d30

Browse files
committed
Refactor Test-SqlDscIsAgentAlert to simplify parameter handling and remove unnecessary severity and message ID checks
1 parent 051a08d commit bdb4d30

File tree

3 files changed

+9
-231
lines changed

3 files changed

+9
-231
lines changed
Lines changed: 7 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
<#
22
.SYNOPSIS
3-
Tests if a SQL Agent Alert exists and has the desired properties.
3+
Tests if a SQL Agent Alert exists.
44
55
.DESCRIPTION
66
This command tests if a SQL Agent Alert exists on a SQL Server Database Engine
7-
instance and optionally validates its properties.
7+
instance.
88
99
.PARAMETER ServerObject
1010
Specifies current server connection object.
1111
1212
.PARAMETER Name
1313
Specifies the name of the SQL Agent Alert to test.
1414
15-
.PARAMETER Severity
16-
Specifies the expected severity level for the SQL Agent Alert. Valid range is 0 to 25.
17-
If specified, the command will return $true only if the alert exists and has this severity.
18-
19-
.PARAMETER MessageId
20-
Specifies the expected message ID for the SQL Agent Alert. Valid range is 0 to 2147483647.
21-
If specified, the command will return $true only if the alert exists and has this message ID.
22-
2315
.INPUTS
2416
Microsoft.SqlServer.Management.Smo.Server
2517
@@ -36,15 +28,9 @@
3628
3729
.EXAMPLE
3830
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
39-
$serverObject | Test-SqlDscIsAgentAlert -Name 'MyAlert' -Severity 16
40-
41-
Tests if the SQL Agent Alert named 'MyAlert' exists and has severity level 16.
42-
43-
.EXAMPLE
44-
$serverObject = Connect-SqlDscDatabaseEngine -InstanceName 'MyInstance'
45-
$serverObject | Test-SqlDscIsAgentAlert -Name 'MyAlert' -MessageId 50001
31+
$serverObject | Test-SqlDscIsAgentAlert -Name 'MyAlert'
4632
47-
Tests if the SQL Agent Alert named 'MyAlert' exists and has message ID 50001.
33+
Tests if the SQL Agent Alert named 'MyAlert' exists using pipeline input.
4834
#>
4935
function Test-SqlDscIsAgentAlert
5036
{
@@ -61,28 +47,15 @@ function Test-SqlDscIsAgentAlert
6147
[Parameter(Mandatory = $true)]
6248
[ValidateNotNullOrEmpty()]
6349
[System.String]
64-
$Name,
65-
66-
[Parameter()]
67-
[ValidateRange(0, 25)]
68-
[System.Int32]
69-
$Severity,
70-
71-
[Parameter()]
72-
[ValidateRange(0, 2147483647)]
73-
[System.Int32]
74-
$MessageId
50+
$Name
7551
)
7652

77-
# cSpell: ignore TSAA
53+
# cSpell: ignore TSIAA
7854
process
7955
{
80-
# Validate that both Severity and MessageId are not specified
81-
Assert-BoundParameter -BoundParameterList $PSBoundParameters -MutuallyExclusiveList1 @('Severity') -MutuallyExclusiveList2 @('MessageId')
82-
8356
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_TestingAlert -f $Name)
8457

85-
$alertObject = Get-AgentAlertObject -ServerObject $ServerObject -Name $Name
58+
$alertObject = Get-AgentAlertObject -ServerObject $ServerObject -Name $Name -ErrorAction 'SilentlyContinue'
8659

8760
if ($null -eq $alertObject)
8861
{
@@ -93,46 +66,6 @@ function Test-SqlDscIsAgentAlert
9366

9467
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_AlertFound -f $Name)
9568

96-
# If no specific properties are specified, just return true (alert exists)
97-
if (-not $PSBoundParameters.ContainsKey('Severity') -and -not $PSBoundParameters.ContainsKey('MessageId'))
98-
{
99-
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_NoPropertyTest)
100-
101-
return $true
102-
}
103-
104-
# Test severity if specified
105-
if ($PSBoundParameters.ContainsKey('Severity'))
106-
{
107-
if ($alertObject.Severity -ne $Severity)
108-
{
109-
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_SeverityMismatch -f $alertObject.Severity, $Severity)
110-
111-
return $false
112-
}
113-
else
114-
{
115-
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_SeverityMatch -f $Severity)
116-
}
117-
}
118-
119-
# Test message ID if specified
120-
if ($PSBoundParameters.ContainsKey('MessageId'))
121-
{
122-
if ($alertObject.MessageId -ne $MessageId)
123-
{
124-
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_MessageIdMismatch -f $alertObject.MessageId, $MessageId)
125-
126-
return $false
127-
}
128-
else
129-
{
130-
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_MessageIdMatch -f $MessageId)
131-
}
132-
}
133-
134-
Write-Verbose -Message ($script:localizedData.Test_SqlDscIsAgentAlert_AllTestsPassed -f $Name)
135-
13669
return $true
13770
}
13871
}

tests/Integration/Commands/Test-SqlDscIsAgentAlert.Integration.Tests.ps1

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,39 +75,4 @@ Describe 'Test-SqlDscIsAgentAlert' -Tag @('Integration_SQL2017', 'Integration_SQ
7575
$result | Should -BeFalse
7676
}
7777
}
78-
79-
Context 'When checking severity' {
80-
It 'Should return true for matching severity' {
81-
$result = $script:sqlServerObject | Test-SqlDscIsAgentAlert -Name 'IntegrationTest_SeverityAlert' -Severity 16
82-
83-
$result | Should -BeTrue
84-
}
85-
86-
It 'Should return false for non-matching severity' {
87-
$result = $script:sqlServerObject | Test-SqlDscIsAgentAlert -Name 'IntegrationTest_SeverityAlert' -Severity 14
88-
89-
$result | Should -BeFalse
90-
}
91-
}
92-
93-
Context 'When checking message ID' {
94-
It 'Should return true for matching message ID' {
95-
$result = $script:sqlServerObject | Test-SqlDscIsAgentAlert -Name 'IntegrationTest_MessageIdAlert' -MessageId 50001
96-
97-
$result | Should -BeTrue
98-
}
99-
100-
It 'Should return false for non-matching message ID' {
101-
$result = $script:sqlServerObject | Test-SqlDscIsAgentAlert -Name 'IntegrationTest_MessageIdAlert' -MessageId 50002
102-
103-
$result | Should -BeFalse
104-
}
105-
}
106-
107-
Context 'When both Severity and MessageId are specified' {
108-
It 'Should throw error for both Severity and MessageId parameters' {
109-
{ $script:sqlServerObject | Test-SqlDscIsAgentAlert -Name 'IntegrationTest_SeverityAlert' -Severity 16 -MessageId 50001 } |
110-
Should -Throw
111-
}
112-
}
11378
}

tests/Unit/Public/Test-SqlDscIsAgentAlert.Tests.ps1

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
21
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', Justification = 'Suppressing this rule because Script Analyzer does not understand Pester syntax.')]
32
param ()
43

@@ -49,7 +48,7 @@ Describe 'Test-SqlDscIsAgentAlert' -Tag 'Public' {
4948
It 'Should have the correct parameters in parameter set <ExpectedParameterSetName>' -ForEach @(
5049
@{
5150
ExpectedParameterSetName = '__AllParameterSets'
52-
ExpectedParameters = '[-ServerObject] <Server> [-Name] <String> [[-Severity] <int>] [[-MessageId] <int>] [<CommonParameters>]'
51+
ExpectedParameters = '[-ServerObject] <Server> [-Name] <string> [<CommonParameters>]'
5352
}
5453
) {
5554
$result = (Get-Command -Name 'Test-SqlDscIsAgentAlert').ParameterSets |
@@ -78,124 +77,33 @@ Describe 'Test-SqlDscIsAgentAlert' -Tag 'Public' {
7877
$parameterInfo = (Get-Command -Name 'Test-SqlDscIsAgentAlert').Parameters['Name']
7978
$parameterInfo.Attributes.Mandatory | Should -BeTrue
8079
}
81-
82-
It 'Should have Severity as an optional parameter' {
83-
$parameterInfo = (Get-Command -Name 'Test-SqlDscIsAgentAlert').Parameters['Severity']
84-
$parameterInfo.Attributes.Mandatory | Should -BeFalse
85-
}
86-
87-
It 'Should have MessageId as an optional parameter' {
88-
$parameterInfo = (Get-Command -Name 'Test-SqlDscIsAgentAlert').Parameters['MessageId']
89-
$parameterInfo.Attributes.Mandatory | Should -BeFalse
90-
}
91-
}
92-
93-
Context 'When validating parameter ranges' {
94-
It 'Should accept valid Severity values (0-25)' {
95-
$command = Get-Command -Name 'Test-SqlDscIsAgentAlert'
96-
$severityParam = $command.Parameters['Severity']
97-
$validateRangeAttribute = $severityParam.Attributes | Where-Object { $_ -is [System.Management.Automation.ValidateRangeAttribute] }
98-
99-
$validateRangeAttribute.MinRange | Should -Be 0
100-
$validateRangeAttribute.MaxRange | Should -Be 25
101-
}
102-
103-
It 'Should accept valid MessageId values (0-2147483647)' {
104-
$command = Get-Command -Name 'Test-SqlDscIsAgentAlert'
105-
$messageIdParam = $command.Parameters['MessageId']
106-
$validateRangeAttribute = $messageIdParam.Attributes | Where-Object { $_ -is [System.Management.Automation.ValidateRangeAttribute] }
107-
108-
$validateRangeAttribute.MinRange | Should -Be 0
109-
$validateRangeAttribute.MaxRange | Should -Be 2147483647
110-
}
11180
}
11281

11382
Context 'When testing alert existence only' {
11483
BeforeAll {
11584
# Mock the alert object using SMO stub types
11685
$script:mockAlert = [Microsoft.SqlServer.Management.Smo.Agent.Alert]::CreateTypeInstance()
11786
$script:mockAlert.Name = 'TestAlert'
118-
$script:mockAlert.Severity = 16
119-
$script:mockAlert.MessageId = 0
12087

12188
# Mock server object using SMO stub types
12289
$script:mockServerObject = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance()
12390

124-
Mock -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName
12591
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -MockWith { return $script:mockAlert }
12692
}
12793

128-
It 'Should return true when alert exists and no properties are specified' {
94+
It 'Should return true when alert exists' {
12995
$result = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert'
13096

13197
$result | Should -BeTrue
132-
Should -Invoke -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName -Times 1 -Exactly
13398
Should -Invoke -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -Times 1 -Exactly
13499
}
135100
}
136101

137-
Context 'When testing alert with severity' {
138-
BeforeAll {
139-
# Mock the alert object with specific severity using SMO stub types
140-
$script:mockAlert = [Microsoft.SqlServer.Management.Smo.Agent.Alert]::CreateTypeInstance()
141-
$script:mockAlert.Name = 'TestAlert'
142-
$script:mockAlert.Severity = 16
143-
$script:mockAlert.MessageId = 0
144-
145-
# Mock server object using SMO stub types
146-
$script:mockServerObject = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance()
147-
148-
Mock -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName
149-
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -MockWith { return $script:mockAlert }
150-
}
151-
152-
It 'Should return true when alert exists and severity matches' {
153-
$result = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
154-
155-
$result | Should -BeTrue
156-
}
157-
158-
It 'Should return false when alert exists but severity does not match' {
159-
$result = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 14
160-
161-
$result | Should -BeFalse
162-
}
163-
}
164-
165-
Context 'When testing alert with message ID' {
166-
BeforeAll {
167-
# Mock the alert object with specific message ID using SMO stub types
168-
$script:mockAlert = [Microsoft.SqlServer.Management.Smo.Agent.Alert]::CreateTypeInstance()
169-
$script:mockAlert.Name = 'TestAlert'
170-
$script:mockAlert.Severity = 0
171-
$script:mockAlert.MessageId = 50001
172-
173-
# Mock server object using SMO stub types
174-
$script:mockServerObject = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance()
175-
176-
Mock -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName
177-
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -MockWith { return $script:mockAlert }
178-
}
179-
180-
It 'Should return true when alert exists and message ID matches' {
181-
$result = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -MessageId 50001
182-
183-
$result | Should -BeTrue
184-
}
185-
186-
It 'Should return false when alert exists but message ID does not match' {
187-
$result = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -MessageId 50002
188-
189-
$result | Should -BeFalse
190-
}
191-
}
192-
193102
Context 'When alert does not exist' {
194103
BeforeAll {
195104
# Mock server object using SMO stub types
196105
$script:mockServerObject = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance()
197106

198-
Mock -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName
199107
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName
200108
}
201109

@@ -207,32 +115,6 @@ Describe 'Test-SqlDscIsAgentAlert' -Tag 'Public' {
207115
}
208116
}
209117

210-
Context 'When parameter validation is called' {
211-
BeforeAll {
212-
# Mock server object using SMO stub types
213-
$script:mockServerObject = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance()
214-
215-
Mock -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName
216-
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName
217-
}
218-
219-
It 'Should call parameter validation with both severity and message ID' {
220-
$null = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16 -MessageId 50001
221-
222-
Should -Invoke -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName -ParameterFilter {
223-
$BoundParameterList.ContainsKey('Severity') -and $BoundParameterList.ContainsKey('MessageId')
224-
} -Times 1 -Exactly
225-
}
226-
227-
It 'Should call parameter validation with only severity' {
228-
$null = Test-SqlDscIsAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
229-
230-
Should -Invoke -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName -ParameterFilter {
231-
$BoundParameterList.ContainsKey('Severity') -and -not $BoundParameterList.ContainsKey('MessageId')
232-
} -Times 1 -Exactly
233-
}
234-
}
235-
236118
Context 'When using the alias Test-SqlDscAgentAlert' {
237119
BeforeAll {
238120
# Mock the alert object using SMO stub types
@@ -242,15 +124,13 @@ Describe 'Test-SqlDscIsAgentAlert' -Tag 'Public' {
242124
# Mock the server object using SMO stub types
243125
$script:mockServerObject = [Microsoft.SqlServer.Management.Smo.Server]::CreateTypeInstance()
244126

245-
Mock -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName
246127
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -MockWith { return $script:mockAlert }
247128
}
248129

249130
It 'Should work with alias Test-SqlDscAgentAlert' {
250131
$result = Test-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert'
251132

252133
$result | Should -BeTrue
253-
Should -Invoke -CommandName 'Assert-BoundParameter' -ModuleName $script:dscModuleName -Times 1 -Exactly
254134
Should -Invoke -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -Times 1 -Exactly
255135
}
256136
}

0 commit comments

Comments
 (0)