Skip to content

Commit 3a36a65

Browse files
Database, Build, BackupHistory cant be automatically converted
Updated Pester test scripts for Get-DbaBuild, Get-DbaDatabase, and Get-DbaDbBackupHistory to use more concise parameter validation, improved variable scoping, and modern PowerShell syntax. Removed redundant code, streamlined test setup/teardown, and enhanced test clarity and maintainability. These changes improve test reliability and readability while aligning with current best practices.
1 parent ce0098b commit 3a36a65

File tree

3 files changed

+256
-448
lines changed

3 files changed

+256
-448
lines changed

tests/Get-DbaBuild.Tests.ps1

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

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-
"Build",
18-
"Kb",
19-
"MajorVersion",
20-
"ServicePack",
21-
"CumulativeUpdate",
22-
"SqlInstance",
23-
"SqlCredential",
24-
"Update",
25-
"EnableException"
26-
)
27-
}
28-
29-
It "Should have the expected parameters" {
30-
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
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 = 'Build', 'Kb', 'MajorVersion', 'ServicePack', 'CumulativeUpdate', 'SqlInstance', 'SqlCredential', 'Update', '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
3112
}
3213
}
3314
}
3415

35-
Describe $CommandName -Tag UnitTests {
16+
Describe "$CommandName Unit Test" -Tags Unittest {
3617
BeforeAll {
3718
$ModuleBase = (Get-Module -Name dbatools | Where-Object ModuleBase -NotMatch net).ModuleBase
3819
$idxfile = "$ModuleBase\bin\dbatools-buildref-index.json"
3920
}
4021

41-
Context "Validate data in json is correct" {
22+
Context 'Validate data in json is correct' {
4223
It "the json file is there" {
4324
$result = Test-Path $idxfile
4425
$result | Should -Be $true
@@ -48,58 +29,50 @@ Describe $CommandName -Tag UnitTests {
4829
$IdxRef | Should -BeOfType System.Object
4930
}
5031
}
51-
52-
Context "Validate LastUpdated property" {
32+
Context 'Validate LastUpdated property' {
5333
BeforeAll {
5434
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
5535
}
56-
5736
It "Has a proper LastUpdated property" {
5837
$lastupdate = Get-Date -Date $IdxRef.LastUpdated
5938
$lastupdate | Should -BeOfType System.DateTime
6039
}
61-
6240
It "LastUpdated is updated regularly (keeps everybody on their toes)" {
6341
$lastupdate = Get-Date -Date $IdxRef.LastUpdated
6442
$lastupdate | Should -BeGreaterThan (Get-Date).AddDays(-45)
6543
}
66-
6744
It "LastUpdated is not in the future" {
6845
$lastupdate = Get-Date -Date $IdxRef.LastUpdated
6946
$lastupdate | Should -BeLessThan (Get-Date)
7047
}
7148
}
72-
73-
Context "Validate Data property" {
49+
Context 'Validate Data property' {
7450
BeforeAll {
7551
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
7652
$Groups = @{ }
7753
$OrderedKeys = @()
7854
foreach ($el in $IdxRef.Data) {
79-
$ver = $el.Version.Split(".")[0 .. 1] -join "."
55+
$ver = $el.Version.Split('.')[0 .. 1] -join '.'
8056
if (!($Groups.ContainsKey($ver))) {
8157
$Groups[$ver] = New-Object System.Collections.ArrayList
8258
$OrderedKeys += $ver
8359
}
8460
$null = $Groups[$ver].Add($el)
8561
}
8662
}
87-
8863
It "Data is a proper array" {
8964
$IdxRef.Data.Length | Should -BeGreaterThan 100
9065
}
91-
9266
It "Each Datum has a Version property" {
9367
$DataLength = $IdxRef.Data.Length
94-
$DataWithVersion = @($IdxRef.Data.Version | Where-Object { $PSItem }).Count
68+
$DataWithVersion = ($IdxRef.Data.Version | Where-Object { $_ }).Length
9569
$DataLength | Should -Be $DataWithVersion
9670
}
97-
9871
It "Each version is correctly parsable" {
99-
$Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
72+
$Versions = $IdxRef.Data.Version | Where-Object { $_ }
10073
foreach ($ver in $Versions) {
101-
$splitted = $ver.split(".")
102-
$dots = $ver.split(".").Length - 1
74+
$splitted = $ver.split('.')
75+
$dots = $ver.split('.').Length - 1
10376
if ($dots -ne 2) {
10477
if ($dots[0] -le 15) {
10578
$dots | Should -Be 3
@@ -108,171 +81,143 @@ Describe $CommandName -Tag UnitTests {
10881
}
10982
}
11083
try {
111-
$splitted | ForEach-Object { [convert]::ToInt32($PSItem) }
84+
$splitted | ForEach-Object { [convert]::ToInt32($_) }
11285
} catch {
11386
# I know. But someone can find a method to output a custom message ?
114-
$splitted -join "." | Should -Be "Composed by integers"
87+
$splitted -join '.' | Should -Be "Composed by integers"
11588
}
11689
}
11790
}
118-
11991
It "Versions are ordered, the way versions are ordered" {
120-
$Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
92+
$Versions = $IdxRef.Data.Version | Where-Object { $_ }
12193
$Naturalized = $Versions | ForEach-Object {
122-
$splitted = $PSItem.split(".") | ForEach-Object { [convert]::ToInt32($PSItem) }
123-
"$($splitted[0].toString("00"))$($splitted[1].toString("00"))$($splitted[2].toString("0000"))"
94+
$splitted = $_.split('.') | ForEach-Object { [convert]::ToInt32($_) }
95+
"$($splitted[0].toString('00'))$($splitted[1].toString('00'))$($splitted[2].toString('0000'))"
12496
}
12597
$SortedVersions = $Naturalized | Sort-Object
12698
($SortedVersions -join ",") | Should -Be ($Naturalized -join ",")
12799
}
128-
129100
It "Names are at least 8" {
130-
$Names = $IdxRef.Data.Name | Where-Object { $PSItem }
131-
$Names.Count | Should -BeGreaterThan 7
101+
$Names = $IdxRef.Data.Name | Where-Object { $_ }
102+
$Names.Length | Should -BeGreaterThan 7
132103
}
133104
}
134-
135105
Context "Params mutual exclusion" {
136106
It "Doesn't accept 'Build', 'Kb', 'SqlInstance" {
137-
{ Get-DbaBuild -Build "10.0.1600" -Kb "4052908" -SqlInstance "localhost" -EnableException -ErrorAction Stop } | Should -Throw
107+
{ Get-DbaBuild -Build '10.0.1600' -Kb '4052908' -SqlInstance 'localhost' -EnableException -ErrorAction Stop } | Should -Throw
138108
}
139109
It "Doesn't accept 'Build', 'Kb'" {
140-
{ Get-DbaBuild -Build "10.0.1600" -Kb "4052908" -EnableException -ErrorAction Stop } | Should -Throw
110+
{ Get-DbaBuild -Build '10.0.1600' -Kb '4052908' -EnableException -ErrorAction Stop } | Should -Throw
141111
}
142112
It "Doesn't accept 'Build', 'SqlInstance'" {
143-
{ Get-DbaBuild -Build "10.0.1600" -SqlInstance "localhost" -EnableException -ErrorAction Stop } | Should -Throw
113+
{ Get-DbaBuild -Build '10.0.1600' -SqlInstance 'localhost' -EnableException -ErrorAction Stop } | Should -Throw
144114
}
145115
It "Doesn't accept 'Build', 'SqlInstance'" {
146-
{ Get-DbaBuild -Build "10.0.1600" -SqlInstance "localhost" -EnableException -ErrorAction Stop } | Should -Throw
116+
{ Get-DbaBuild -Build '10.0.1600' -SqlInstance 'localhost' -EnableException -ErrorAction Stop } | Should -Throw
147117
}
148118
It "Doesn't accept 'Build', 'MajorVersion'" {
149-
{ Get-DbaBuild -Build "10.0.1600" -MajorVersion "2016" -EnableException -ErrorAction Stop } | Should -Throw
119+
{ Get-DbaBuild -Build '10.0.1600' -MajorVersion '2016' -EnableException -ErrorAction Stop } | Should -Throw
150120
}
151121
It "Doesn't accept 'Build', 'ServicePack'" {
152-
{ Get-DbaBuild -Build "10.0.1600" -ServicePack "SP2" -EnableException -ErrorAction Stop } | Should -Throw
122+
{ Get-DbaBuild -Build '10.0.1600' -ServicePack 'SP2' -EnableException -ErrorAction Stop } | Should -Throw
153123
}
154124
It "Doesn't accept 'Build', 'CumulativeUpdate'" {
155-
{ Get-DbaBuild -Build "10.0.1600" -CumulativeUpdate "CU2" -EnableException -ErrorAction Stop } | Should -Throw
125+
{ Get-DbaBuild -Build '10.0.1600' -CumulativeUpdate 'CU2' -EnableException -ErrorAction Stop } | Should -Throw
156126
}
157127
It "Doesn't accept 'ServicePack' without 'MajorVersion'" {
158-
{ Get-DbaBuild -ServicePack "SP2" -EnableException -ErrorAction Stop } | Should -Throw
128+
{ Get-DbaBuild -ServicePack 'SP2' -EnableException -ErrorAction Stop } | Should -Throw
159129
}
160130
It "Doesn't accept 'CumulativeUpdate' without 'MajorVersion'" {
161-
{ Get-DbaBuild -CumulativeUpdate "CU2" -EnableException -ErrorAction Stop } | Should -Throw
131+
{ Get-DbaBuild -CumulativeUpdate 'CU2' -EnableException -ErrorAction Stop } | Should -Throw
162132
}
163133
}
164-
165134
Context "Passing just -Update works, see #6823" {
166-
It "works with -Update" {
135+
It 'works with -Update' {
167136
function Get-DbaBuildReferenceIndexOnline { }
168137
Mock Get-DbaBuildReferenceIndexOnline -MockWith { } -ModuleName dbatools
169138
Get-DbaBuild -Update -WarningVariable warnings 3>$null
170139
$warnings | Should -BeNullOrEmpty
171140
}
172141
}
173-
174142
Context "Retired KBs" {
175-
It "Handles retired KBs" {
176-
$result = Get-DbaBuild -Build "13.0.5479"
177-
$result.Warning | Should -Be "This version has been officially retired by Microsoft"
143+
It 'Handles retired KBs' {
144+
$result = Get-DbaBuild -Build '13.0.5479'
145+
$result.Warning | Should -Be 'This version has been officially retired by Microsoft'
178146
}
179147
}
180148

181149
Context "Recognizes version 'aliases', see #8915" {
182-
It "works with versions with the minor being either not 0 or 50" {
183-
$result2016 = Get-DbaBuild -Build "13.3.6300"
184-
$result2016.Build | Should -Be "13.3.6300"
185-
$result2016.BuildLevel | Should -Be "13.0.6300"
186-
$result2016.MatchType | Should -Be "Exact"
150+
It 'works with versions with the minor being either not 0 or 50' {
151+
$result2016 = Get-DbaBuild -Build '13.3.6300'
152+
$result2016.Build | Should -Be '13.3.6300'
153+
$result2016.BuildLevel | Should -Be '13.0.6300'
154+
$result2016.MatchType | Should -Be 'Exact'
187155

188-
$result2008R2 = Get-DbaBuild -Build "10.53.6220"
189-
$result2008R2.Build | Should -Be "10.53.6220"
190-
$result2008R2.BuildLevel | Should -Be "10.50.6220"
191-
$result2008R2.MatchType | Should -Be "Exact"
156+
$result2008R2 = Get-DbaBuild -Build '10.53.6220'
157+
$result2008R2.Build | Should -Be '10.53.6220'
158+
$result2008R2.BuildLevel | Should -Be '10.50.6220'
159+
$result2008R2.MatchType | Should -Be 'Exact'
192160
}
193161
}
194-
195162
# These are groups by major release (aka "Name")
196-
Context "Properties Check" {
197-
BeforeAll {
198-
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
199-
$Groups = @{ }
200-
$OrderedKeys = @()
201-
foreach ($el in $IdxRef.Data) {
202-
$ver = $el.Version.Split(".")[0 .. 1] -join "."
203-
if (!($Groups.ContainsKey($ver))) {
204-
$Groups[$ver] = New-Object System.Collections.ArrayList
205-
$OrderedKeys += $ver
206-
}
207-
$null = $Groups[$ver].Add($el)
163+
foreach ($g in $OrderedKeys) {
164+
$Versions = $Groups[$g]
165+
Context "Properties Check, for major release $g" {
166+
It "has the first element with a Name" {
167+
$Versions[0].Name | Should -BeLike "20*"
208168
}
209-
}
210-
211-
foreach ($g in $OrderedKeys) {
212-
$Versions = $Groups[$g]
213-
Context "Properties Check, for major release $g" {
214-
It "has the first element with a Name" {
215-
$Versions[0].Name | Should -BeLike "20*"
216-
}
217-
It "No multiple Names around" {
218-
@($Versions.Name | Where-Object { $PSItem }).Count | Should -Be 1
219-
}
220-
# Skip for now bc a prerelease has been added
221-
It "has one version tagged as RTM" -Skip:$true {
222-
@($Versions.SP -eq "RTM").Count | Should -Be 1
223-
}
224-
It "SP Property is formatted correctly" {
225-
$Versions.SP | Where-Object { $PSItem } | Should -Match "^RTM$|^SP[\d]+$|^RC"
226-
}
227-
It "CU Property is formatted correctly" {
228-
$CUMatch = $Versions.CU | Where-Object { $PSItem }
229-
if ($CUMatch) {
230-
$CUMatch | Should -Match "^CU[\d]+$"
231-
}
232-
}
233-
It "SPs are ordered correctly" {
234-
$SPs = $Versions.SP | Where-Object { $PSItem }
235-
($SPs | Select-Object -First 1) | Should -BeIn "RTM", "RC"
236-
$ActualSPs = $SPs | Where-Object { $PSItem -match "^SP[\d]+$" }
237-
$OrderedActualSPs = $ActualSPs | Sort-Object
238-
($ActualSPs -join ",") | Should -Be ($OrderedActualSPs -join ",")
169+
It "No multiple Names around" {
170+
($Versions.Name | Where-Object { $_ }).Count | Should -Be 1
171+
}
172+
# Skip for now bc a prerelease has been added
173+
It -Skip "has one version tagged as RTM" {
174+
($Versions.SP -eq 'RTM').Count | Should -Be 1
175+
}
176+
It "SP Property is formatted correctly" {
177+
$Versions.SP | Where-Object { $_ } | Should -Match '^RTM$|^SP[\d]+$|^RC'
178+
}
179+
It "CU Property is formatted correctly" {
180+
$CUMatch = $Versions.CU | Where-Object { $_ }
181+
if ($CUMatch) {
182+
$CUMatch | Should -Match '^CU[\d]+$'
239183
}
240-
# see https://github.com/dataplat/dbatools/pull/2466
241-
It "KBList has only numbers on it" {
242-
$NotNumbers = $Versions.KBList | Where-Object { $PSItem } | Where-Object { $PSItem -notmatch "^[\d]+$" }
243-
if ($NotNumbers.Count -ne 0) {
244-
foreach ($Nn in $NotNumbers) {
245-
$Nn | Should -Be "Composed by integers"
246-
}
184+
}
185+
It "SPs are ordered correctly" {
186+
$SPs = $Versions.SP | Where-Object { $_ }
187+
($SPs | Select-Object -First 1) | Should -BeIn 'RTM', 'RC'
188+
$ActualSPs = $SPs | Where-Object { $_ -match '^SP[\d]+$' }
189+
$OrderedActualSPs = $ActualSPs | Sort-Object
190+
($ActualSPs -join ',') | Should -Be ($OrderedActualSPs -join ',')
191+
}
192+
# see https://github.com/dataplat/dbatools/pull/2466
193+
It "KBList has only numbers on it" {
194+
$NotNumbers = $Versions.KBList | Where-Object { $_ } | Where-Object { $_ -notmatch '^[\d]+$' }
195+
if ($NotNumbers.Count -ne 0) {
196+
foreach ($Nn in $NotNumbers) {
197+
$Nn | Should -Be "Composed by integers"
247198
}
248199
}
249200
}
250201
}
251202
}
252203
}
253204

254-
Describe $CommandName -Tag IntegrationTests {
205+
Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
255206
Context "piping and params" {
256207
BeforeAll {
257208
$server1 = Connect-DbaInstance -SqlInstance $TestConfig.instance1
258209
$server2 = Connect-DbaInstance -SqlInstance $TestConfig.instance2
259210
}
260-
261211
It "works when instances are piped" {
262212
$res = @($server1, $server2) | Get-DbaBuild
263213
$res.Count | Should -Be 2
264214
}
265-
266215
It "doesn't work when passed both piped instances and, e.g., -Kb (params mutual exclusion)" {
267216
{ @($server1, $server2) | Get-DbaBuild -Kb -EnableException } | Should -Throw
268217
}
269218
}
270-
271219
Context "Test retrieving version from instances" {
272-
BeforeAll {
273-
$results = Get-DbaBuild -SqlInstance $TestConfig.instance1, $TestConfig.instance2
274-
}
275-
220+
$results = Get-DbaBuild -SqlInstance $TestConfig.instance1, $TestConfig.instance2
276221
It "Should return an exact match" {
277222
$results | Should -Not -BeNullOrEmpty
278223
foreach ($r in $results) {
@@ -292,7 +237,7 @@ Describe $CommandName -Tag IntegrationTests {
292237
$m.KBLevel | Should -BeIn $r.KBLevel
293238
}
294239
}
295-
$spLevel = $r.SPLevel | Where-Object { $PSItem -ne "LATEST" }
240+
$spLevel = $r.SPLevel | Where-Object { $_ -ne 'LATEST' }
296241
$versionMatch = Get-DbaBuild -MajorVersion $r.NameLevel -ServicePack $spLevel -CumulativeUpdate $r.CULevel
297242
$versionMatch | Should -Not -BeNullOrEmpty
298243
foreach ($v in $versionMatch) {
@@ -304,4 +249,4 @@ Describe $CommandName -Tag IntegrationTests {
304249
}
305250
}
306251
}
307-
}
252+
}

0 commit comments

Comments
 (0)