Skip to content

Commit 36bfc6d

Browse files
These can't be auto-converted
1 parent d131087 commit 36bfc6d

File tree

2 files changed

+144
-217
lines changed

2 files changed

+144
-217
lines changed

tests/Get-DbaBuild.Tests.ps1

Lines changed: 59 additions & 80 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,7 +29,7 @@ Describe $CommandName -Tag UnitTests {
4829
$IdxRef | Should -BeOfType System.Object
4930
}
5031
}
51-
Context "Validate LastUpdated property" {
32+
Context 'Validate LastUpdated property' {
5233
BeforeAll {
5334
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
5435
}
@@ -65,13 +46,13 @@ Describe $CommandName -Tag UnitTests {
6546
$lastupdate | Should -BeLessThan (Get-Date)
6647
}
6748
}
68-
Context "Validate Data property" {
49+
Context 'Validate Data property' {
6950
BeforeAll {
7051
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
7152
$Groups = @{ }
7253
$OrderedKeys = @()
7354
foreach ($el in $IdxRef.Data) {
74-
$ver = $el.Version.Split(".")[0 .. 1] -join "."
55+
$ver = $el.Version.Split('.')[0 .. 1] -join '.'
7556
if (!($Groups.ContainsKey($ver))) {
7657
$Groups[$ver] = New-Object System.Collections.ArrayList
7758
$OrderedKeys += $ver
@@ -84,14 +65,14 @@ Describe $CommandName -Tag UnitTests {
8465
}
8566
It "Each Datum has a Version property" {
8667
$DataLength = $IdxRef.Data.Length
87-
$DataWithVersion = ($IdxRef.Data.Version | Where-Object { $PSItem }).Length
68+
$DataWithVersion = ($IdxRef.Data.Version | Where-Object { $_ }).Length
8869
$DataLength | Should -Be $DataWithVersion
8970
}
9071
It "Each version is correctly parsable" {
91-
$Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
72+
$Versions = $IdxRef.Data.Version | Where-Object { $_ }
9273
foreach ($ver in $Versions) {
93-
$splitted = $ver.split(".")
94-
$dots = $ver.split(".").Length - 1
74+
$splitted = $ver.split('.')
75+
$dots = $ver.split('.').Length - 1
9576
if ($dots -ne 2) {
9677
if ($dots[0] -le 15) {
9778
$dots | Should -Be 3
@@ -100,82 +81,82 @@ Describe $CommandName -Tag UnitTests {
10081
}
10182
}
10283
try {
103-
$splitted | ForEach-Object { [convert]::ToInt32($PSItem) }
84+
$splitted | ForEach-Object { [convert]::ToInt32($_) }
10485
} catch {
10586
# I know. But someone can find a method to output a custom message ?
106-
$splitted -join "." | Should -Be "Composed by integers"
87+
$splitted -join '.' | Should -Be "Composed by integers"
10788
}
10889
}
10990
}
11091
It "Versions are ordered, the way versions are ordered" {
111-
$Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
92+
$Versions = $IdxRef.Data.Version | Where-Object { $_ }
11293
$Naturalized = $Versions | ForEach-Object {
113-
$splitted = $PSItem.split(".") | ForEach-Object { [convert]::ToInt32($PSItem) }
114-
"$($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'))"
11596
}
11697
$SortedVersions = $Naturalized | Sort-Object
11798
($SortedVersions -join ",") | Should -Be ($Naturalized -join ",")
11899
}
119100
It "Names are at least 8" {
120-
$Names = $IdxRef.Data.Name | Where-Object { $PSItem }
101+
$Names = $IdxRef.Data.Name | Where-Object { $_ }
121102
$Names.Length | Should -BeGreaterThan 7
122103
}
123104
}
124105
Context "Params mutual exclusion" {
125106
It "Doesn't accept 'Build', 'Kb', 'SqlInstance" {
126-
{ 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
127108
}
128109
It "Doesn't accept 'Build', 'Kb'" {
129-
{ 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
130111
}
131112
It "Doesn't accept 'Build', 'SqlInstance'" {
132-
{ 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
133114
}
134115
It "Doesn't accept 'Build', 'SqlInstance'" {
135-
{ 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
136117
}
137118
It "Doesn't accept 'Build', 'MajorVersion'" {
138-
{ 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
139120
}
140121
It "Doesn't accept 'Build', 'ServicePack'" {
141-
{ 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
142123
}
143124
It "Doesn't accept 'Build', 'CumulativeUpdate'" {
144-
{ 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
145126
}
146127
It "Doesn't accept 'ServicePack' without 'MajorVersion'" {
147-
{ Get-DbaBuild -ServicePack "SP2" -EnableException -ErrorAction Stop } | Should -Throw
128+
{ Get-DbaBuild -ServicePack 'SP2' -EnableException -ErrorAction Stop } | Should -Throw
148129
}
149130
It "Doesn't accept 'CumulativeUpdate' without 'MajorVersion'" {
150-
{ Get-DbaBuild -CumulativeUpdate "CU2" -EnableException -ErrorAction Stop } | Should -Throw
131+
{ Get-DbaBuild -CumulativeUpdate 'CU2' -EnableException -ErrorAction Stop } | Should -Throw
151132
}
152133
}
153134
Context "Passing just -Update works, see #6823" {
154-
It "works with -Update" {
135+
It 'works with -Update' {
155136
function Get-DbaBuildReferenceIndexOnline { }
156137
Mock Get-DbaBuildReferenceIndexOnline -MockWith { } -ModuleName dbatools
157138
Get-DbaBuild -Update -WarningVariable warnings 3>$null
158139
$warnings | Should -BeNullOrEmpty
159140
}
160141
}
161142
Context "Retired KBs" {
162-
It "Handles retired KBs" {
163-
$result = Get-DbaBuild -Build "13.0.5479"
164-
$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'
165146
}
166147
}
167148

168149
Context "Recognizes version 'aliases', see #8915" {
169-
It "works with versions with the minor being either not 0 or 50" {
170-
$result2016 = Get-DbaBuild -Build "13.3.6300"
171-
$result2016.Build | Should -Be "13.3.6300"
172-
$result2016.BuildLevel | Should -Be "13.0.6300"
173-
$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'
174155

175-
$result2008R2 = Get-DbaBuild -Build "10.53.6220"
176-
$result2008R2.Build | Should -Be "10.53.6220"
177-
$result2008R2.BuildLevel | Should -Be "10.50.6220"
178-
$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'
179160
}
180161
}
181162
# These are groups by major release (aka "Name")
@@ -186,31 +167,31 @@ Describe $CommandName -Tag UnitTests {
186167
$Versions[0].Name | Should -BeLike "20*"
187168
}
188169
It "No multiple Names around" {
189-
($Versions.Name | Where-Object { $PSItem }).Count | Should -Be 1
170+
($Versions.Name | Where-Object { $_ }).Count | Should -Be 1
190171
}
191172
# Skip for now bc a prerelease has been added
192173
It -Skip "has one version tagged as RTM" {
193-
($Versions.SP -eq "RTM").Count | Should -Be 1
174+
($Versions.SP -eq 'RTM').Count | Should -Be 1
194175
}
195176
It "SP Property is formatted correctly" {
196-
$Versions.SP | Where-Object { $PSItem } | Should -Match "^RTM$|^SP[\d]+$|^RC"
177+
$Versions.SP | Where-Object { $_ } | Should -Match '^RTM$|^SP[\d]+$|^RC'
197178
}
198179
It "CU Property is formatted correctly" {
199-
$CUMatch = $Versions.CU | Where-Object { $PSItem }
180+
$CUMatch = $Versions.CU | Where-Object { $_ }
200181
if ($CUMatch) {
201-
$CUMatch | Should -Match "^CU[\d]+$"
182+
$CUMatch | Should -Match '^CU[\d]+$'
202183
}
203184
}
204185
It "SPs are ordered correctly" {
205-
$SPs = $Versions.SP | Where-Object { $PSItem }
206-
($SPs | Select-Object -First 1) | Should -BeIn "RTM", "RC"
207-
$ActualSPs = $SPs | Where-Object { $PSItem -match "^SP[\d]+$" }
186+
$SPs = $Versions.SP | Where-Object { $_ }
187+
($SPs | Select-Object -First 1) | Should -BeIn 'RTM', 'RC'
188+
$ActualSPs = $SPs | Where-Object { $_ -match '^SP[\d]+$' }
208189
$OrderedActualSPs = $ActualSPs | Sort-Object
209-
($ActualSPs -join ",") | Should -Be ($OrderedActualSPs -join ",")
190+
($ActualSPs -join ',') | Should -Be ($OrderedActualSPs -join ',')
210191
}
211192
# see https://github.com/dataplat/dbatools/pull/2466
212193
It "KBList has only numbers on it" {
213-
$NotNumbers = $Versions.KBList | Where-Object { $PSItem } | Where-Object { $PSItem -notmatch "^[\d]+$" }
194+
$NotNumbers = $Versions.KBList | Where-Object { $_ } | Where-Object { $_ -notmatch '^[\d]+$' }
214195
if ($NotNumbers.Count -ne 0) {
215196
foreach ($Nn in $NotNumbers) {
216197
$Nn | Should -Be "Composed by integers"
@@ -221,7 +202,7 @@ Describe $CommandName -Tag UnitTests {
221202
}
222203
}
223204

224-
Describe $CommandName -Tag IntegrationTests {
205+
Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
225206
Context "piping and params" {
226207
BeforeAll {
227208
$server1 = Connect-DbaInstance -SqlInstance $TestConfig.instance1
@@ -236,9 +217,7 @@ Describe $CommandName -Tag IntegrationTests {
236217
}
237218
}
238219
Context "Test retrieving version from instances" {
239-
BeforeAll {
240-
$results = Get-DbaBuild -SqlInstance $TestConfig.instance1, $TestConfig.instance2
241-
}
220+
$results = Get-DbaBuild -SqlInstance $TestConfig.instance1, $TestConfig.instance2
242221
It "Should return an exact match" {
243222
$results | Should -Not -BeNullOrEmpty
244223
foreach ($r in $results) {
@@ -258,7 +237,7 @@ Describe $CommandName -Tag IntegrationTests {
258237
$m.KBLevel | Should -BeIn $r.KBLevel
259238
}
260239
}
261-
$spLevel = $r.SPLevel | Where-Object { $PSItem -ne "LATEST" }
240+
$spLevel = $r.SPLevel | Where-Object { $_ -ne 'LATEST' }
262241
$versionMatch = Get-DbaBuild -MajorVersion $r.NameLevel -ServicePack $spLevel -CumulativeUpdate $r.CULevel
263242
$versionMatch | Should -Not -BeNullOrEmpty
264243
foreach ($v in $versionMatch) {
@@ -270,4 +249,4 @@ Describe $CommandName -Tag IntegrationTests {
270249
}
271250
}
272251
}
273-
}
252+
}

0 commit comments

Comments
 (0)