Skip to content

Commit 2d1181e

Browse files
Refactor and enhance Pester tests batch (#9769)
1 parent 0eb1df6 commit 2d1181e

File tree

48 files changed

+1752
-794
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1752
-794
lines changed

tests/Get-DbaReplPublisher.Tests.ps1

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaReplPublisher",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
47

8+
$global:TestConfig = Get-TestConfig
59

6-
Add-ReplicationLibrary
10+
Describe $CommandName -Tag UnitTests {
11+
Context "Parameter validation" {
12+
BeforeAll {
13+
Add-ReplicationLibrary
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"SqlInstance",
18+
"SqlCredential",
19+
"EnableException"
20+
)
21+
}
722

8-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
9-
Context "Validate parameters" {
10-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
11-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'EnableException'
12-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
13-
It "Should only contain our specific parameters" {
14-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
23+
It "Should have the expected parameters" {
24+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1525
}
1626
}
1727
}

tests/Get-DbaReplServer.Tests.ps1

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaReplServer",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
47

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
8+
Describe $CommandName -Tag UnitTests {
9+
Context "Parameter validation" {
10+
BeforeAll {
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
14+
"SqlInstance",
15+
"SqlCredential",
16+
"EnableException"
17+
)
18+
}
19+
20+
It "Should have the expected parameters" {
21+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1222
}
1323
}
1424
}

tests/Get-DbaReplSubscription.Tests.ps1

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaReplSubscription",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
611
Add-ReplicationLibrary
712

8-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
9-
Context "Validate parameters" {
10-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm') }
11-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'PublicationName', 'SubscriberName', 'SubscriptionDatabase', 'Type', 'EnableException'
12-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
13-
It "Should only contain our specific parameters" {
14-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
13+
Describe $CommandName -Tag UnitTests {
14+
Context "Parameter validation" {
15+
BeforeAll {
16+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
17+
$expectedParameters = $TestConfig.CommonParameters
18+
$expectedParameters += @(
19+
"SqlInstance",
20+
"SqlCredential",
21+
"Database",
22+
"PublicationName",
23+
"SubscriberName",
24+
"SubscriptionDatabase",
25+
"Type",
26+
"EnableException"
27+
)
28+
}
29+
30+
It "Should have the expected parameters" {
31+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1532
}
1633
}
1734
}

tests/Get-DbaResourceGovernor.Tests.ps1

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaResourceGovernor",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
27
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
38
$global:TestConfig = Get-TestConfig
49

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
10+
Describe $CommandName -Tag UnitTests {
11+
Context "Parameter validation" {
12+
BeforeAll {
13+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
14+
$expectedParameters = $TestConfig.CommonParameters
15+
$expectedParameters += @(
16+
"SqlInstance",
17+
"SqlCredential",
18+
"EnableException"
19+
)
20+
}
21+
22+
It "Should have the expected parameters" {
23+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1224
}
1325
}
1426
}
Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,66 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaRgClassifierFunction",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
11+
Describe $CommandName -Tag UnitTests {
12+
Context "Parameter validation" {
13+
BeforeAll {
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"SqlInstance",
18+
"SqlCredential",
19+
"InputObject",
20+
"EnableException"
21+
)
22+
}
23+
24+
It "Should have the expected parameters" {
25+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1226
}
1327
}
1428
}
1529

16-
Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
30+
Describe $CommandName -Tag IntegrationTests {
1731
BeforeAll {
18-
$sql = "CREATE FUNCTION dbatoolsci_fnRG()
19-
RETURNS sysname
20-
WITH SCHEMABINDING
21-
AS
22-
BEGIN
23-
RETURN N'gOffHoursProcessing'
24-
END"
32+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
33+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
34+
35+
$sql = @"
36+
CREATE FUNCTION dbatoolsci_fnRG()
37+
RETURNS sysname
38+
WITH SCHEMABINDING
39+
AS
40+
BEGIN
41+
RETURN N'gOffHoursProcessing'
42+
END
43+
"@
2544

2645
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query $sql
2746
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query "ALTER RESOURCE GOVERNOR with (CLASSIFIER_FUNCTION = dbo.dbatoolsci_fnRG); ALTER RESOURCE GOVERNOR RECONFIGURE"
47+
48+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
49+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
2850
}
51+
2952
AfterAll {
53+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
54+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
55+
3056
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query "ALTER RESOURCE GOVERNOR WITH (CLASSIFIER_FUNCTION = NULL); ALTER RESOURCE GOVERNOR RECONFIGURE"
3157
Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query "DROP FUNCTION [dbo].[dbatoolsci_fnRG]"
3258
}
3359

3460
Context "Command works" {
35-
It "returns the proper classifier function" {
61+
It "Returns the proper classifier function" {
3662
$results = Get-DbaRgClassifierFunction -SqlInstance $TestConfig.instance2
37-
$results.Name | Should -Be 'dbatoolsci_fnRG'
63+
$results.Name | Should -Be "dbatoolsci_fnRG"
3864
}
3965
}
4066
}

tests/Get-DbaRgResourcePool.Tests.ps1

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaRgResourcePool",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
28
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
39
$global:TestConfig = Get-TestConfig
410

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Type', 'InputObject', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
11+
Describe $CommandName -Tag UnitTests {
12+
Context "Parameter validation" {
13+
BeforeAll {
14+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
15+
$expectedParameters = $TestConfig.CommonParameters
16+
$expectedParameters += @(
17+
"SqlInstance",
18+
"SqlCredential",
19+
"Type",
20+
"InputObject",
21+
"EnableException"
22+
)
23+
}
24+
25+
It "Should have the expected parameters" {
26+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1227
}
1328
}
1429
}
1530

16-
Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
17-
Context "Command actually works" {
18-
$results = Get-DbaRgResourcePool -SqlInstance $TestConfig.instance2
19-
it "Gets Results" {
20-
$results | Should Not Be $null
31+
Describe $CommandName -Tag IntegrationTests {
32+
Context "When getting resource pools" {
33+
BeforeAll {
34+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
35+
$allResults = Get-DbaRgResourcePool -SqlInstance $TestConfig.instance2
36+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
37+
}
38+
39+
It "Gets Results" {
40+
$allResults | Should -Not -BeNullOrEmpty
2141
}
2242
}
23-
Context "Command actually works using -Type" {
24-
$results = Get-DbaRgResourcePool -SqlInstance $TestConfig.instance2 -Type Internal
25-
it "Gets Results" {
26-
$results | Should Not Be $null
43+
44+
Context "When getting resource pools using -Type parameter" {
45+
BeforeAll {
46+
$PSDefaultParameterValues["*-Dba*:EnableException"] = $true
47+
$typeResults = Get-DbaRgResourcePool -SqlInstance $TestConfig.instance2 -Type Internal
48+
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
49+
}
50+
51+
It "Gets Results with Type filter" {
52+
$typeResults | Should -Not -BeNullOrEmpty
2753
}
2854
}
2955
}

tests/Get-DbaRgWorkloadGroup.Tests.ps1

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaRgWorkloadGroup",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
47

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'EnableException'
9-
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
8+
Describe $CommandName -Tag UnitTests {
9+
Context "Parameter validation" {
10+
BeforeAll {
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
14+
"SqlInstance",
15+
"SqlCredential",
16+
"InputObject",
17+
"EnableException"
18+
)
19+
}
20+
21+
It "Should have the expected parameters" {
22+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1223
}
1324
}
1425
}

tests/Get-DbaRunningJob.Tests.ps1

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaRunningJob",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
47

5-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
6-
Context "Validate parameters" {
7-
[array]$params = ([Management.Automation.CommandMetaData]$ExecutionContext.SessionState.InvokeCommand.GetCommand($CommandName, 'Function')).Parameters.Keys
8-
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'EnableException'
8+
Describe $CommandName -Tag UnitTests {
9+
Context "Parameter validation" {
10+
BeforeAll {
11+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
12+
$expectedParameters = $TestConfig.CommonParameters
13+
$expectedParameters += @(
14+
"SqlInstance",
15+
"SqlCredential",
16+
"InputObject",
17+
"EnableException"
18+
)
19+
}
920

10-
It "Should only contain our specific parameters" {
11-
Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params | Should -BeNullOrEmpty
21+
It "Should have the expected parameters" {
22+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1223
}
1324
}
1425
}

0 commit comments

Comments
 (0)