Skip to content

Commit 3f0e056

Browse files
committed
Add error handling for non-existent database properties in Test-SqlDscDatabaseProperty tests
1 parent 95488cf commit 3f0e056

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

source/en-US/SqlServerDsc.strings.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ ConvertFrom-StringData @'
403403
DatabaseProperty_TestingPropertiesFromObject = Testing properties of database '{0}' on instance '{1}' using database object. (TSDDP0002)
404404
DatabaseProperty_PropertyWrong = The database '{0}' property '{1}' has the value '{2}', but expected it to have the value '{3}'. (TSDDP0003)
405405
DatabaseProperty_PropertyCorrect = The database '{0}' property '{1}' has the expected value '{2}'. (TSDDP0004)
406+
DatabaseProperty_PropertyNotFound = The property '{0}' does not exist on database '{1}'. This might be due to the property not being supported on this SQL Server version. (TSDDP0005)
406407
407408
## Set-SqlDscDatabaseDefault
408409
DatabaseDefault_Set = Setting default objects of database '{0}' on instance '{1}'. (SSDDD0001)

tests/Unit/Public/Test-SqlDscDatabaseProperty.Tests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,48 @@ Describe 'Test-SqlDscDatabaseProperty' -Tag 'Public' {
409409
}
410410
}
411411

412+
Context 'When testing a property that does not exist on the database object' {
413+
BeforeAll {
414+
$mockServerObject = New-Object -TypeName 'Microsoft.SqlServer.Management.Smo.Server' |
415+
Add-Member -MemberType 'NoteProperty' -Name 'InstanceName' -Value 'TestInstance' -PassThru -Force
416+
417+
# Mock Get-SqlDscDatabase to return our custom minimal database object
418+
Mock -CommandName Get-SqlDscDatabase -MockWith {
419+
<#
420+
Create a minimal database object without certain properties using PSCustomObject
421+
This allows us to control exactly which properties exist, simulating older SQL Server versions
422+
#>
423+
return [PSCustomObject] @{
424+
Name = 'MinimalDatabase'
425+
Collation = 'SQL_Latin1_General_CP1_CI_AS'
426+
# Intentionally not including AcceleratedRecoveryEnabled to simulate SQL Server version that doesn't support it
427+
}
428+
}
429+
}
430+
431+
It 'Should throw an exception when property does not exist on the database object' {
432+
# AcceleratedRecoveryEnabled is not added to the minimal database object
433+
{ Test-SqlDscDatabaseProperty -ServerObject $mockServerObject -Name 'MinimalDatabase' -AcceleratedRecoveryEnabled $true -ErrorAction 'Stop' } |
434+
Should -Throw -ExpectedMessage "The property 'AcceleratedRecoveryEnabled' does not exist on database 'MinimalDatabase'. This might be due to the property not being supported on this SQL Server version.*"
435+
}
436+
437+
It 'Should continue testing other properties after encountering a missing property' {
438+
# Test with both a missing property and an existing property
439+
$result = Test-SqlDscDatabaseProperty -ServerObject $mockServerObject -Name 'MinimalDatabase' -AcceleratedRecoveryEnabled $true -Collation 'SQL_Latin1_General_CP1_CI_AS' -ErrorAction 'SilentlyContinue'
440+
441+
# Should return true because the existing property (Collation) matches
442+
$result | Should -BeTrue
443+
}
444+
445+
It 'Should return false when a missing property is tested along with a non-matching property' {
446+
# Test with a missing property and a non-matching existing property
447+
$result = Test-SqlDscDatabaseProperty -ServerObject $mockServerObject -Name 'MinimalDatabase' -AcceleratedRecoveryEnabled $true -Collation 'Different_Collation' -ErrorAction 'SilentlyContinue'
448+
449+
# Should return false because the existing property (Collation) does not match
450+
$result | Should -BeFalse
451+
}
452+
}
453+
412454
Context 'Parameter validation' {
413455
It 'Should have the correct parameters in parameter set <ExpectedParameterSetName>' -ForEach @(
414456
@{

0 commit comments

Comments
 (0)