Skip to content

Commit 0877d60

Browse files
committed
Refactor integration tests for Add-SqlDscDataFile and Add-SqlDscFileGroup to use real SMO Database and FileGroup objects; add AfterAll cleanup for database connections.
1 parent f14707a commit 0877d60

File tree

4 files changed

+35
-59
lines changed

4 files changed

+35
-59
lines changed

tests/Integration/Commands/Add-SqlDscDataFile.Integration.Tests.ps1

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,25 @@ BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

2929
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
30+
}
3031

31-
# Import the SMO module to ensure real SMO types are available
32-
Import-SqlDscPreferredModule
32+
Describe 'Add-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
$script:mockInstanceName = 'DSCSQLTEST'
35+
$script:mockComputerName = Get-ComputerName
3336

34-
$script:mockInstanceName = 'DSCSQLTEST'
35-
$script:mockComputerName = Get-ComputerName
37+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
38+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
3639

37-
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
38-
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
40+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
3941

40-
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
42+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
43+
}
4144

42-
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
43-
}
45+
AfterAll {
46+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
47+
}
4448

45-
Describe 'Add-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
4649
Context 'When adding a DataFile to a FileGroup with real SMO types' {
4750
BeforeEach {
4851
# Create real SMO Database and FileGroup objects
@@ -103,25 +106,4 @@ Describe 'Add-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019
103106
$result[1] | Should -Be $dataFile2
104107
}
105108
}
106-
107-
Context 'When verifying DataFile parent relationship' {
108-
BeforeEach {
109-
$script:testDatabase = [Microsoft.SqlServer.Management.Smo.Database]::new()
110-
$script:testDatabase.Name = 'TestDatabase'
111-
$script:testDatabase.Parent = $script:serverObject
112-
113-
$script:testFileGroup = New-SqlDscFileGroup -Database $script:testDatabase -Name 'TestFileGroup' -Confirm:$false
114-
$script:testDataFile = New-SqlDscDataFile -FileGroup $script:testFileGroup -Name 'TestDataFile' -FileName 'C:\Data\TestDataFile.ndf' -Confirm:$false
115-
}
116-
117-
It 'Should update DataFile parent reference when added to FileGroup' {
118-
$script:testDataFile.Parent | Should -BeNullOrEmpty
119-
120-
Add-SqlDscDataFile -FileGroup $script:testFileGroup -DataFile $script:testDataFile
121-
122-
# Note: The parent may or may not be updated depending on SMO implementation
123-
# This test verifies the DataFile is in the collection
124-
$script:testFileGroup.Files[$script:testDataFile.Name] | Should -Be $script:testDataFile
125-
}
126-
}
127109
}

tests/Integration/Commands/Add-SqlDscFileGroup.Integration.Tests.ps1

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,25 @@ BeforeAll {
2727
$script:moduleName = 'SqlServerDsc'
2828

2929
Import-Module -Name $script:moduleName -Force -ErrorAction 'Stop'
30+
}
3031

31-
# Import the SMO module to ensure real SMO types are available
32-
Import-SqlDscPreferredModule
32+
Describe 'Add-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
33+
BeforeAll {
34+
$script:mockInstanceName = 'DSCSQLTEST'
35+
$script:mockComputerName = Get-ComputerName
3336

34-
$script:mockInstanceName = 'DSCSQLTEST'
35-
$script:mockComputerName = Get-ComputerName
37+
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
38+
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
3639

37-
$mockSqlAdministratorUserName = 'SqlAdmin' # Using computer name as NetBIOS name throw exception.
38-
$mockSqlAdministratorPassword = ConvertTo-SecureString -String 'P@ssw0rd1' -AsPlainText -Force
40+
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
3941

40-
$script:mockSqlAdminCredential = [System.Management.Automation.PSCredential]::new($mockSqlAdministratorUserName, $mockSqlAdministratorPassword)
42+
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
43+
}
4144

42-
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
43-
}
45+
AfterAll {
46+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
47+
}
4448

45-
Describe 'Add-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
4649
Context 'When adding a FileGroup to a Database with real SMO types' {
4750
BeforeEach {
4851
# Create real SMO Database object

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BeforeAll {
3333
}
3434

3535
Describe 'New-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019', 'Integration_SQL2022') {
36-
BeforeAll {
36+
BeforeAll {
3737
$script:mockInstanceName = 'DSCSQLTEST'
3838
$script:mockComputerName = Get-ComputerName
3939

@@ -45,6 +45,10 @@ Describe 'New-SqlDscDataFile' -Tag @('Integration_SQL2017', 'Integration_SQL2019
4545
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
4646
}
4747

48+
AfterAll {
49+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
50+
}
51+
4852
Context 'When creating a DataFile with a FileGroup object' {
4953
BeforeAll {
5054
# Create a real SMO Database object

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ Describe 'New-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL201
4545
$script:serverObject = Connect-SqlDscDatabaseEngine -InstanceName $script:mockInstanceName -Credential $script:mockSqlAdminCredential -ErrorAction 'Stop'
4646
}
4747

48+
AfterAll {
49+
Disconnect-SqlDscDatabaseEngine -ServerObject $script:serverObject
50+
}
51+
4852
Context 'When creating a standalone FileGroup with real SMO types' {
4953
It 'Should create a standalone FileGroup successfully' {
5054
$result = New-SqlDscFileGroup -Name 'TestFileGroup'
@@ -106,21 +110,4 @@ Describe 'New-SqlDscFileGroup' -Tag @('Integration_SQL2017', 'Integration_SQL201
106110
$result | Should -BeNullOrEmpty
107111
}
108112
}
109-
110-
Context 'When verifying FileGroup properties' {
111-
BeforeAll {
112-
# Create a real SMO Database object
113-
$script:propertyTestDatabase = [Microsoft.SqlServer.Management.Smo.Database]::new()
114-
$script:propertyTestDatabase.Name = 'PropertyTestDatabase'
115-
$script:propertyTestDatabase.Parent = $script:serverObject
116-
}
117-
118-
It 'Should have Files collection initialized when attached to a Database' {
119-
$result = New-SqlDscFileGroup -Database $script:propertyTestDatabase -Name 'TestFileGroup' -Confirm:$false
120-
121-
$result.Files | Should -Not -BeNullOrEmpty
122-
$result.Files | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.DataFileCollection'
123-
$result.Files.Count | Should -Be 0
124-
}
125-
}
126113
}

0 commit comments

Comments
 (0)