Skip to content

Commit

Permalink
Update Tests to include unit test. Fixing documentation of Parameters (
Browse files Browse the repository at this point in the history
  • Loading branch information
sqllensman authored and potatoqualitee committed Nov 2, 2018
1 parent 3266f21 commit 81d6ef0
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 58 deletions.
6 changes: 0 additions & 6 deletions functions/Add-DbaCmsRegServerGroup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ function Add-DbaCmsRegServerGroup {
.PARAMETER InputObject
Allows results from Get-DbaCmsRegServerGroup to be piped in
.PARAMETER IncludeRegisteredServers
Create the registered servers within the group, too
.PARAMETER InputObject
Allows results from Get-DbaCmsRegServerGroup to be piped in
.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.
Expand Down
3 changes: 0 additions & 3 deletions functions/Export-DbaCmsRegServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ function Export-DbaCmsRegServer {
.PARAMETER SqlCredential
Login to the target instance using alternative credentials. Windows and SQL Authentication supported. Accepts credential objects (Get-Credential)
.PARAMETER Group
Exports a specific group.
.PARAMETER CredentialPersistenceType
Used to specify how the login and passwords are persisted. Valid values include None, PersistLoginName and PersistLoginNameAndPassword.
Expand Down
24 changes: 24 additions & 0 deletions tests/Add-DbaCmsRegServer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tags "UnitTests" {
Context "Validate parameters" {
$knownParameters = 'SqlInstance', 'SqlCredential', 'ServerName', 'Name', 'Description', 'Group', 'InputObject', 'EnableException'
$SupportShouldProcess = $true
$paramCount = $knownParameters.Count
if ($SupportShouldProcess) {
$defaultParamCount = 13
}
else {
$defaultParamCount = 11
}
$command = Get-Command -Name $CommandName
[object[]]$params = $command.Parameters.Keys

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
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Context "Setup" {
BeforeAll {
Expand Down
26 changes: 25 additions & 1 deletion tests/Add-DbaCmsRegServerGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tags "UnitTests" {
Context "Validate parameters" {
$knownParameters = 'SqlInstance', 'SqlCredential', 'Name', 'Description', 'Group', 'InputObject', 'EnableException'
$SupportShouldProcess = $true
$paramCount = $knownParameters.Count
if ($SupportShouldProcess) {
$defaultParamCount = 13
}
else {
$defaultParamCount = 11
}
$command = Get-Command -Name $CommandName
[object[]]$params = $command.Parameters.Keys

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
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Context "Setup" {
BeforeAll {
Expand All @@ -12,7 +36,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
AfterAll {
Get-DbaCmsRegServerGroup -SqlInstance $script:instance1 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServerGroup -Confirm:$false
}

It "adds a registered server group" {
$results = Add-DbaCmsRegServerGroup -SqlInstance $script:instance1 -Name $group
$results.Name | Should -Be $group
Expand Down
26 changes: 25 additions & 1 deletion tests/Add-DbaDbMirrorMonitor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@ $commandname = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tags "UnitTests" {
Context "Validate parameters" {
$knownParameters = 'SqlInstance', 'SqlCredential', 'EnableException'
$SupportShouldProcess = $true
$paramCount = $knownParameters.Count
if ($SupportShouldProcess) {
$defaultParamCount = 13
}
else {
$defaultParamCount = 11
}
$command = Get-Command -Name $CommandName
[object[]]$params = $command.Parameters.Keys

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
}
}
}

Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$null = Remove-DbaDbMirrorMonitor -SqlInstance $script:instance2 -WarningAction SilentlyContinue
}

It "adds the mirror monitor" {
$results = Add-DbaDbMirrorMonitor -SqlInstance $script:instance2 -WarningAction SilentlyContinue
$results.MonitorStatus | Should -Be 'Added'
Expand Down
40 changes: 32 additions & 8 deletions tests/Export-DbaCmsRegServer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,73 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tags "UnitTests" {
Context "Validate parameters" {
$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'Path', 'CredentialPersistenceType', 'EnableException'
$SupportShouldProcess = $false
$paramCount = $knownParameters.Count
if ($SupportShouldProcess) {
$defaultParamCount = 13
}
else {
$defaultParamCount = 11
}
$command = Get-Command -Name $CommandName
[object[]]$params = $command.Parameters.Keys

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
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$srvName = "dbatoolsci-server1"
$group = "dbatoolsci-group1"
$regSrvName = "dbatoolsci-server12"
$regSrvDesc = "dbatoolsci-server123"

$newGroup = Add-DbaCmsRegServerGroup -SqlInstance $script:instance1 -Name $group
$newServer = Add-DbaCmsRegServer -SqlInstance $script:instance1 -ServerName $srvName -Name $regSrvName -Description $regSrvDesc

$srvName2 = "dbatoolsci-server2"
$group2 = "dbatoolsci-group1a"
$regSrvName2 = "dbatoolsci-server21"
$regSrvDesc2 = "dbatoolsci-server321"

$newGroup2 = Add-DbaCmsRegServerGroup -SqlInstance $script:instance1 -Name $group2
$newServer2 = Add-DbaCmsRegServer -SqlInstance $script:instance1 -ServerName $srvName2 -Name $regSrvName2 -Description $regSrvDesc2

$regSrvName3 = "dbatoolsci-server3"
$srvName3 = "dbatoolsci-server3"
$regSrvDesc3 = "dbatoolsci-server3desc"

$newServer3 = Add-DbaCmsRegServer -SqlInstance $script:instance1 -ServerName $srvName3 -Name $regSrvName3 -Description $regSrvDesc3
}
AfterAll {
Get-DbaCmsRegServer -SqlInstance $script:instance1, $script:instance2 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServer -Confirm:$false
Get-DbaCmsRegServerGroup -SqlInstance $script:instance1, $script:instance2 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServerGroup -Confirm:$false
$results, $results2, $results3 | Remove-Item -ErrorAction Ignore
}

It -Skip "should create an xml file" {
$results = $newServer | Export-DbaCmsRegServer
$results -is [System.IO.FileInfo] | Should -Be $true
$results.Extension -eq '.xml' | Should -Be $true
}

It "should create a specific xml file when using Path" {
$results2 = $newGroup2 | Export-DbaCmsRegServer -Path C:\temp\dbatoolsci_regserverexport.xml
$results2 -is [System.IO.FileInfo] | Should -Be $true
$results2.FullName | Should -Be 'C:\temp\dbatoolsci_regserverexport.xml'
Get-Content -Path $results2 -Raw | Should -Match dbatoolsci-group1a
}

It "creates an importable xml file" {
$results3 = $newServer3 | Export-DbaCmsRegServer -Path C:\temp\dbatoolsci_regserverexport.xml
$results4 = Import-DbaCmsRegServer -SqlInstance $script:instance2 -Path $results3
Expand Down
42 changes: 33 additions & 9 deletions tests/Get-DbaCmsRegServer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,69 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tags "UnitTests" {
Context "Validate parameters" {
$knownParameters = 'SqlInstance','SqlCredential','Name','ServerName','Group','ExcludeGroup','Id','IncludeSelf','ExcludeCmsServer','ResolveNetworkName','EnableException'
$SupportShouldProcess = $false
$paramCount = $knownParameters.Count
if ($SupportShouldProcess) {
$defaultParamCount = 13
}
else {
$defaultParamCount = 11
}
$command = Get-Command -Name $CommandName
[object[]]$params = $command.Parameters.Keys

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
}
}
}

Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Context "Setup" {
BeforeAll {
$server = Connect-DbaInstance $script:instance1
$regStore = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($server.ConnectionContext.SqlConnectionObject)
$dbStore = $regStore.DatabaseEngineServerGroup

$srvName = "dbatoolsci-server1"
$group = "dbatoolsci-group1"
$regSrvName = "dbatoolsci-server12"
$regSrvDesc = "dbatoolsci-server123"

<# Create that first group #>
$newGroup = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($dbStore, $group)
$newGroup.Create()
$dbStore.Refresh()

$groupStore = $dbStore.ServerGroups[$group]
$newServer = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($groupStore, $regSrvName)
$newServer.ServerName = $srvName
$newServer.Description = $regSrvDesc
$newServer.Create()

<# Create the sub-group #>
$srvName2 = "dbatoolsci-server2"
$group2 = "dbatoolsci-group1a"
$regSrvName2 = "dbatoolsci-server21"
$regSrvDesc2 = "dbatoolsci-server321"

$newGroup2 = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($groupStore, $group2)
$newGroup2.Create()
$dbStore.Refresh()

$groupStore2 = $dbStore.ServerGroups[$group].ServerGroups[$group2]
$newServer2 = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($groupStore2, $regSrvName2)
$newServer2.ServerName = $srvName2
$newServer2.Description = $regSrvDesc2
$newServer2.Create()

$regSrvName3 = "dbatoolsci-server3"
$srvName3 = "dbatoolsci-server3"
$regSrvDesc3 = "dbatoolsci-server3desc"
Expand All @@ -53,7 +77,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
Get-DbaCmsRegServer -SqlInstance $script:instance1 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServer -Confirm:$false
Get-DbaCmsRegServerGroup -SqlInstance $script:instance1 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServerGroup -Confirm:$false
}

It "Should return multiple objects" {
$results = Get-DbaCmsRegServer -SqlInstance $script:instance1 -Group $group
$results.Count | Should Be 2
Expand All @@ -66,7 +90,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$results = Get-DbaCmsRegServer -SqlInstance $script:instance1 -ExcludeGroup "$group\$group2"
@($results | Where-Object Name -eq $srvName3).Count | Should -Be 1
}

# Property Comparisons will come later when we have the commands
}
}
Expand Down
40 changes: 32 additions & 8 deletions tests/Get-DbaCmsRegServerGroup.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,63 @@ $CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

Describe "$CommandName Unit Tests" -Tags "UnitTests" {
Context "Validate parameters" {
$knownParameters = 'SqlInstance','SqlCredential','Group','ExcludeGroup','Id','EnableException'
$SupportShouldProcess = $false
$paramCount = $knownParameters.Count
if ($SupportShouldProcess) {
$defaultParamCount = 13
}
else {
$defaultParamCount = 11
}
$command = Get-Command -Name $CommandName
[object[]]$params = $command.Parameters.Keys

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
}
}
}

Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Context "Setup" {
BeforeAll {
$server = Connect-DbaInstance $script:instance1
$regStore = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServersStore($server.ConnectionContext.SqlConnectionObject)
$dbStore = $regStore.DatabaseEngineServerGroup

$srvName = "dbatoolsci-server1"
$group = "dbatoolsci-group1"
$regSrvName = "dbatoolsci-server12"
$regSrvDesc = "dbatoolsci-server123"

<# Create that first group #>
$newGroup = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($dbStore, $group)
$newGroup.Create()
$dbStore.Refresh()

$groupStore = $dbStore.ServerGroups[$group]
$newServer = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($groupStore, $regSrvName)
$newServer.ServerName = $srvName
$newServer.Description = $regSrvDesc
$newServer.Create()

<# Create the sub-group #>
$srvName2 = "dbatoolsci-server2"
$group2 = "dbatoolsci-group1a"
$regSrvName2 = "dbatoolsci-server21"
$regSrvDesc2 = "dbatoolsci-server321"

$newGroup2 = New-Object Microsoft.SqlServer.Management.RegisteredServers.ServerGroup($groupStore, $group2)
$newGroup2.Create()
$dbStore.Refresh()

$groupStore2 = $dbStore.ServerGroups[$group].ServerGroups[$group2]
$newServer2 = New-Object Microsoft.SqlServer.Management.RegisteredServers.RegisteredServer($groupStore2, $regSrvName2)
$newServer2.ServerName = $srvName2
Expand All @@ -45,7 +69,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
Get-DbaCmsRegServer -SqlInstance $script:instance1 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServer -Confirm:$false
Get-DbaCmsRegServerGroup -SqlInstance $script:instance1 | Where-Object Name -match dbatoolsci | Remove-DbaCmsRegServerGroup -Confirm:$false
}

It "Should return one group" {
$results = Get-DbaCmsRegServerGroup -SqlInstance $script:instance1 -Group $group
$results.Count | Should Be 1
Expand All @@ -54,7 +78,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
$results = Get-DbaCmsRegServerGroup -SqlInstance $script:instance1 -Group "$group\$group2"
$results.Count | Should Be 1
}

# Property Comparisons will come later when we have the commands
}
}
Loading

0 comments on commit 81d6ef0

Please sign in to comment.