tests/Remove-DbaClientAlias.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
$commandname = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
    BeforeAll {
        $null = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias1 -Verbose:$false
        $null = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias2 -Verbose:$false
        $null = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias3 -Verbose:$false
        $null = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias4 -Verbose:$false
        $null = New-DbaClientAlias -ServerName sql2016 -Alias dbatoolscialias5 -Verbose:$false
    }

    InModuleScope 'dbatools' {
        Context "removes the alias" {
            $aliases = Get-DbaClientAlias
            It "alias exists" {
                $aliases.AliasName -contains 'dbatoolscialias1' | Should Be $true
            }

            $null = Remove-DbaClientAlias -Alias dbatoolscialias1 -Verbose:$false

            $aliases = Get-DbaClientAlias
            It "alias is not included in results" {
                $aliases.AliasName -notcontains 'dbatoolscialias1' | Should Be $true
            }
        }

        Context "removes an array of aliases" {
            $testCases = @(
                @{'Alias' = 'dbatoolscialias2'},
                @{'Alias' = 'dbatoolscialias3'}
            )

            $aliases = Get-DbaClientAlias
            It "alias <Alias> exists" -TestCases $testCases {
                param ($Alias)

                $aliases.AliasName -contains $Alias | Should Be $true
            }

            $null = Remove-DbaClientAlias -Alias @('dbatoolscialias2', 'dbatoolscialias3')

            $aliases = Get-DbaClientAlias
            It "alias <Alias> was removed" -TestCases $testCases {
                param ($Alias)

                $aliases.AliasName -notcontains $Alias | Should Be $true
            }
        }

        Context "removes an alias through the pipeline" {
            $aliases = Get-DbaClientAlias
            It "alias exists" {
                $aliases.AliasName -contains 'dbatoolscialias4' | Should Be $true
            }

            $null = Get-DbaClientAlias | Where-Object { $_.AliasName -eq 'dbatoolscialias4' } | Remove-DbaClientAlias
            $aliases = Get-DbaClientAlias
            It "alias was removed" {
                $aliases.AliasName -notcontains 'dbatoolscialias4' | Should Be $true
            }
        }

        Context "SQL client is not installed" {
            Mock -CommandName 'Test-Path' -MockWith {
                return $false
            }

            $defaultParamValues = $PSDefaultParameterValues
            $PSDefaultParameterValues=@{"*:WarningVariable"="+buffer"}

            $null = Remove-DbaClientAlias -Alias 'dbatoolscialias5' -WarningAction 'SilentlyContinue'

            $PSDefaultParameterValues = $defaultParamValues

            It "warns that the key doesn't exist" {
                $buffer.Count -ge 4 | Should -Be $true
            }
        }
    }
}