Skip to content

Commit 70f4f9f

Browse files
authored
Improve integration and unit tests cleanup (#2293)
1 parent 8acbed1 commit 70f4f9f

10 files changed

+227
-219
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
9999

100100
### Changed
101101

102+
- Refactored integration tests to remove `finally` blocks from `It`-blocks and
103+
use Pester `BeforeEach`/`AfterEach` blocks instead, following DSC Community
104+
coding guidelines. This improves test cleanup reliability and maintainability
105+
across the following test files ([issue #2288](https://github.com/dsccommunity/SqlServerDsc/issues/2288)):
106+
- `New-SqlDscLogin.Integration.Tests.ps1`
107+
- `New-SqlDscAudit.Integration.Tests.ps1`
108+
- `Get-SqlDscDatabasePermission.Integration.Tests.ps1`
109+
- `Get-SqlDscPreferredModule.Integration.Tests.ps1`
110+
- `New-SqlDscDatabase.Integration.Tests.ps1`
111+
- Refactored unit tests to remove `finally` blocks from `It`-blocks and
112+
use Pester `AfterEach` blocks instead, following DSC Community coding
113+
guidelines. This improves test cleanup reliability and maintainability
114+
across the following test files ([issue #2288](https://github.com/dsccommunity/SqlServerDsc/issues/2288)):
115+
- `Set-SqlDscConfigurationOption.Tests.ps1`
116+
- `Get-SqlDscConfigurationOption.Tests.ps1`
117+
- `Test-SqlDscConfigurationOption.Tests.ps1`
102118
- `Test-SqlDscIsDatabasePrincipal` and `Get-SqlDscDatabasePermission`
103119
- Added `Refresh` parameter to refresh SMO collections before checking
104120
database principals, addressing issues with custom database roles created

tests/Integration/Commands/Get-SqlDscDatabasePermission.Integration.Tests.ps1

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,19 @@ Describe 'Get-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integrati
163163
}
164164

165165
Context 'When working with built-in database roles' {
166+
BeforeEach {
167+
$script:customRoleName = $null
168+
}
169+
170+
AfterEach {
171+
# Clean up the custom role if it was created
172+
if ($script:customRoleName)
173+
{
174+
$dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$script:customRoleName];"
175+
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue'
176+
}
177+
}
178+
166179
It 'Should return permissions for db_datareader role' {
167180
# Note: The command excludes fixed roles by default, so this should return null or empty
168181
$result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name 'db_datareader' -ErrorAction 'SilentlyContinue'
@@ -173,33 +186,24 @@ Describe 'Get-SqlDscDatabasePermission' -Tag @('Integration_SQL2017', 'Integrati
173186

174187
It 'Should work with non-fixed database roles when they exist' {
175188
# Create a custom database role for testing
176-
$customRoleName = 'TestRole_' + (Get-Random)
177-
$createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$customRoleName];"
189+
$script:customRoleName = 'TestRole_' + (Get-Random)
190+
$createRoleSql = "USE [$($script:testDatabaseName)]; CREATE ROLE [$script:customRoleName];"
178191
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $createRoleSql -Force -ErrorAction 'Stop'
179192

180-
try
181-
{
182-
# Grant a permission to the custom role
183-
$grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$customRoleName];"
184-
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop'
193+
# Grant a permission to the custom role
194+
$grantRolePermissionSql = "USE [$($script:testDatabaseName)]; GRANT CONNECT TO [$script:customRoleName];"
195+
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $grantRolePermissionSql -Force -ErrorAction 'Stop'
185196

186-
# Test getting permissions for the custom role
187-
$result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $customRoleName -Refresh
197+
# Test getting permissions for the custom role
198+
$result = Get-SqlDscDatabasePermission -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Name $script:customRoleName -Refresh
188199

189-
$result | Should -Not -BeNullOrEmpty
190-
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo]
200+
$result | Should -Not -BeNullOrEmpty
201+
$result | Should -BeOfType [Microsoft.SqlServer.Management.Smo.DatabasePermissionInfo]
191202

192-
# Verify the Connect permission we granted is present
193-
$connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true }
194-
$connectPermission | Should -Not -BeNullOrEmpty
195-
$connectPermission.PermissionState | Should -Be 'Grant'
196-
}
197-
finally
198-
{
199-
# Clean up the custom role
200-
$dropRoleSql = "USE [$($script:testDatabaseName)]; DROP ROLE [$customRoleName];"
201-
Invoke-SqlDscQuery -ServerObject $script:serverObject -DatabaseName $script:testDatabaseName -Query $dropRoleSql -Force -ErrorAction 'SilentlyContinue'
202-
}
203+
# Verify the Connect permission we granted is present
204+
$connectPermission = $result | Where-Object { $_.PermissionType.Connect -eq $true }
205+
$connectPermission | Should -Not -BeNullOrEmpty
206+
$connectPermission.PermissionState | Should -Be 'Grant'
203207
}
204208
}
205209
}

tests/Integration/Commands/Get-SqlDscPreferredModule.Integration.Tests.ps1

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,25 +210,28 @@ Describe 'Get-SqlDscPreferredModule' -Tag @('Integration_SQL2017', 'Integration_
210210
}
211211
}
212212

213-
It 'Should throw an error when specified version does not exist' {
214-
# Backup original environment variable
215-
$originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion
216-
217-
try {
218-
# Set to a non-existent version
219-
$env:SMODefaultModuleVersion = '999.999.999'
220-
221-
{ Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule'
213+
Context 'When testing error handling' {
214+
BeforeEach {
215+
# Capture the current environment variable value
216+
$script:originalSMODefaultModuleVersion = $env:SMODefaultModuleVersion
222217
}
223-
finally {
224-
# Restore original environment variable
225-
if ($originalSMODefaultModuleVersion) {
226-
$env:SMODefaultModuleVersion = $originalSMODefaultModuleVersion
218+
219+
AfterEach {
220+
# Restore the environment variable
221+
if ($script:originalSMODefaultModuleVersion) {
222+
$env:SMODefaultModuleVersion = $script:originalSMODefaultModuleVersion
227223
}
228224
else {
229225
Remove-Item -Path 'env:SMODefaultModuleVersion' -ErrorAction 'SilentlyContinue'
230226
}
231227
}
228+
229+
It 'Should throw an error when specified version does not exist' {
230+
# Set to a non-existent version
231+
$env:SMODefaultModuleVersion = '999.999.999'
232+
233+
{ Get-SqlDscPreferredModule -ErrorAction 'Stop' } | Should -Throw -ErrorId 'GSDPM0001,Get-SqlDscPreferredModule'
234+
}
232235
}
233236
}
234237
}

tests/Integration/Commands/New-SqlDscAudit.Integration.Tests.ps1

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -105,30 +105,18 @@ Describe 'New-SqlDscAudit' -Tag @('Integration_SQL2017', 'Integration_SQL2019',
105105
}
106106

107107
It 'Should create a security log audit successfully' {
108-
$securityLogAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random)
108+
$script:testAuditName = 'SqlDscTestAudit_SecLog_' + (Get-Random)
109109

110-
try
111-
{
112-
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop
110+
$result = New-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -LogType 'SecurityLog' -PassThru -Force -ErrorAction Stop
113111

114-
$result | Should -Not -BeNullOrEmpty
115-
$result.Name | Should -Be $securityLogAuditName
116-
$result.DestinationType | Should -Be 'SecurityLog'
112+
$result | Should -Not -BeNullOrEmpty
113+
$result.Name | Should -Be $script:testAuditName
114+
$result.DestinationType | Should -Be 'SecurityLog'
117115

118-
# Verify the audit exists in the server
119-
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction Stop
120-
$createdAudit | Should -Not -BeNullOrEmpty
121-
$createdAudit.DestinationType | Should -Be 'SecurityLog'
122-
}
123-
finally
124-
{
125-
# Clean up
126-
$auditToRemove = Get-SqlDscAudit -ServerObject $script:serverObject -Name $securityLogAuditName -ErrorAction 'SilentlyContinue'
127-
if ($auditToRemove)
128-
{
129-
Remove-SqlDscAudit -AuditObject $auditToRemove -Force -ErrorAction 'SilentlyContinue'
130-
}
131-
}
116+
# Verify the audit exists in the server
117+
$createdAudit = Get-SqlDscAudit -ServerObject $script:serverObject -Name $script:testAuditName -ErrorAction Stop
118+
$createdAudit | Should -Not -BeNullOrEmpty
119+
$createdAudit.DestinationType | Should -Be 'SecurityLog'
132120
}
133121

134122
It 'Should support PassThru parameter' {

tests/Integration/Commands/New-SqlDscDatabase.Integration.Tests.ps1

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,29 @@ Describe 'New-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019
9696
}
9797

9898
Context 'When using the Refresh parameter' {
99-
It 'Should refresh the database collection before creating' {
100-
$uniqueName = 'SqlDscTestRefresh_' + (Get-Random)
101-
102-
try
103-
{
104-
$result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $uniqueName -Refresh -Force -ErrorAction Stop
99+
BeforeEach {
100+
$script:refreshTestDbName = $null
101+
}
105102

106-
$result | Should -Not -BeNullOrEmpty
107-
$result.Name | Should -Be $uniqueName
108-
}
109-
finally
103+
AfterEach {
104+
# Clean up the refresh test database if it was created
105+
if ($script:refreshTestDbName)
110106
{
111-
# Clean up
112-
$dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $uniqueName -ErrorAction 'SilentlyContinue'
107+
$dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestDbName -ErrorAction 'SilentlyContinue'
113108
if ($dbToRemove)
114109
{
115110
$null = Remove-SqlDscDatabase -DatabaseObject $dbToRemove -Force
116111
}
117112
}
118113
}
114+
115+
It 'Should refresh the database collection before creating' {
116+
$script:refreshTestDbName = 'SqlDscTestRefresh_' + (Get-Random)
117+
118+
$result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:refreshTestDbName -Refresh -Force -ErrorAction Stop
119+
120+
$result | Should -Not -BeNullOrEmpty
121+
$result.Name | Should -Be $script:refreshTestDbName
122+
}
119123
}
120124
}

0 commit comments

Comments
 (0)