Skip to content

Commit d131087

Browse files
Let's try with Sonnet
1 parent 3a36a65 commit d131087

File tree

3 files changed

+323
-183
lines changed

3 files changed

+323
-183
lines changed

tests/Get-DbaBuild.Tests.ps1

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-DbaBuild",
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 = '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
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
1231
}
1332
}
1433
}
1534

16-
Describe "$CommandName Unit Test" -Tags Unittest {
35+
Describe $CommandName -Tag UnitTests {
1736
BeforeAll {
1837
$ModuleBase = (Get-Module -Name dbatools | Where-Object ModuleBase -NotMatch net).ModuleBase
1938
$idxfile = "$ModuleBase\bin\dbatools-buildref-index.json"
2039
}
2140

22-
Context 'Validate data in json is correct' {
41+
Context "Validate data in json is correct" {
2342
It "the json file is there" {
2443
$result = Test-Path $idxfile
2544
$result | Should -Be $true
@@ -29,7 +48,7 @@ Describe "$CommandName Unit Test" -Tags Unittest {
2948
$IdxRef | Should -BeOfType System.Object
3049
}
3150
}
32-
Context 'Validate LastUpdated property' {
51+
Context "Validate LastUpdated property" {
3352
BeforeAll {
3453
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
3554
}
@@ -46,13 +65,13 @@ Describe "$CommandName Unit Test" -Tags Unittest {
4665
$lastupdate | Should -BeLessThan (Get-Date)
4766
}
4867
}
49-
Context 'Validate Data property' {
68+
Context "Validate Data property" {
5069
BeforeAll {
5170
$IdxRef = Get-Content $idxfile -Raw | ConvertFrom-Json
5271
$Groups = @{ }
5372
$OrderedKeys = @()
5473
foreach ($el in $IdxRef.Data) {
55-
$ver = $el.Version.Split('.')[0 .. 1] -join '.'
74+
$ver = $el.Version.Split(".")[0 .. 1] -join "."
5675
if (!($Groups.ContainsKey($ver))) {
5776
$Groups[$ver] = New-Object System.Collections.ArrayList
5877
$OrderedKeys += $ver
@@ -65,14 +84,14 @@ Describe "$CommandName Unit Test" -Tags Unittest {
6584
}
6685
It "Each Datum has a Version property" {
6786
$DataLength = $IdxRef.Data.Length
68-
$DataWithVersion = ($IdxRef.Data.Version | Where-Object { $_ }).Length
87+
$DataWithVersion = ($IdxRef.Data.Version | Where-Object { $PSItem }).Length
6988
$DataLength | Should -Be $DataWithVersion
7089
}
7190
It "Each version is correctly parsable" {
72-
$Versions = $IdxRef.Data.Version | Where-Object { $_ }
91+
$Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
7392
foreach ($ver in $Versions) {
74-
$splitted = $ver.split('.')
75-
$dots = $ver.split('.').Length - 1
93+
$splitted = $ver.split(".")
94+
$dots = $ver.split(".").Length - 1
7695
if ($dots -ne 2) {
7796
if ($dots[0] -le 15) {
7897
$dots | Should -Be 3
@@ -81,82 +100,82 @@ Describe "$CommandName Unit Test" -Tags Unittest {
81100
}
82101
}
83102
try {
84-
$splitted | ForEach-Object { [convert]::ToInt32($_) }
103+
$splitted | ForEach-Object { [convert]::ToInt32($PSItem) }
85104
} catch {
86105
# I know. But someone can find a method to output a custom message ?
87-
$splitted -join '.' | Should -Be "Composed by integers"
106+
$splitted -join "." | Should -Be "Composed by integers"
88107
}
89108
}
90109
}
91110
It "Versions are ordered, the way versions are ordered" {
92-
$Versions = $IdxRef.Data.Version | Where-Object { $_ }
111+
$Versions = $IdxRef.Data.Version | Where-Object { $PSItem }
93112
$Naturalized = $Versions | ForEach-Object {
94-
$splitted = $_.split('.') | ForEach-Object { [convert]::ToInt32($_) }
95-
"$($splitted[0].toString('00'))$($splitted[1].toString('00'))$($splitted[2].toString('0000'))"
113+
$splitted = $PSItem.split(".") | ForEach-Object { [convert]::ToInt32($PSItem) }
114+
"$($splitted[0].toString("00"))$($splitted[1].toString("00"))$($splitted[2].toString("0000"))"
96115
}
97116
$SortedVersions = $Naturalized | Sort-Object
98117
($SortedVersions -join ",") | Should -Be ($Naturalized -join ",")
99118
}
100119
It "Names are at least 8" {
101-
$Names = $IdxRef.Data.Name | Where-Object { $_ }
120+
$Names = $IdxRef.Data.Name | Where-Object { $PSItem }
102121
$Names.Length | Should -BeGreaterThan 7
103122
}
104123
}
105124
Context "Params mutual exclusion" {
106125
It "Doesn't accept 'Build', 'Kb', 'SqlInstance" {
107-
{ Get-DbaBuild -Build '10.0.1600' -Kb '4052908' -SqlInstance 'localhost' -EnableException -ErrorAction Stop } | Should -Throw
126+
{ Get-DbaBuild -Build "10.0.1600" -Kb "4052908" -SqlInstance "localhost" -EnableException -ErrorAction Stop } | Should -Throw
108127
}
109128
It "Doesn't accept 'Build', 'Kb'" {
110-
{ Get-DbaBuild -Build '10.0.1600' -Kb '4052908' -EnableException -ErrorAction Stop } | Should -Throw
129+
{ Get-DbaBuild -Build "10.0.1600" -Kb "4052908" -EnableException -ErrorAction Stop } | Should -Throw
111130
}
112131
It "Doesn't accept 'Build', 'SqlInstance'" {
113-
{ Get-DbaBuild -Build '10.0.1600' -SqlInstance 'localhost' -EnableException -ErrorAction Stop } | Should -Throw
132+
{ Get-DbaBuild -Build "10.0.1600" -SqlInstance "localhost" -EnableException -ErrorAction Stop } | Should -Throw
114133
}
115134
It "Doesn't accept 'Build', 'SqlInstance'" {
116-
{ Get-DbaBuild -Build '10.0.1600' -SqlInstance 'localhost' -EnableException -ErrorAction Stop } | Should -Throw
135+
{ Get-DbaBuild -Build "10.0.1600" -SqlInstance "localhost" -EnableException -ErrorAction Stop } | Should -Throw
117136
}
118137
It "Doesn't accept 'Build', 'MajorVersion'" {
119-
{ Get-DbaBuild -Build '10.0.1600' -MajorVersion '2016' -EnableException -ErrorAction Stop } | Should -Throw
138+
{ Get-DbaBuild -Build "10.0.1600" -MajorVersion "2016" -EnableException -ErrorAction Stop } | Should -Throw
120139
}
121140
It "Doesn't accept 'Build', 'ServicePack'" {
122-
{ Get-DbaBuild -Build '10.0.1600' -ServicePack 'SP2' -EnableException -ErrorAction Stop } | Should -Throw
141+
{ Get-DbaBuild -Build "10.0.1600" -ServicePack "SP2" -EnableException -ErrorAction Stop } | Should -Throw
123142
}
124143
It "Doesn't accept 'Build', 'CumulativeUpdate'" {
125-
{ Get-DbaBuild -Build '10.0.1600' -CumulativeUpdate 'CU2' -EnableException -ErrorAction Stop } | Should -Throw
144+
{ Get-DbaBuild -Build "10.0.1600" -CumulativeUpdate "CU2" -EnableException -ErrorAction Stop } | Should -Throw
126145
}
127146
It "Doesn't accept 'ServicePack' without 'MajorVersion'" {
128-
{ Get-DbaBuild -ServicePack 'SP2' -EnableException -ErrorAction Stop } | Should -Throw
147+
{ Get-DbaBuild -ServicePack "SP2" -EnableException -ErrorAction Stop } | Should -Throw
129148
}
130149
It "Doesn't accept 'CumulativeUpdate' without 'MajorVersion'" {
131-
{ Get-DbaBuild -CumulativeUpdate 'CU2' -EnableException -ErrorAction Stop } | Should -Throw
150+
{ Get-DbaBuild -CumulativeUpdate "CU2" -EnableException -ErrorAction Stop } | Should -Throw
132151
}
133152
}
134153
Context "Passing just -Update works, see #6823" {
135-
It 'works with -Update' {
154+
It "works with -Update" {
136155
function Get-DbaBuildReferenceIndexOnline { }
137156
Mock Get-DbaBuildReferenceIndexOnline -MockWith { } -ModuleName dbatools
138157
Get-DbaBuild -Update -WarningVariable warnings 3>$null
139158
$warnings | Should -BeNullOrEmpty
140159
}
141160
}
142161
Context "Retired KBs" {
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'
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"
146165
}
147166
}
148167

149168
Context "Recognizes version 'aliases', see #8915" {
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'
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"
155174

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'
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"
160179
}
161180
}
162181
# These are groups by major release (aka "Name")
@@ -167,31 +186,31 @@ Describe "$CommandName Unit Test" -Tags Unittest {
167186
$Versions[0].Name | Should -BeLike "20*"
168187
}
169188
It "No multiple Names around" {
170-
($Versions.Name | Where-Object { $_ }).Count | Should -Be 1
189+
($Versions.Name | Where-Object { $PSItem }).Count | Should -Be 1
171190
}
172191
# Skip for now bc a prerelease has been added
173192
It -Skip "has one version tagged as RTM" {
174-
($Versions.SP -eq 'RTM').Count | Should -Be 1
193+
($Versions.SP -eq "RTM").Count | Should -Be 1
175194
}
176195
It "SP Property is formatted correctly" {
177-
$Versions.SP | Where-Object { $_ } | Should -Match '^RTM$|^SP[\d]+$|^RC'
196+
$Versions.SP | Where-Object { $PSItem } | Should -Match "^RTM$|^SP[\d]+$|^RC"
178197
}
179198
It "CU Property is formatted correctly" {
180-
$CUMatch = $Versions.CU | Where-Object { $_ }
199+
$CUMatch = $Versions.CU | Where-Object { $PSItem }
181200
if ($CUMatch) {
182-
$CUMatch | Should -Match '^CU[\d]+$'
201+
$CUMatch | Should -Match "^CU[\d]+$"
183202
}
184203
}
185204
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]+$' }
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]+$" }
189208
$OrderedActualSPs = $ActualSPs | Sort-Object
190-
($ActualSPs -join ',') | Should -Be ($OrderedActualSPs -join ',')
209+
($ActualSPs -join ",") | Should -Be ($OrderedActualSPs -join ",")
191210
}
192211
# see https://github.com/dataplat/dbatools/pull/2466
193212
It "KBList has only numbers on it" {
194-
$NotNumbers = $Versions.KBList | Where-Object { $_ } | Where-Object { $_ -notmatch '^[\d]+$' }
213+
$NotNumbers = $Versions.KBList | Where-Object { $PSItem } | Where-Object { $PSItem -notmatch "^[\d]+$" }
195214
if ($NotNumbers.Count -ne 0) {
196215
foreach ($Nn in $NotNumbers) {
197216
$Nn | Should -Be "Composed by integers"
@@ -202,7 +221,7 @@ Describe "$CommandName Unit Test" -Tags Unittest {
202221
}
203222
}
204223

205-
Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
224+
Describe $CommandName -Tag IntegrationTests {
206225
Context "piping and params" {
207226
BeforeAll {
208227
$server1 = Connect-DbaInstance -SqlInstance $TestConfig.instance1
@@ -217,7 +236,9 @@ Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
217236
}
218237
}
219238
Context "Test retrieving version from instances" {
220-
$results = Get-DbaBuild -SqlInstance $TestConfig.instance1, $TestConfig.instance2
239+
BeforeAll {
240+
$results = Get-DbaBuild -SqlInstance $TestConfig.instance1, $TestConfig.instance2
241+
}
221242
It "Should return an exact match" {
222243
$results | Should -Not -BeNullOrEmpty
223244
foreach ($r in $results) {
@@ -237,7 +258,7 @@ Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
237258
$m.KBLevel | Should -BeIn $r.KBLevel
238259
}
239260
}
240-
$spLevel = $r.SPLevel | Where-Object { $_ -ne 'LATEST' }
261+
$spLevel = $r.SPLevel | Where-Object { $PSItem -ne "LATEST" }
241262
$versionMatch = Get-DbaBuild -MajorVersion $r.NameLevel -ServicePack $spLevel -CumulativeUpdate $r.CULevel
242263
$versionMatch | Should -Not -BeNullOrEmpty
243264
foreach ($v in $versionMatch) {
@@ -249,4 +270,4 @@ Describe "$commandname Integration Tests" -Tags 'IntegrationTests' {
249270
}
250271
}
251272
}
252-
}
273+
}

0 commit comments

Comments
 (0)