Skip to content
Merged
12 changes: 8 additions & 4 deletions private/functions/Get-ObjectNameParts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,17 @@ function Get-ObjectNameParts {
}

if ($fixSchema) {
$dbName = $dbName.Replace($fixSchema, '')
if ($dbName) {
$dbName = $dbName.Replace($fixSchema, '')
}
if ($schema -eq $fixSchema) {
$schema = $null
} else {
$schema = $dbName.Replace($fixSchema, '')
} elseif ($schema) {
$schema = $schema.Replace($fixSchema, '')
}
if ($name) {
$name = $name.Replace($fixSchema, '')
}
$name = $name.Replace($fixSchema, '')
}

$fqtns = [PSCustomObject] @{
Expand Down
2 changes: 1 addition & 1 deletion public/Export-DbaBinaryFile.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function Export-DbaBinaryFile {
<#
.SYNOPSIS
Extracts binary data from SQL Server tables and writes it to physical files
Extracts binary data from SQL Server tables and writes it to physical files.

.DESCRIPTION
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.
Expand Down
86 changes: 48 additions & 38 deletions tests/Get-ObjectNameParts.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,64 +1,74 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig
. "$PSScriptRoot\..\private\functions\Get-DirectoryRestoreFile.ps1"
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-ObjectNameParts",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'ObjectName'
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
. "$PSScriptRoot\..\private\functions\Get-ObjectNameParts.ps1"

Describe $CommandName -Tag UnitTests {
Context 'Parameter validation' {
BeforeAll {
$command = Get-Command $CommandName
$hasParameters = $command.Parameters.Keys | Where-Object { $PSItem -notin ('whatif', 'confirm') }
$expectedParameters = @(
'ObjectName'
)
}

It 'Should have the expected parameters' {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$CommandName Integration Tests" -Tag 'IntegrationTests' {
Context "Test one part names" {
It "Should return correct parts" {
Describe $CommandName -Tag IntegrationTests {
Context 'Test one part names' {
It 'Should return correct parts' {
$objectName = 'table1', '[table2]', '[tab..le3]', '[table]]x4]', '[table5]]]'
$table = 'table1', 'table2', 'tab..le3', 'table]]x4', 'table5]]'
for ($i = 0; $i -lt $input.Count; $i++) {
$table = 'table1', 'table2', 'tab..le3', 'table]x4', 'table5]'
for ($i = 0; $i -lt $objectName.Count; $i++) {
$result = Get-ObjectNameParts -ObjectName $objectName[$i]
$result.Parsed | Should Be $true
$result.Database | Should Be $null
$result.Schema | Should Be $null
$result.Name | Should Be $table[$i]
$result.Parsed | Should -Be $true
$result.Database | Should -BeNull
$result.Schema | Should -BeNull
$result.Name | Should -Be $table[$i]
}
}
}
Context "Test two part names" {
It "Should return correct parts" {
$objectName = 'schema1.table1', '[sche..ma2].[table2]', 'schema3.[tab..le3]', '[schema4].[table]]x4]', 'schema5.[table5]]]'
$table = 'table1', 'table2', 'tab..le3', 'table]]x4', 'table5]]'
Context 'Test two part names' {
It 'Should return correct parts' {
$objectName = 'schema1.table1', '[sche..ma2].[table2]', '[sche ma3].[tab..le3]', '[schema4].[table]]x4]', 'schema5.[table5]]]'
$table = 'table1', 'table2', 'tab..le3', 'table]x4', 'table5]'
$schema = 'schema1', 'sche..ma2', 'sche ma3', 'schema4', 'schema5'
for ($i = 0; $i -lt $input.Count; $i++) {
for ($i = 0; $i -lt $objectName.Count; $i++) {
$result = Get-ObjectNameParts -ObjectName $objectName[$i]
$result.Parsed | Should Be $true
$result.Database | Should Be $null
$result.Schema | Should Be $schema[$i]
$result.Name | Should Be $table[$i]
$result.Parsed | Should -Be $true
$result.Database | Should -BeNull
$result.Schema | Should -Be $schema[$i]
$result.Name | Should -Be $table[$i]
}
}
}
Context "Test three part names" {
It "Should return correct parts" {
Context 'Test three part names' {
It 'Should return correct parts' {
$objectName = 'database1.schema1.table1', 'database2..table2', 'database3..[tab..le3]', 'db4.[sche..ma4].table4'
$table = 'table1', 'table2', 'tab..le3', 'table4'
$schema = 'schema1', $null, $null, 'sche..ma4'
$database = 'database1', 'database2', 'database3', 'db4'
for ($i = 0; $i -lt $input.Count; $i++) {
for ($i = 0; $i -lt $objectName.Count; $i++) {
$result = Get-ObjectNameParts -ObjectName $objectName[$i]
$result.Parsed | Should Be $true
$result.Database | Should Be $database[$i]
$result.Schema | Should Be $schema[$i]
$result.Name | Should Be $table[$i]
$result.Parsed | Should -Be $true
$result.Database | Should -Be $database[$i]
$result.Schema | Should -Be $schema[$i]
$result.Name | Should -Be $table[$i]
}
}
}
Context "Test wrong names" {
It "Should not return parts for 'part1.part2.part3.part4'" {
(Get-ObjectNameParts -ObjectName 'part1.part2.part3.part4').Parsed | Should Be $false
Context 'Test wrong names' {
It 'Should not return parts for ''part1.part2.part3.part4''' {
(Get-ObjectNameParts -ObjectName 'part1.part2.part3.part4').Parsed | Should -Be $false
}
}
}
20 changes: 14 additions & 6 deletions tests/InModule.Commands.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "InModule.Commands",
$PSDefaultParameterValues = $TestConfig.Defaults
)

Describe $CommandName -Tag IntegrationTests {
# The original test used Get-TestConfig, but that is now handled by the test runner
# and the results are available in $TestConfig.
# The original test also dynamically set $CommandName, which is now static.

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