tests/Get-DbaRepPublication.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
. "$PSScriptRoot\..\internal\functions\Connect-SqlInstance.ps1"

Describe "$commandname Unit Tests" -Tag 'UnitTests' {

    InModuleScope dbatools {

        Context "Parameter Validation" {

            [object[]]$params = (Get-ChildItem function:\Get-DbaRepPublication).Parameters.Keys
            $knownParameters = 'SqlInstance', 'Database', 'SqlCredential', 'PublicationType', 'EnableException'
            $paramCount = $knownParameters.Count
            $defaultParamCount = $params.Count - $paramCount

            It "Should contain our specific parameters" {
                ( (Compare-Object -ReferenceObject $knownParameters -DifferenceObject $params -IncludeEqual | Where-Object SideIndicator -eq "==").Count ) | Should Be $paramCount
            }

            It "Should only contain $paramCount parameters" {
                $params.Count - $defaultParamCount | Should Be $paramCount
            }

        }

        Context "Code Validation" {

            Mock Connect-ReplicationDB -MockWith {
                [object]@{
                    Name = 'TestDB'
                    TransPublications = @{
                        Name = 'TestDB_pub'
                        Type = 'Transactional'
                    }
                    MergePublications = @{}
                }
            }

            Mock Connect-SqlInstance -MockWith {
                [object]@{
                    Name      = "MockServerName"
                    ComputerName   = 'MockComputerName'
                    Databases = @{
                                    Name = 'TestDB'
                                    #state
                                    #status
                                    ID = 5
                                    ReplicationOptions = 'Published'
                                }
                    ConnectionContext = @{
                                           SqlConnectionObject = 'FakeConnectionContext'
                                        }
                }
            }

            It "Honors the SQLInstance parameter" {
                $Results = Get-DbaRepPublication -SqlInstance MockServerName
                $Results.Server | Should Be "MockServerName"
            }

            It "Honors the Database parameter" {
                $Results = Get-DbaRepPublication -SqlInstance MockServerName -Database TestDB
                $Results.Database | Should Be "TestDB"
            }

            It "Honors the PublicationType parameter" {

                Mock Connect-ReplicationDB -MockWith {
                    [object]@{
                        Name = 'TestDB'
                        TransPublications = @{
                            Name = 'TestDB_pub'
                            Type = 'Snapshot'
                        }
                        MergePublications = @{}
                    }
                }

                $Results = Get-DbaRepPublication -SqlInstance MockServerName -Database TestDB -PublicationType Snapshot
                $Results.PublicationType | Should Be "Snapshot"
            }

            It "Stops if the SqlInstance does not exist" {
            
                Mock Connect-SqlInstance -MockWith { Throw }
        
                { Get-DbaRepPublication -sqlinstance MockServerName -EnableException} | should Throw
                
            }

            It "Stops if validate set for PublicationType is not met" {

                { Get-DbaRepPublication -SqlInstance MockServerName -PublicationType NotAPubType } | should Throw

            }
        }
    }
}