Skip to content

Commit d82b5f8

Browse files
committed
Add tests to ensure Severity and MessageId are reset to avoid conflicts during updates
1 parent bc7871f commit d82b5f8

File tree

1 file changed

+145
-12
lines changed

1 file changed

+145
-12
lines changed

tests/Unit/Public/Set-SqlDscAgentAlert.Tests.ps1

Lines changed: 145 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,6 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
7979
$alertObjectParameterSet = $parameterInfo.ParameterSets['AlertObject']
8080
$alertObjectParameterSet.IsMandatory | Should -BeTrue
8181
}
82-
83-
# Note: ShouldProcess test temporarily disabled due to platform-specific detection issues
84-
# It 'Should support ShouldProcess' {
85-
# $commandInfo = Get-Command -Name 'Set-SqlDscAgentAlert'
86-
# $commandInfo.CmdletBinding.SupportsShouldProcess | Should -BeTrue
87-
# }
8882
}
8983

9084
Context 'When validating parameter ranges' {
@@ -115,8 +109,10 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
115109
$script:mockAlert.Severity = 14
116110
$script:mockAlert.MessageId = 0
117111

118-
# Mock alert collection using SMO stub types
112+
# Mock alert collection using SMO stub types with refresh tracking
119113
$script:mockAlertCollection = [Microsoft.SqlServer.Management.Smo.Agent.AlertCollection]::CreateTypeInstance()
114+
$script:refreshCalled = $false
115+
$script:mockAlertCollection | Add-Member -MemberType ScriptMethod -Name 'Refresh' -Value { $script:refreshCalled = $true } -Force
120116

121117
# Mock the JobServer using SMO stub types
122118
$script:mockJobServer = [Microsoft.SqlServer.Management.Smo.Agent.JobServer]::CreateTypeInstance()
@@ -130,6 +126,11 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
130126
Mock -CommandName 'Get-AgentAlertObject' -MockWith { return $script:mockAlert }
131127
}
132128

129+
BeforeEach {
130+
# Reset the refresh tracking before each test
131+
$script:refreshCalled = $false
132+
}
133+
133134
It 'Should update alert severity successfully' {
134135
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
135136

@@ -139,11 +140,47 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
139140
$script:mockAlert.MessageId | Should -Be 0
140141
}
141142

143+
It 'Should set MessageId to 0 when updating Severity to ensure no conflicts' {
144+
# Set up alert with existing MessageId
145+
$script:mockAlert.Severity = 0
146+
$script:mockAlert.MessageId = 12345
147+
148+
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
149+
150+
$script:mockAlert.Severity | Should -Be 16
151+
$script:mockAlert.MessageId | Should -Be 0
152+
}
153+
154+
It 'Should set Severity to 0 when updating MessageId to ensure no conflicts' {
155+
# Set up alert with existing Severity
156+
$script:mockAlert.Severity = 15
157+
$script:mockAlert.MessageId = 0
158+
159+
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -MessageId 50001
160+
161+
$script:mockAlert.MessageId | Should -Be 50001
162+
$script:mockAlert.Severity | Should -Be 0
163+
}
164+
165+
It 'Should call Alter method when changes are made' {
166+
$script:alterCalled = $false
167+
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
168+
169+
# Set initial values different from what we'll set
170+
$script:mockAlert.Severity = 10
171+
$script:mockAlert.MessageId = 0
172+
173+
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
174+
175+
$script:alterCalled | Should -BeTrue
176+
}
177+
142178
Context 'When using PassThru parameter' {
143179
It 'Should update alert message ID successfully' {
144180
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -MessageId 50001
145181

146182
$script:mockAlert.MessageId | Should -Be 50001
183+
$script:mockAlert.Severity | Should -Be 0 # Should be set to 0 to avoid conflicts
147184
}
148185

149186
It 'Should return alert object' {
@@ -162,11 +199,42 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
162199
}
163200

164201
Context 'When using PassThru parameter' {
165-
It 'Should refresh server object when Refresh is specified' {
166-
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16 -Refresh
202+
It 'Should not refresh server object when Refresh is not specified' {
203+
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
204+
205+
$script:refreshCalled | Should -BeFalse
206+
}
207+
}
208+
209+
Context 'When testing change detection logic' {
210+
It 'Should detect changes when Severity is different' {
211+
$script:alterCalled = $false
212+
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
213+
214+
# Set up alert with different severity
215+
$script:mockAlert.Severity = 10
216+
$script:mockAlert.MessageId = 0
217+
218+
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 16
167219

168-
# Verify that Refresh was called on the Alerts collection
169-
# This would need to be mocked more specifically to verify the call
220+
$script:alterCalled | Should -BeTrue
221+
$script:mockAlert.Severity | Should -Be 16
222+
$script:mockAlert.MessageId | Should -Be 0 # Should be reset to 0
223+
}
224+
225+
It 'Should detect changes when MessageId is different' {
226+
$script:alterCalled = $false
227+
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
228+
229+
# Set up alert with different message ID
230+
$script:mockAlert.Severity = 0
231+
$script:mockAlert.MessageId = 12345
232+
233+
$null = Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -MessageId 50001
234+
235+
$script:alterCalled | Should -BeTrue
236+
$script:mockAlert.MessageId | Should -Be 50001
237+
$script:mockAlert.Severity | Should -Be 0 # Should be reset to 0
170238
}
171239
}
172240
}
@@ -189,6 +257,41 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
189257
$script:mockAlert.Severity | Should -Be 16
190258
$script:mockAlert.MessageId | Should -Be 0
191259
}
260+
261+
It 'Should set MessageId to 0 when updating Severity using AlertObject' {
262+
# Set up alert with existing MessageId
263+
$script:mockAlert.Severity = 10
264+
$script:mockAlert.MessageId = 12345
265+
266+
$null = Set-SqlDscAgentAlert -AlertObject $script:mockAlert -Severity 16
267+
268+
$script:mockAlert.Severity | Should -Be 16
269+
$script:mockAlert.MessageId | Should -Be 0
270+
}
271+
272+
It 'Should set Severity to 0 when updating MessageId using AlertObject' {
273+
# Set up alert with existing Severity
274+
$script:mockAlert.Severity = 15
275+
$script:mockAlert.MessageId = 0
276+
277+
$null = Set-SqlDscAgentAlert -AlertObject $script:mockAlert -MessageId 50001
278+
279+
$script:mockAlert.MessageId | Should -Be 50001
280+
$script:mockAlert.Severity | Should -Be 0
281+
}
282+
283+
It 'Should call Alter method when changes are made using AlertObject' {
284+
$script:alterCalled = $false
285+
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
286+
287+
# Set initial values different from what we'll set
288+
$script:mockAlert.Severity = 10
289+
$script:mockAlert.MessageId = 0
290+
291+
$null = Set-SqlDscAgentAlert -AlertObject $script:mockAlert -Severity 16
292+
293+
$script:alterCalled | Should -BeTrue
294+
}
192295
}
193296

194297
Context 'When alert does not exist' {
@@ -223,10 +326,40 @@ Describe 'Set-SqlDscAgentAlert' -Tag 'Public' {
223326
Mock -CommandName 'Get-AgentAlertObject' -ModuleName $script:dscModuleName -MockWith { return $script:mockAlert }
224327
}
225328

226-
It 'Should not call Alter when properties are unchanged' {
329+
It 'Should not call Alter when Severity is already correct' {
227330
$script:alterCalled = $false
228331
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
229332

333+
# Set up alert with the same severity we're going to set
334+
$script:mockAlert.Severity = 14
335+
$script:mockAlert.MessageId = 0
336+
337+
Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 14
338+
339+
$script:alterCalled | Should -BeFalse
340+
}
341+
342+
It 'Should not call Alter when MessageId is already correct' {
343+
$script:alterCalled = $false
344+
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
345+
346+
# Set up alert with the same message ID we're going to set
347+
$script:mockAlert.Severity = 0
348+
$script:mockAlert.MessageId = 50001
349+
350+
Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -MessageId 50001
351+
352+
$script:alterCalled | Should -BeFalse
353+
}
354+
355+
It 'Should not call Alter when both Severity and MessageId parameters are provided but values are unchanged' {
356+
$script:alterCalled = $false
357+
$script:mockAlert | Add-Member -MemberType ScriptMethod -Name 'Alter' -Value { $script:alterCalled = $true } -Force
358+
359+
# Set up alert with same values we're going to set
360+
$script:mockAlert.Severity = 14
361+
$script:mockAlert.MessageId = 0
362+
230363
Set-SqlDscAgentAlert -ServerObject $script:mockServerObject -Name 'TestAlert' -Severity 14 -MessageId 0
231364

232365
$script:alterCalled | Should -BeFalse

0 commit comments

Comments
 (0)