tests/Select-DbaObject.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
$commandname = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$commandname Unit Tests" -Tag "UnitTests" {
    $global:object = [PSCustomObject]@{
        Foo  = 42
        Bar  = 18
        Tara = 21
    }
    
    $global:object2 = [PSCustomObject]@{
        Foo = 42000
        Bar = 23
    }
    
    $global:list = @()
    $global:list += $object
    $global:list += [PSCustomObject]@{
        Foo  = 23
        Bar  = 88
        Tara = 28
    }
    
    It "renames Bar to Bar2" {
        ($object | Select-DbaObject -Property 'Foo', 'Bar as Bar2').PSObject.Properties.Name | Should -Be 'Foo', 'Bar2'
    }
    
    It "changes Bar to string" {
        ($object | Select-DbaObject -Property 'Bar to string').Bar.GetType().FullName | Should -Be 'System.String'
    }
    
    it "converts numbers to sizes" {
        ($object2 | Select-DbaObject -Property 'Foo size KB:1').Foo | Should -Be 41
        ($object2 | Select-DbaObject -Property 'Foo size KB:1:1').Foo | Should -Be "41 KB"
    }
    
    it "picks values from other variables" {
        ($object2 | Select-DbaObject -Property 'Tara from object').Tara | Should -Be 21
    }
    
    it "picks values from the properties of the right object in a list" {
        ($object2 | Select-DbaObject -Property 'Tara from List where Foo = Bar').Tara | Should -Be 28
    }
    
    It "sets the correct properties to show in whitelist mode" {
        $obj = [PSCustomObject]@{ Foo = "Bar"; Bar = 42; Right = "Left" }
        $null = $obj | Select-DbaObject -ShowProperty Foo, Bar
        $obj.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | Should -Be 'Foo', 'Bar'
    }
    
    It "sets the correct properties to show in blacklist mode" {
        $obj = [PSCustomObject]@{ Foo = "Bar"; Bar = 42; Right = "Left" }
        $null = $obj | Select-DbaObject -ShowExcludeProperty Foo
        $obj.PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames | Should -Be 'Bar', 'Right'
    }
    
    It "sets the correct typename" {
        $obj = [PSCustomObject]@{ Foo = "Bar"; Bar = 42; Right = "Left" }
        $null = $obj | Select-DbaObject -TypeName 'Foo.Bar'
        $obj.PSObject.TypeNames[0] | Should -Be 'Foo.Bar'
    }
    
    It "adds properties without harming the original object when used with -KeepInputObject" {
        $item = Get-Item "$PSScriptRoot\Select-DbaObject.Tests.ps1"
        $modItem = $item | Select-DbaObject "Length as Size size KB:1:1" -KeepInputObject
        $modItem.GetType().FullName | Should -Be 'System.IO.FileInfo'
        $modItem.Size | Should -BeLike '* KB'
    }
}