Skip to content

Commit 2b13c4e

Browse files
committed
Add integration tests for creating a database with custom file groups and data files
1 parent d410342 commit 2b13c4e

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,66 @@ Describe 'New-SqlDscDatabase' -Tag @('Integration_SQL2017', 'Integration_SQL2019
150150
Should -Throw
151151
}
152152
}
153+
154+
Context 'When creating a database with file groups' {
155+
BeforeAll {
156+
$script:testDatabaseWithFileGroups = 'SqlDscTestDbFileGroups_' + (Get-Random)
157+
158+
# Get the default data directory from the server
159+
$script:dataDirectory = $script:serverObject.Settings.DefaultFile
160+
161+
if (-not $script:dataDirectory)
162+
{
163+
$script:dataDirectory = $script:serverObject.Information.MasterDBPath
164+
}
165+
166+
# Ensure the directory exists
167+
if (-not (Test-Path -Path $script:dataDirectory))
168+
{
169+
$null = New-Item -Path $script:dataDirectory -ItemType Directory -Force
170+
}
171+
}
172+
173+
AfterAll {
174+
# Clean up test database
175+
$dbToRemove = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseWithFileGroups -ErrorAction 'SilentlyContinue'
176+
if ($dbToRemove)
177+
{
178+
$null = Remove-SqlDscDatabase -DatabaseObject $dbToRemove -Force -ErrorAction 'Stop'
179+
}
180+
}
181+
182+
It 'Should create a database with custom file groups and data files' {
183+
# Create PRIMARY filegroup with data file
184+
$primaryFileGroup = New-SqlDscFileGroup -Name 'PRIMARY'
185+
$primaryFilePath = Join-Path -Path $script:dataDirectory -ChildPath ($script:testDatabaseWithFileGroups + '_Primary.mdf')
186+
$null = New-SqlDscDataFile -FileGroup $primaryFileGroup -Name ($script:testDatabaseWithFileGroups + '_Primary') -FileName $primaryFilePath -Force
187+
188+
# Create a secondary filegroup with data file
189+
$secondaryFileGroup = New-SqlDscFileGroup -Name 'SecondaryFG'
190+
$secondaryFilePath = Join-Path -Path $script:dataDirectory -ChildPath ($script:testDatabaseWithFileGroups + '_Secondary.ndf')
191+
$null = New-SqlDscDataFile -FileGroup $secondaryFileGroup -Name ($script:testDatabaseWithFileGroups + '_Secondary') -FileName $secondaryFilePath -Force
192+
193+
# Create database with file groups
194+
$result = New-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseWithFileGroups -FileGroup @($primaryFileGroup, $secondaryFileGroup) -Force -ErrorAction 'Stop'
195+
196+
$result | Should -Not -BeNullOrEmpty
197+
$result.Name | Should -Be $script:testDatabaseWithFileGroups
198+
$result | Should -BeOfType 'Microsoft.SqlServer.Management.Smo.Database'
199+
200+
# Verify the database exists with correct file groups
201+
$createdDb = Get-SqlDscDatabase -ServerObject $script:serverObject -Name $script:testDatabaseWithFileGroups -Refresh -ErrorAction 'Stop'
202+
$createdDb | Should -Not -BeNullOrEmpty
203+
204+
# Verify PRIMARY filegroup exists
205+
$createdDb.FileGroups['PRIMARY'] | Should -Not -BeNullOrEmpty
206+
$createdDb.FileGroups['PRIMARY'].Files.Count | Should -Be 1
207+
$createdDb.FileGroups['PRIMARY'].Files[0].Name | Should -Be ($script:testDatabaseWithFileGroups + '_Primary')
208+
209+
# Verify secondary filegroup exists
210+
$createdDb.FileGroups['SecondaryFG'] | Should -Not -BeNullOrEmpty
211+
$createdDb.FileGroups['SecondaryFG'].Files.Count | Should -Be 1
212+
$createdDb.FileGroups['SecondaryFG'].Files[0].Name | Should -Be ($script:testDatabaseWithFileGroups + '_Secondary')
213+
}
214+
}
153215
}

0 commit comments

Comments
 (0)