Skip to content

Commit ce0098b

Browse files
Refactor and enhance Get-DbaCredential and Get-DbaDatabase tests
Improved test setup and teardown in Get-DbaCredential.Tests.ps1 by managing EnableException in $PSDefaultParameterValues and refactoring credential creation. In Get-DbaDatabase.Tests.ps1, reorganized and consolidated integration and unit tests, removed redundant code, and improved test clarity and reliability.
1 parent 5fc846c commit ce0098b

File tree

2 files changed

+96
-88
lines changed

2 files changed

+96
-88
lines changed

tests/Get-DbaCredential.Tests.ps1

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ $global:TestConfig = Get-TestConfig
1313
Describe $CommandName -Tag UnitTests {
1414
Context "Parameter validation" {
1515
BeforeAll {
16-
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
16+
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
1717
$expectedParameters = $TestConfig.CommonParameters
1818
$expectedParameters += @(
1919
"SqlInstance",
@@ -34,9 +34,12 @@ Describe $CommandName -Tag UnitTests {
3434

3535
Describe $CommandName -Tag IntegrationTests {
3636
BeforeAll {
37-
$logins = "thor", "thorsmomma"
37+
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
38+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
39+
40+
$logins = "thor", "thorsmomma"
3841
$plaintext = "BigOlPassword!"
39-
$password = ConvertTo-SecureString $plaintext -AsPlainText -Force
42+
$password = ConvertTo-SecureString $plaintext -AsPlainText -Force
4043

4144
# Add user
4245
foreach ($login in $logins) {
@@ -51,46 +54,55 @@ Describe $CommandName -Tag IntegrationTests {
5154
}
5255
$null = New-DbaCredential @splatThorCred
5356

54-
$splatMommaCred = @{
57+
$splatThorsmormmaCred = @{
5558
SqlInstance = $TestConfig.instance2
5659
Identity = "thorsmomma"
5760
Password = $password
5861
}
59-
$null = New-DbaCredential @splatMommaCred
60-
}
62+
$null = New-DbaCredential @splatThorsmormmaCred
6163

64+
# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
65+
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
66+
}
6267
AfterAll {
63-
$splatGetCred = @{
64-
SqlInstance = $TestConfig.instance2
65-
Identity = "thor", "thorsmomma"
66-
ErrorAction = "Stop"
67-
WarningAction = "SilentlyContinue"
68-
}
68+
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
69+
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true
70+
6971
try {
72+
$splatGetCred = @{
73+
SqlInstance = $TestConfig.instance2
74+
Identity = "thor", "thorsmomma"
75+
ErrorAction = "Stop"
76+
WarningAction = "SilentlyContinue"
77+
}
7078
(Get-DbaCredential @splatGetCred).Drop()
7179
} catch { }
7280

7381
foreach ($login in $logins) {
7482
$null = Invoke-Command2 -ScriptBlock { net user $args /delete *>&1 } -ArgumentList $login -ComputerName $TestConfig.instance2
7583
}
84+
85+
# As this is the last block we do not need to reset the $PSDefaultParameterValues.
7686
}
7787

7888
Context "Get credentials" {
7989
It "Should get just one credential with the proper properties when using Identity" {
80-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thorsmomma
90+
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity "thorsmomma"
8191
$results.Name | Should -Be "thorsmomma"
8292
$results.Identity | Should -Be "thorsmomma"
8393
}
84-
8594
It "Should get just one credential with the proper properties when using Name" {
86-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Name thorsmomma
95+
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Name "thorsmomma"
8796
$results.Name | Should -Be "thorsmomma"
8897
$results.Identity | Should -Be "thorsmomma"
8998
}
90-
9199
It "gets more than one credential" {
92-
$results = Get-DbaCredential -SqlInstance $TestConfig.instance2 -Identity thor, thorsmomma
93-
$results.Status.Count | Should -BeGreaterThan 1
100+
$splatMultipleCreds = @{
101+
SqlInstance = $TestConfig.instance2
102+
Identity = "thor", "thorsmomma"
103+
}
104+
$results = Get-DbaCredential @splatMultipleCreds
105+
$results.Count | Should -BeGreaterThan 1
94106
}
95107
}
96108
}

tests/Get-DbaDatabase.Tests.ps1

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ param(
55
$PSDefaultParameterValues = $TestConfig.Defaults
66
)
77

8-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
9-
$global:TestConfig = Get-TestConfig
10-
118
Describe $CommandName -Tag UnitTests {
129
Context "Validate parameters" {
1310
BeforeAll {
@@ -39,14 +36,74 @@ Describe $CommandName -Tag UnitTests {
3936
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
4037
}
4138
}
39+
}
4240

43-
Context "Input validation" {
41+
Describe $CommandName -Tag IntegrationTests {
42+
Context "Count system databases on localhost" {
43+
BeforeAll {
44+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -ExcludeUser
45+
}
46+
47+
It "reports the right number of databases" {
48+
$results.Count | Should -BeExactly 4
49+
}
50+
}
51+
52+
Context "Check that tempdb database is in Simple recovery mode" {
53+
BeforeAll {
54+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database tempdb
55+
}
56+
57+
It "tempdb's recovery mode is Simple" {
58+
$results.RecoveryModel | Should -Be "Simple"
59+
}
60+
}
61+
62+
Context "Check that master database is accessible" {
63+
BeforeAll {
64+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master
65+
}
66+
67+
It "master is accessible" {
68+
$results.IsAccessible | Should -Be $true
69+
}
70+
}
71+
72+
Context "Results return if no backup" {
4473
BeforeAll {
45-
## Ensure it is the module that is being coded that is in the session when running just this Pester test
46-
# Remove-Module dbatools -Force -ErrorAction SilentlyContinue
47-
# $Base = Split-Path -parent $PSCommandPath
48-
# Import-Module $Base\..\dbatools.psd1
74+
$random = Get-Random
75+
$dbname1 = "dbatoolsci_Backup_$random"
76+
$dbname2 = "dbatoolsci_NoBackup_$random"
77+
$null = New-DbaDatabase -SqlInstance $TestConfig.instance1 -Name $dbname1, $dbname2
78+
$null = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Type Full -FilePath nul -Database $dbname1
79+
}
4980

81+
AfterAll {
82+
$null = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname1, $dbname2 | Remove-DbaDatabase -Confirm:$false
83+
}
84+
85+
It "Should not report as database has full backup" {
86+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname1 -NoFullBackup
87+
$results.Count | Should -BeExactly 0
88+
}
89+
90+
It "Should report 1 database with no full backup" {
91+
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname2 -NoFullBackup
92+
$results.Count | Should -BeExactly 1
93+
}
94+
}
95+
}
96+
97+
Describe $CommandName -Tag UnitTests {
98+
BeforeAll {
99+
## Ensure it is the module that is being coded that is in the session when running just this Pester test
100+
# Remove-Module dbatools -Force -ErrorAction SilentlyContinue
101+
# $Base = Split-Path -parent $PSCommandPath
102+
# Import-Module $Base\..\dbatools.psd1
103+
}
104+
105+
Context "Input validation" {
106+
BeforeAll {
50107
Mock Stop-Function { } -ModuleName dbatools
51108
Mock Test-FunctionInterrupt { } -ModuleName dbatools
52109
Mock Connect-DbaInstance -MockWith {
@@ -78,7 +135,7 @@ Describe $CommandName -Tag UnitTests {
78135
}
79136

80137
It "Should Call Stop-Function if NoUserDbs and NoSystemDbs are specified" {
81-
Get-DbaDatabase -SqlInstance Dummy -ExcludeSystem -ExcludeUser -ErrorAction SilentlyContinue | Should -Be $null
138+
Get-DbaDatabase -SqlInstance Dummy -ExcludeSystem -ExcludeUser -ErrorAction SilentlyContinue | Should -BeNullOrEmpty
82139
}
83140

84141
It "Validates that Stop Function Mock has been called" {
@@ -168,65 +225,4 @@ Describe $CommandName -Tag UnitTests {
168225
Assert-MockCalled @assertMockParams
169226
}
170227
}
171-
}
172-
173-
Describe $CommandName -Tag IntegrationTests {
174-
Context "Count system databases on localhost" {
175-
BeforeAll {
176-
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -ExcludeUser
177-
}
178-
179-
It "reports the right number of databases" {
180-
$results.Status.Count | Should -BeExactly 4
181-
}
182-
}
183-
184-
Context "Check that tempdb database is in Simple recovery mode" {
185-
BeforeAll {
186-
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database tempdb
187-
}
188-
189-
It "tempdb's recovery mode is Simple" {
190-
$results.RecoveryModel | Should -Be "Simple"
191-
}
192-
}
193-
194-
Context "Check that master database is accessible" {
195-
BeforeAll {
196-
$results = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database master
197-
}
198-
199-
It "master is accessible" {
200-
$results.IsAccessible | Should -Be $true
201-
}
202-
}
203-
}
204-
205-
Describe $CommandName -Tag IntegrationTests {
206-
BeforeAll {
207-
$random = Get-Random
208-
$dbname1 = "dbatoolsci_Backup_$random"
209-
$dbname2 = "dbatoolsci_NoBackup_$random"
210-
$null = New-DbaDatabase -SqlInstance $TestConfig.instance1 -Name $dbname1, $dbname2
211-
$null = Backup-DbaDatabase -SqlInstance $TestConfig.instance1 -Type Full -FilePath nul -Database $dbname1
212-
}
213-
214-
AfterAll {
215-
$null = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname1, $dbname2 | Remove-DbaDatabase -Confirm:$false
216-
}
217-
218-
Context "Results return if no backup" {
219-
BeforeAll {
220-
$resultsWithBackup = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname1 -NoFullBackup
221-
$resultsNoBackup = Get-DbaDatabase -SqlInstance $TestConfig.instance1 -Database $dbname2 -NoFullBackup
222-
}
223-
224-
It "Should not report as database has full backup" {
225-
$resultsWithBackup.Status.Count | Should -BeExactly 0
226-
}
227-
228-
It "Should report 1 database with no full backup" {
229-
$resultsNoBackup.Status.Count | Should -BeExactly 1
230-
}
231-
}
232228
}

0 commit comments

Comments
 (0)