Skip to content

Refactor and enhance Pester tests batch #9768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions tests/Get-DbaDbServiceBrokerService.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ param(
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
Expand All @@ -31,49 +28,53 @@ Describe $CommandName -Tag UnitTests {

Describe $CommandName -Tag IntegrationTests {
BeforeAll {
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$procname = "dbatools_$(Get-Random)"
$server.Query("CREATE PROCEDURE $procname AS SELECT 1", "tempdb")
$queuename = "dbatools_$(Get-Random)"
$server.Query("CREATE QUEUE $queuename WITH STATUS = ON , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = $procname , MAX_QUEUE_READERS = 1 , EXECUTE AS OWNER ), POISON_MESSAGE_HANDLING (STATUS = ON)", "tempdb")
$servicename = "dbatools_$(Get-Random)"
$server.Query("CREATE SERVICE $servicename ON QUEUE $queuename ([DEFAULT])", "tempdb")
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Set up Service Broker components for testing
$global:testServer = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$global:testProcName = "dbatools_{0}" -f $(Get-Random)
$global:testServer.Query("CREATE PROCEDURE $global:testProcName AS SELECT 1", "tempdb")
$global:testQueueName = "dbatools_{0}" -f $(Get-Random)
$global:testServer.Query("CREATE QUEUE $global:testQueueName WITH STATUS = ON , RETENTION = OFF , ACTIVATION ( STATUS = ON , PROCEDURE_NAME = $global:testProcName , MAX_QUEUE_READERS = 1 , EXECUTE AS OWNER ), POISON_MESSAGE_HANDLING (STATUS = ON)", "tempdb")
$global:testServiceName = "dbatools_{0}" -f $(Get-Random)
$global:testServer.Query("CREATE SERVICE $global:testServiceName ON QUEUE $global:testQueueName ([DEFAULT])", "tempdb")

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}


AfterAll {
try {
$null = $server.Query("DROP SERVICE $servicename", "tempdb")
$null = $server.Query("DROP QUEUE $queuename", "tempdb")
$null = $server.Query("DROP PROCEDURE $procname", "tempdb")
} catch {
# Suppress errors during cleanup
}
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Cleanup Service Broker components
$null = $global:testServer.Query("DROP SERVICE $global:testServiceName", "tempdb")
$null = $global:testServer.Query("DROP QUEUE $global:testQueueName", "tempdb")
$null = $global:testServer.Query("DROP PROCEDURE $global:testProcName", "tempdb")
}

Context "Gets the service broker service" {
BeforeAll {
$splatServiceBroker = @{
SqlInstance = $TestConfig.instance2
Database = "tempdb"
ExcludeSystemService = $true
}
$results = Get-DbaDbServiceBrokerService @splatServiceBroker
$testResults = Get-DbaDbServiceBrokerService -SqlInstance $TestConfig.instance2 -Database tempdb -ExcludeSystemService:$true
}

It "Gets results" {
$results | Should -Not -BeNullOrEmpty
$testResults | Should -Not -BeNullOrEmpty
}

It "Should have a name of $servicename" {
$results.Name | Should -BeExactly $servicename
It "Should have a name of $global:testServiceName" {
$testResults.Name | Should -Be $global:testServiceName
}


It "Should have an owner of dbo" {
$results.Owner | Should -BeExactly "dbo"
$testResults.Owner | Should -Be "dbo"
}

It "Should have a queuename of $queuename" {
$results.QueueName | Should -BeExactly $queuename
It "Should have a queuename of $global:testQueueName" {
$testResults.QueueName | Should -Be $global:testQueueName
}
}
}
4 changes: 2 additions & 2 deletions tests/Get-DbaDbSharePoint.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ Describe $CommandName -Tag IntegrationTests {
# Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
# So we don't run the following line but skip the tests
# . $sqlpackage /Action:Import /tsn:$TestConfig.instance2 /tdn:Sharepoint_Config /sf:$bacpac /p:Storage=File
$skip = $true
$global:skip = $true
} else {
Write-Warning -Message "No bacpac found in path [$bacpac], skipping tests."
$skip = $true
$global:skip = $true
}

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
Expand Down
41 changes: 17 additions & 24 deletions tests/Get-DbaDbSnapshot.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ param(
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
Expand Down Expand Up @@ -35,15 +32,15 @@ Describe $CommandName -Tag IntegrationTests {
Context "Operations on snapshots" {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

Get-DbaProcess -SqlInstance $TestConfig.instance2 | Where-Object Program -match dbatools | Stop-DbaProcess -Confirm:$false -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$db1 = "dbatoolsci_GetSnap"
$db1_snap1 = "dbatoolsci_GetSnap_snapshotted1"
$db1_snap2 = "dbatoolsci_GetSnap_snapshotted2"
$db2 = "dbatoolsci_GetSnap2"
$db2_snap1 = "dbatoolsci_GetSnap2_snapshotted"
$global:server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$global:db1 = "dbatoolsci_GetSnap"
$global:db1_snap1 = "dbatoolsci_GetSnap_snapshotted1"
$global:db1_snap2 = "dbatoolsci_GetSnap_snapshotted2"
$global:db2 = "dbatoolsci_GetSnap2"
$global:db2_snap1 = "dbatoolsci_GetSnap2_snapshotted"
Remove-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Database $db1, $db2 -Confirm:$false
Get-DbaDatabase -SqlInstance $TestConfig.instance2 -Database $db1, $db2 | Remove-DbaDatabase -Confirm:$false
$server.Query("CREATE DATABASE $db1")
Expand All @@ -53,12 +50,12 @@ Describe $CommandName -Tag IntegrationTests {
$null = New-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Database $db2 -Name $db2_snap1 -WarningAction SilentlyContinue

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}

AfterAll {
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

Remove-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Database $db1, $db2 -ErrorAction SilentlyContinue -Confirm:$false
Remove-DbaDatabase -Confirm:$false -SqlInstance $TestConfig.instance2 -Database $db1, $db2 -ErrorAction SilentlyContinue
Expand All @@ -68,44 +65,40 @@ Describe $CommandName -Tag IntegrationTests {

It "Gets all snapshots by default" {
$results = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2
($results | Where-Object Name -Like "dbatoolsci_GetSnap*").Count | Should -BeExactly 3
($results | Where-Object Name -Like "dbatoolsci_GetSnap*").Count | Should -Be 3
}

It "Honors the Database parameter, returning only snapshots of that database" {
$results = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Database $db1
$results.Count | Should -BeExactly 2
$results.Count | Should -Be 2
$result = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Database $db2
$result.SnapshotOf | Should -Be $db2
$result.SnapshotOf | Should -Be $db2
}

It "Honors the ExcludeDatabase parameter, returning relevant snapshots" {
$alldbs = (Get-DbaDatabase -SqlInstance $TestConfig.instance2 | Where-Object IsDatabaseSnapShot -eq $false | Where-Object Name -notin @($db1, $db2)).Name
$results = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -ExcludeDatabase $alldbs
$results.Count | Should -BeExactly 3
$results.Count | Should -Be 3
}

It "Honors the Snapshot parameter" {
$result = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Snapshot $db1_snap1
$result.Name | Should -Be $db1_snap1
$result.SnapshotOf | Should -Be $db1
$result.Name | Should -Be $db1_snap1
$result.SnapshotOf | Should -Be $db1
}

It "Honors the ExcludeSnapshot parameter" {
$result = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -ExcludeSnapshot $db1_snap1 -Database $db1
$result.Name | Should -Be $db1_snap2
$result.Name | Should -Be $db1_snap2
}

It "has the correct default properties" {
$result = Get-DbaDbSnapshot -SqlInstance $TestConfig.instance2 -Database $db2
$ExpectedPropsDefault = @(
"ComputerName",
"CreateDate",
"InstanceName",
"Name",
"SnapshotOf",
"SqlInstance",
"DiskUsage"
)
$ExpectedPropsDefault = "ComputerName", "CreateDate", "InstanceName", "Name", "SnapshotOf", "SqlInstance", "DiskUsage"
($result.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | Sort-Object) | Should -Be ($ExpectedPropsDefault | Sort-Object)
}
}
Expand Down
55 changes: 27 additions & 28 deletions tests/Get-DbaDbSpace.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ param(
$PSDefaultParameterValues = $TestConfig.Defaults
)

Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig

Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
Expand All @@ -33,74 +30,76 @@ Describe $CommandName -Tag UnitTests {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Create test database for space testing
$dbname = "dbatoolsci_test_$(Get-Random)"
# Set variables. They are available in all the It blocks.
$dbName = "dbatoolsci_test_$(Get-Random)"
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$null = $server.Query("Create Database [$dbname]")
$null = $server.Query("Create Database [$dbName]")

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
}


AfterAll {
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true

# Cleanup test database
$null = Remove-DbaDatabase -SqlInstance $TestConfig.instance2 -Database $dbname
# Cleanup all created objects.
Remove-DbaDatabase -SqlInstance $TestConfig.instance2 -Database $dbName -Confirm:$false

# As this is the last block we do not need to reset the $PSDefaultParameterValues.
}


#Skipping these tests as internals of Get-DbaDbSpace seems to be unreliable in CI
Context "Gets DbSpace" {
BeforeAll {
$results = Get-DbaDbSpace -SqlInstance $TestConfig.instance2 | Where-Object Database -eq $dbname
$allResults = @(Get-DbaDbSpace -SqlInstance $TestConfig.instance2 | Where-Object Database -eq $dbName)
}

It "Gets results" {
$results | Should -Not -BeNullOrEmpty
$allResults | Should -Not -BeNullOrEmpty
}

It "Should retrieve space for test database" {
$results[0].Database | Should -Be $dbname
$results[0].UsedSpace | Should -Not -BeNullOrEmpty
It "Should retrieve space for $dbName" {
$allResults[0].Database | Should -Be $dbName
$allResults[0].UsedSpace | Should -Not -BeNullOrEmpty
}

It "Should have a physical path for test database" {
$results[0].physicalname | Should -Not -BeNullOrEmpty
It "Should have a physical path for $dbName" {
$allResults[0].PhysicalName | Should -Not -BeNullOrEmpty
}
}

#Skipping these tests as internals of Get-DbaDbSpace seems to be unreliable in CI
Context "Gets DbSpace when using -Database" {
BeforeAll {
$results = Get-DbaDbSpace -SqlInstance $TestConfig.instance2 -Database $dbname
$databaseResults = @(Get-DbaDbSpace -SqlInstance $TestConfig.instance2 -Database $dbName)
}

It "Gets results" {
$results | Should -Not -BeNullOrEmpty
$databaseResults | Should -Not -BeNullOrEmpty
}

It "Should retrieve space for test database" {
$results[0].Database | Should -Be $dbname
$results[0].UsedSpace | Should -Not -BeNullOrEmpty
It "Should retrieve space for $dbName" {
$databaseResults[0].Database | Should -Be $dbName
$databaseResults[0].UsedSpace | Should -Not -BeNullOrEmpty
}

It "Should have a physical path for test database" {
$results[0].physicalname | Should -Not -BeNullOrEmpty
It "Should have a physical path for $dbName" {
$databaseResults[0].PhysicalName | Should -Not -BeNullOrEmpty
}
}

Context "Gets no DbSpace for specific database when using -ExcludeDatabase" {
BeforeAll {
$results = Get-DbaDbSpace -SqlInstance $TestConfig.instance2 -ExcludeDatabase $dbname
$excludeResults = @(Get-DbaDbSpace -SqlInstance $TestConfig.instance2 -ExcludeDatabase $dbName)
}

It "Gets no results" {
$results.database | Should -Not -Contain $dbname
It "Gets no results for excluded database" {
$excludeResults.Database | Should -Not -Contain $dbName
}
}
}
Loading
Loading