Skip to content

Commit 188366b

Browse files
Complete Pester 5 migration - Migrate final 4 meta-tests and fix duplicate test execution (#9937)
1 parent 1de4713 commit 188366b

File tree

9 files changed

+347
-563
lines changed

9 files changed

+347
-563
lines changed

private/functions/Get-ObjectNameParts.ps1

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,17 @@ function Get-ObjectNameParts {
110110
}
111111

112112
if ($fixSchema) {
113-
$dbName = $dbName.Replace($fixSchema, '')
113+
if ($dbName) {
114+
$dbName = $dbName.Replace($fixSchema, '')
115+
}
114116
if ($schema -eq $fixSchema) {
115117
$schema = $null
116-
} else {
117-
$schema = $dbName.Replace($fixSchema, '')
118+
} elseif ($schema) {
119+
$schema = $schema.Replace($fixSchema, '')
120+
}
121+
if ($name) {
122+
$name = $name.Replace($fixSchema, '')
118123
}
119-
$name = $name.Replace($fixSchema, '')
120124
}
121125

122126
$fqtns = [PSCustomObject] @{

public/Export-DbaBinaryFile.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function Export-DbaBinaryFile {
22
<#
33
.SYNOPSIS
4-
Extracts binary data from SQL Server tables and writes it to physical files
4+
Extracts binary data from SQL Server tables and writes it to physical files.
55
66
.DESCRIPTION
77
Retrieves binary data stored in SQL Server tables and writes it as files to the filesystem. This is useful for extracting documents, images, or other files that have been stored in database columns using binary, varbinary, or image datatypes.
Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,74 @@
1-
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
2-
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
3-
$global:TestConfig = Get-TestConfig
4-
. "$PSScriptRoot\..\private\functions\Get-DirectoryRestoreFile.ps1"
1+
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
2+
param(
3+
$ModuleName = "dbatools",
4+
$CommandName = "Get-ObjectNameParts",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
57

6-
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
7-
Context "Validate parameters" {
8-
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
9-
[object[]]$knownParameters = 'ObjectName'
10-
It "Should only contain our specific parameters" {
11-
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
8+
. "$PSScriptRoot\..\private\functions\Get-ObjectNameParts.ps1"
9+
10+
Describe $CommandName -Tag UnitTests {
11+
Context 'Parameter validation' {
12+
BeforeAll {
13+
$command = Get-Command $CommandName
14+
$hasParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin ('whatif', 'confirm') }
15+
$expectedParameters = @(
16+
'ObjectName'
17+
)
18+
}
19+
20+
It 'Should have the expected parameters' {
21+
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
1222
}
1323
}
1424
}
1525

16-
Describe "$CommandName Integration Tests" -Tag 'IntegrationTests' {
17-
Context "Test one part names" {
18-
It "Should return correct parts" {
26+
Describe $CommandName -Tag IntegrationTests {
27+
Context 'Test one part names' {
28+
It 'Should return correct parts' {
1929
$objectName = 'table1', '[table2]', '[tab..le3]', '[table]]x4]', '[table5]]]'
20-
$table = 'table1', 'table2', 'tab..le3', 'table]]x4', 'table5]]'
21-
for ($i = 0; $i -lt $input.Count; $i++) {
30+
$table = 'table1', 'table2', 'tab..le3', 'table]x4', 'table5]'
31+
for ($i = 0; $i -lt $objectName.Count; $i++) {
2232
$result = Get-ObjectNameParts -ObjectName $objectName[$i]
23-
$result.Parsed | Should Be $true
24-
$result.Database | Should Be $null
25-
$result.Schema | Should Be $null
26-
$result.Name | Should Be $table[$i]
33+
$result.Parsed | Should -Be $true
34+
$result.Database | Should -BeNull
35+
$result.Schema | Should -BeNull
36+
$result.Name | Should -Be $table[$i]
2737
}
2838
}
2939
}
30-
Context "Test two part names" {
31-
It "Should return correct parts" {
32-
$objectName = 'schema1.table1', '[sche..ma2].[table2]', 'schema3.[tab..le3]', '[schema4].[table]]x4]', 'schema5.[table5]]]'
33-
$table = 'table1', 'table2', 'tab..le3', 'table]]x4', 'table5]]'
40+
Context 'Test two part names' {
41+
It 'Should return correct parts' {
42+
$objectName = 'schema1.table1', '[sche..ma2].[table2]', '[sche ma3].[tab..le3]', '[schema4].[table]]x4]', 'schema5.[table5]]]'
43+
$table = 'table1', 'table2', 'tab..le3', 'table]x4', 'table5]'
3444
$schema = 'schema1', 'sche..ma2', 'sche ma3', 'schema4', 'schema5'
35-
for ($i = 0; $i -lt $input.Count; $i++) {
45+
for ($i = 0; $i -lt $objectName.Count; $i++) {
3646
$result = Get-ObjectNameParts -ObjectName $objectName[$i]
37-
$result.Parsed | Should Be $true
38-
$result.Database | Should Be $null
39-
$result.Schema | Should Be $schema[$i]
40-
$result.Name | Should Be $table[$i]
47+
$result.Parsed | Should -Be $true
48+
$result.Database | Should -BeNull
49+
$result.Schema | Should -Be $schema[$i]
50+
$result.Name | Should -Be $table[$i]
4151
}
4252
}
4353
}
44-
Context "Test three part names" {
45-
It "Should return correct parts" {
54+
Context 'Test three part names' {
55+
It 'Should return correct parts' {
4656
$objectName = 'database1.schema1.table1', 'database2..table2', 'database3..[tab..le3]', 'db4.[sche..ma4].table4'
4757
$table = 'table1', 'table2', 'tab..le3', 'table4'
4858
$schema = 'schema1', $null, $null, 'sche..ma4'
4959
$database = 'database1', 'database2', 'database3', 'db4'
50-
for ($i = 0; $i -lt $input.Count; $i++) {
60+
for ($i = 0; $i -lt $objectName.Count; $i++) {
5161
$result = Get-ObjectNameParts -ObjectName $objectName[$i]
52-
$result.Parsed | Should Be $true
53-
$result.Database | Should Be $database[$i]
54-
$result.Schema | Should Be $schema[$i]
55-
$result.Name | Should Be $table[$i]
62+
$result.Parsed | Should -Be $true
63+
$result.Database | Should -Be $database[$i]
64+
$result.Schema | Should -Be $schema[$i]
65+
$result.Name | Should -Be $table[$i]
5666
}
5767
}
5868
}
59-
Context "Test wrong names" {
60-
It "Should not return parts for 'part1.part2.part3.part4'" {
61-
(Get-ObjectNameParts -ObjectName 'part1.part2.part3.part4').Parsed | Should Be $false
69+
Context 'Test wrong names' {
70+
It 'Should not return parts for ''part1.part2.part3.part4''' {
71+
(Get-ObjectNameParts -ObjectName 'part1.part2.part3.part4').Parsed | Should -Be $false
6272
}
6373
}
6474
}

tests/InModule.Commands.Tests.ps1

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
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 = "InModule.Commands",
5+
$PSDefaultParameterValues = $TestConfig.Defaults
6+
)
7+
8+
Describe $CommandName -Tag IntegrationTests {
9+
# The original test used Get-TestConfig, but that is now handled by the test runner
10+
# and the results are available in $TestConfig.
11+
# The original test also dynamically set $CommandName, which is now static.
412

5-
Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
613
Context "duplicate commands are not added" {
714
It "only indexes one instance per command" {
815
# this no longer works in PS 5.1, no idea why, maybe it doesn't like the new test for core or desktop
916
# $commandlist = Import-PowerShellDataFile -Path '$PSScriptRoot\..\dbatools.psd1'
10-
$commandlist = Invoke-Expression (Get-Content '$PSScriptRoot\..\dbatools.psd1' -Raw)
17+
$psd1Path = Join-Path $PSScriptRoot '..\dbatools.psd1'
18+
$commandlist = Invoke-Expression (Get-Content $psd1Path -Raw)
1119
$dupes = $commandlist.FunctionsToExport | Group-Object | Where-Object Count -gt 1
12-
$dupes.Name | Should -be $null
20+
$dupes | Should -BeNullOrEmpty
1321
}
1422
}
1523
}

0 commit comments

Comments
 (0)