Skip to content

Commit b990bb0

Browse files
authored
SqlAgentAlert: Add tests (#2153)
1 parent 53a4a65 commit b990bb0

File tree

3 files changed

+173
-57
lines changed

3 files changed

+173
-57
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- Fixed Azure DevOps pipeline conditions that were preventing DSC resource
1313
integration tests from running when they should by removing incorrect quotes
1414
around boolean values.
15+
- `SqlAgentAlert`
16+
- Minor fix in `source/Classes/020.SqlAgentAlert.ps1` to correct `ExcludeDscProperties`
17+
formatting (added missing delimiter).
1518

1619
### Added
1720

@@ -57,6 +60,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5760
- Refactored GitHub Copilot workflow setup to be module-agnostic via MODULE_NAME
5861
environment variable, includes full-history detection, uses idempotent .NET
5962
tool install, and adds Linux dependency handling ([issue #2127](https://github.com/dsccommunity/SqlServerDsc/issues/2127)).
63+
- `SqlAgentAlert`
64+
- Added additional unit tests covering MessageId-based alerts, the hidden
65+
`Modify()` method behavior, and `AssertProperties()` validation scenarios.
6066
- Module now outputs a verbose message instead of a warning when the SMO
6167
dependency module is missing during import to work around a DSC v3 issue.
6268
- VS Code tasks configuration was improved to support AI.

source/Classes/020.SqlAgentAlert.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class SqlAgentAlert : SqlResourceBase
144144
$this.ExcludeDscProperties = @(
145145
'InstanceName',
146146
'ServerName',
147-
'Credential'
147+
'Credential',
148148
'Name'
149149
)
150150
}

tests/Unit/Classes/SqlAgentAlert.Tests.ps1

Lines changed: 166 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,17 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
124124
}
125125

126126
Context 'When using the Get() method' {
127-
BeforeAll {
128-
InModuleScope -ScriptBlock {
129-
$script:mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server'
127+
It 'Should return current state when alert exists' {
128+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
130129
$script:mockAlertObject = New-Object -TypeName 'PSCustomObject'
131130
$script:mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
132131
$script:mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 16
133-
# MessageId is not set when using Severity-based alerts
132+
133+
return $script:mockAlertObject
134134
}
135-
}
136135

137-
It 'Should return current state when alert exists' {
138136
InModuleScope -ScriptBlock {
139-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
140-
return $script:mockAlertObject
141-
}
137+
$script:mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server'
142138

143139
$instance = [SqlAgentAlert] @{
144140
Name = 'TestAlert'
@@ -159,10 +155,12 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
159155
}
160156

161157
It 'Should return absent state when alert does not exist' {
158+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
159+
return $null
160+
}
161+
162162
InModuleScope -ScriptBlock {
163-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
164-
return $null
165-
}
163+
$script:mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server'
166164

167165
$instance = [SqlAgentAlert] @{
168166
Name = 'TestAlert'
@@ -180,6 +178,38 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
180178
$result.Ensure | Should -Be 'Absent'
181179
}
182180
}
181+
182+
It 'Should return current state when alert exists with MessageId' {
183+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
184+
$script:mockAlertObjectWithMessageId = New-Object -TypeName 'PSCustomObject'
185+
$script:mockAlertObjectWithMessageId | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
186+
$script:mockAlertObjectWithMessageId | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 0
187+
$script:mockAlertObjectWithMessageId | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 50001
188+
189+
return $script:mockAlertObjectWithMessageId
190+
}
191+
192+
InModuleScope -ScriptBlock {
193+
$script:mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server'
194+
195+
$instance = [SqlAgentAlert] @{
196+
Name = 'TestAlert'
197+
InstanceName = 'MSSQLSERVER'
198+
MessageId = 50002
199+
} |
200+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
201+
return $script:mockServerObject
202+
} -PassThru
203+
204+
$result = $instance.Get()
205+
206+
$result | Should -Not -BeNullOrEmpty
207+
$result.Name | Should -Be 'TestAlert'
208+
$result.Ensure | Should -Be 'Present'
209+
$result.MessageId | Should -Be 50001
210+
$result.Severity | Should -BeNullOrEmpty
211+
}
212+
}
183213
}
184214

185215
Context 'When using the Test() method' {
@@ -190,16 +220,16 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
190220
}
191221

192222
It 'Should return true when alert exists and is in desired state' {
223+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
224+
return $script:mockAlertObject
225+
}
226+
193227
InModuleScope -ScriptBlock {
194228
$script:mockAlertObject = New-Object -TypeName 'PSCustomObject'
195229
$script:mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
196230
$script:mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 16
197231
$script:mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 0
198232

199-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
200-
return $script:mockAlertObject
201-
}
202-
203233
$instance = [SqlAgentAlert] @{
204234
Name = 'TestAlert'
205235
InstanceName = 'MSSQLSERVER'
@@ -217,11 +247,9 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
217247
}
218248

219249
It 'Should return false when alert does not exist but should be present' {
220-
InModuleScope -ScriptBlock {
221-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
222-
return $null
223-
}
250+
Mock -CommandName 'Get-SqlDscAgentAlert'
224251

252+
InModuleScope -ScriptBlock {
225253
$instance = [SqlAgentAlert] @{
226254
Name = 'TestAlert'
227255
InstanceName = 'MSSQLSERVER'
@@ -269,6 +297,27 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
269297
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
270298
}
271299
}
300+
301+
It 'Should create alert with MessageId' {
302+
InModuleScope -ScriptBlock {
303+
$instance = [SqlAgentAlert] @{
304+
Name = 'TestAlert'
305+
InstanceName = 'MSSQLSERVER'
306+
Ensure = 'Present'
307+
MessageId = 50001
308+
} |
309+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
310+
return $script:mockServerObject
311+
} -PassThru
312+
313+
$null = $instance.Set()
314+
315+
Should -Invoke -CommandName 'New-SqlDscAgentAlert' -ParameterFilter {
316+
$MessageId -eq 50001
317+
} -Exactly -Times 1 -Scope It
318+
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
319+
}
320+
}
272321
}
273322

274323
Context 'When it exists and Ensure is Absent' {
@@ -320,18 +369,79 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
320369
Mock -CommandName 'Set-SqlDscAgentAlert'
321370
}
322371

323-
Context 'When Ensure is Present and alert exists' {
324-
It 'Should update alert when Severity property differs' {
372+
373+
Context 'When Ensure is Present and alert does not exist' {
374+
BeforeAll {
375+
Mock -CommandName 'Get-SqlDscAgentAlert'
376+
}
377+
378+
It 'Should create alert with Severity' {
379+
InModuleScope -ScriptBlock {
380+
$instance = [SqlAgentAlert] @{
381+
Name = 'TestAlert'
382+
InstanceName = 'MSSQLSERVER'
383+
Ensure = 'Present'
384+
Severity = 16
385+
} |
386+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
387+
return $script:mockServerObject
388+
} -PassThru
389+
390+
$properties = @{
391+
Severity = 16
392+
}
393+
394+
$null = $instance.Modify($properties)
395+
396+
Should -Invoke -CommandName 'Get-SqlDscAgentAlert' -Exactly -Times 1 -Scope It
397+
Should -Invoke -CommandName 'New-SqlDscAgentAlert' -ParameterFilter {
398+
$Severity -eq 16
399+
} -Exactly -Times 1 -Scope It
400+
Should -Invoke -CommandName 'Set-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
401+
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
402+
}
403+
}
404+
405+
It 'Should create alert with MessageId' {
325406
InModuleScope -ScriptBlock {
326-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
327-
$mockAlertObject = New-Object -TypeName 'PSCustomObject'
328-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
329-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 10
330-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 0
407+
$instance = [SqlAgentAlert] @{
408+
Name = 'TestAlert'
409+
InstanceName = 'MSSQLSERVER'
410+
Ensure = 'Present'
411+
MessageId = 50001
412+
} |
413+
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetServerObject' -Value {
414+
return $script:mockServerObject
415+
} -PassThru
331416

332-
return $mockAlertObject
417+
$properties = @{
418+
MessageId = 50001
333419
}
334420

421+
$null = $instance.Modify($properties)
422+
423+
Should -Invoke -CommandName 'Get-SqlDscAgentAlert' -Exactly -Times 1 -Scope It
424+
Should -Invoke -CommandName 'New-SqlDscAgentAlert' -ParameterFilter {
425+
$MessageId -eq 50001
426+
} -Exactly -Times 1 -Scope It
427+
Should -Invoke -CommandName 'Set-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
428+
Should -Invoke -CommandName 'Remove-SqlDscAgentAlert' -Exactly -Times 0 -Scope It
429+
}
430+
}
431+
}
432+
433+
Context 'When Ensure is Present and alert exists' {
434+
It 'Should update alert when Severity property differs' {
435+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
436+
$mockAlertObject = New-Object -TypeName 'PSCustomObject'
437+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
438+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 10
439+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 0
440+
441+
return $mockAlertObject
442+
}
443+
444+
InModuleScope -ScriptBlock {
335445
$instance = [SqlAgentAlert] @{
336446
Name = 'TestAlert'
337447
InstanceName = 'MSSQLSERVER'
@@ -359,16 +469,16 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
359469
}
360470

361471
It 'Should update alert when MessageId property differs' {
362-
InModuleScope -ScriptBlock {
363-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
364-
$mockAlertObject = New-Object -TypeName 'PSCustomObject'
365-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
366-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 0
367-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 50001
472+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
473+
$mockAlertObject = New-Object -TypeName 'PSCustomObject'
474+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
475+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 0
476+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 50001
368477

369-
return $mockAlertObject
370-
}
478+
return $mockAlertObject
479+
}
371480

481+
InModuleScope -ScriptBlock {
372482
$instance = [SqlAgentAlert] @{
373483
Name = 'TestAlert'
374484
InstanceName = 'MSSQLSERVER'
@@ -396,16 +506,16 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
396506
}
397507

398508
It 'Should not update alert when no properties differ' {
399-
InModuleScope -ScriptBlock {
400-
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
401-
$mockAlertObject = New-Object -TypeName 'PSCustomObject'
402-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
403-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 16
404-
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 0
509+
Mock -CommandName 'Get-SqlDscAgentAlert' -MockWith {
510+
$mockAlertObject = New-Object -TypeName 'PSCustomObject'
511+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Name' -Value 'TestAlert'
512+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'Severity' -Value 16
513+
$mockAlertObject | Add-Member -MemberType 'NoteProperty' -Name 'MessageId' -Value 0
405514

406-
return $mockAlertObject
407-
}
515+
return $mockAlertObject
516+
}
408517

518+
InModuleScope -ScriptBlock {
409519
$instance = [SqlAgentAlert] @{
410520
Name = 'TestAlert'
411521
InstanceName = 'MSSQLSERVER'
@@ -568,10 +678,12 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
568678
}
569679

570680
Context 'When Ensure is Present' {
681+
BeforeAll {
682+
Mock -CommandName 'Assert-BoundParameter'
683+
}
684+
571685
It 'Should call Assert-BoundParameter to validate at least one of Severity or MessageId is specified' {
572686
InModuleScope -ScriptBlock {
573-
Mock -CommandName 'Assert-BoundParameter' -MockWith { }
574-
575687
$properties = @{
576688
Name = 'TestAlert'
577689
Ensure = 'Present'
@@ -591,8 +703,6 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
591703

592704
It 'Should call Assert-BoundParameter to validate Severity and MessageId are mutually exclusive' {
593705
InModuleScope -ScriptBlock {
594-
Mock -CommandName 'Assert-BoundParameter' -MockWith { }
595-
596706
$properties = @{
597707
Name = 'TestAlert'
598708
Ensure = 'Present'
@@ -613,9 +723,9 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
613723

614724
Context 'When Ensure is Absent' {
615725
It 'Should call Assert-BoundParameter to validate Severity and MessageId are not allowed' {
616-
InModuleScope -ScriptBlock {
617-
Mock -CommandName 'Assert-BoundParameter' -MockWith { }
726+
Mock -CommandName 'Assert-BoundParameter'
618727

728+
InModuleScope -ScriptBlock {
619729
$properties = @{
620730
Name = 'TestAlert'
621731
Ensure = 'Absent'
@@ -633,14 +743,14 @@ Describe 'SqlAgentAlert' -Tag 'SqlAgentAlert' {
633743
}
634744

635745
It 'Should call Assert-BoundParameter with correct parameters when Severity is specified' {
636-
InModuleScope -ScriptBlock {
637-
Mock -CommandName 'Assert-BoundParameter' -MockWith {
638-
# Simulate the Assert-BoundParameter throwing an exception for NotAllowed parameters
639-
if ($NotAllowedList -and ($BoundParameterList.ContainsKey('Severity') -or $BoundParameterList.ContainsKey('MessageId'))) {
640-
throw 'Parameter validation failed'
641-
}
746+
Mock -CommandName 'Assert-BoundParameter' -MockWith {
747+
# Simulate the Assert-BoundParameter throwing an exception for NotAllowed parameters
748+
if ($NotAllowedList -and ($BoundParameterList.ContainsKey('Severity') -or $BoundParameterList.ContainsKey('MessageId'))) {
749+
throw 'Parameter validation failed'
642750
}
751+
}
643752

753+
InModuleScope -ScriptBlock {
644754
$properties = @{
645755
Name = 'TestAlert'
646756
Ensure = 'Absent'

0 commit comments

Comments
 (0)