Skip to content

Commit

Permalink
Add DbaAgentSchedule tests (dataplat#5396)
Browse files Browse the repository at this point in the history
* Remove documentation of unused parameter

* Add missing DbaAgentSchedule tests
  • Loading branch information
joshcorr authored and potatoqualitee committed Apr 22, 2019
1 parent 8d24497 commit fb5ff23
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 24 deletions.
3 changes: 0 additions & 3 deletions functions/New-DbaAgentSchedule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ function New-DbaAgentSchedule {

If force is used the start time will be '23:59:59'

.PARAMETER Owner
The name of the server principal that owns the schedule. If no value is given the schedule is owned by the creator.

.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/Set-DbaAgentSchedule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,6 @@ function Set-DbaAgentSchedule {
Example: '010000' for 01:00:00 AM.
Example: '140000' for 02:00:00 PM.

.PARAMETER Owner
The name of the server principal that owns the schedule. If no value is given the schedule is owned by the creator.

.PARAMETER WhatIf
Shows what would happen if the command were to run. No actions are actually performed.

Expand Down
205 changes: 199 additions & 6 deletions tests/New-DbaAgentSchedule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,208 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance','SqlCredential','Job','Schedule','Disabled','FrequencyType','FrequencyInterval','FrequencySubdayType','FrequencySubdayInterval','FrequencyRelativeInterval','FrequencyRecurrenceFactor','StartDate','EndDate','StartTime','EndTime','Force','EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Job', 'Schedule', 'Disabled', 'FrequencyType', 'FrequencyInterval', 'FrequencySubdayType', 'FrequencySubdayInterval', 'FrequencyRelativeInterval', 'FrequencyRecurrenceFactor', 'StartDate', 'EndDate', 'StartTime', 'EndTime', 'Force', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
<#
Integration test should appear below and are custom to the command you are writing.
Read https://github.com/sqlcollaborative/dbatools/blob/development/contributing.md#tests
for more guidence.
#>

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_newschedule' -OwnerLogin 'sa'
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job 'dbatoolsci_newschedule' -StepId 1 -StepName 'dbatoolsci Test Select' -Subsystem TransactSql -SubsystemServer $script:instance2 -Command "SELECT * FROM master.sys.all_columns;" -CmdExecSuccessCode 0 -OnSuccessAction QuitWithSuccess -OnFailAction QuitWithFailure -Database master -DatabaseUser sa

$start = (Get-Date).AddDays(2).ToString('yyyyMMdd')
$end = (Get-Date).AddDays(4).ToString('yyyyMMdd')
}
AfterAll {
$null = Remove-DbaAgentJob -SqlInstance $script:instance2 -Job 'dbatoolsci_newschedule'
}

Context "Should create schedules based on static frequency" {
BeforeAll {
$results = New-Object System.collections.arraylist
}
AfterAll {
$null = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'} |
Remove-DbaAgentSchedule -Confirm:$false -Force
Remove-Variable -Name results
}

foreach ($frequency in ('Once', 'AgentStart', 'IdleComputer')) {
$variables = @{SqlInstance = $script:instance2
Schedule = "dbatoolsci_$frequency"
Job = 'dbatoolsci_newschedule'
FrequencyType = $frequency
FrequencyRecurrenceFactor = '1'
}

if ($frequency -ne 'IdleComputer') {
$results.add($(New-DbaAgentSchedule -StartDate $start -StartTime '010000' -EndDate $end -EndTime '020000' @variables))
} else {
$results.add($(New-DbaAgentSchedule -Disabled -Force @variables))
}
}

It "Should have Results" {
$results | Should Not BeNullOrEmpty
}
foreach ($r in $results) {
It "$($r.name) Should be a schedule on an existing job" {
$($r.parent) | Should Be 'dbatoolsci_newschedule'
}
}
}

Context "Should create schedules based on calendar frequency" {
BeforeAll {
$results = New-Object System.collections.arraylist
}
AfterAll {
$null = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'} |
Remove-DbaAgentSchedule -Confirm:$false -Force
Remove-Variable -Name results
}

foreach ($frequency in ('Daily', 'Weekly', 'Monthly', 'MonthlyRelative')) {
$variables = @{SqlInstance = $script:instance2
Schedule = "dbatoolsci_$frequency"
Job = 'dbatoolsci_newschedule'
FrequencyType = $frequency
FrequencyRecurrenceFactor = '1'
FrequencyInterval = '1'
FrequencyRelativeInterval = 'First'
StartDate = $start
StartTime = '010000'
EndDate = $end
EndTime = '020000'
}

$results.add( $(New-DbaAgentSchedule @variables))
}

It "Should have Results" {
$results | Should Not BeNullOrEmpty
}
foreach ($r in $results) {
It "$($r.name) Should be a schedule on an existing job" {
$($r.parent) | Should Be 'dbatoolsci_newschedule'
}
}
}

Context "Should create schedules with various frequency interval" {
BeforeAll {
$results = New-Object System.collections.arraylist
}
AfterAll {
$null = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'} |
Remove-DbaAgentSchedule -Confirm:$false -Force
Remove-Variable -Name results
}

foreach ($frequencyinterval in ('EveryDay', 'Weekdays', 'Weekend', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
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)) {
$variables = @{SqlInstance = $script:instance2
Schedule = "dbatoolsci_$frequencyinterval"
Job = 'dbatoolsci_newschedule'
FrequencyType = 'Daily'
FrequencyRecurrenceFactor = '1'
FrequencyInterval = $frequencyinterval
StartDate = $start
StartTime = '010000'
EndDate = $end
EndTime = '020000'
}

$results.add( $(New-DbaAgentSchedule @variables))
}

It "Should have Results" {
$results | Should Not BeNullOrEmpty
}
foreach ($r in $results) {
It "$($r.name) Should be a schedule on an existing job" {
$($r.parent) | Should Be 'dbatoolsci_newschedule'
}
}
}

Context "Should create schedules with various frequency subday type" {
BeforeAll {
$results = New-Object System.collections.arraylist
}
AfterAll {
$null = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'} |
Remove-DbaAgentSchedule -Confirm:$false -Force
Remove-Variable -Name results
}

foreach ($FrequencySubdayType in ('Time', 'Seconds', 'Minutes', 'Hours')) {
$variables = @{SqlInstance = $script:instance2
Schedule = "dbatoolsci_$FrequencySubdayType"
Job = 'dbatoolsci_newschedule'
FrequencyRecurrenceFactor = '1'
FrequencySubdayInterval = '1'
FrequencySubdayType = $FrequencySubdayType
StartDate = $start
StartTime = '010000'
EndDate = $end
EndTime = '020000'
}

$results.add( $(New-DbaAgentSchedule @variables))
}

It "Should have Results" {
$results | Should Not BeNullOrEmpty
}
foreach ($r in $results) {
It "$($r.name) Should be a schedule on an existing job" {
$($r.parent) | Should Be 'dbatoolsci_newschedule'
}
}
}

Context "Should create schedules with various frequency relative interval" {
BeforeAll {
$results = New-Object System.collections.arraylist
}
AfterAll {
$null = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'} |
Remove-DbaAgentSchedule -Confirm:$false -Force
Remove-Variable -Name results
}

foreach ($FrequencyRelativeInterval in ('Unused', 'First', 'Second', 'Third', 'Fourth', 'Last')) {
$variables = @{SqlInstance = $script:instance2
Schedule = "dbatoolsci_$FrequencyRelativeInterval"
Job = 'dbatoolsci_newschedule'
FrequencyRecurrenceFactor = '1'
FrequencyRelativeInterval = $FrequencyRelativeInterval
StartDate = $start
StartTime = '010000'
EndDate = $end
EndTime = '020000'
}

$results.add( $(New-DbaAgentSchedule @variables))
}

It "Should have Results" {
$results | Should Not BeNullOrEmpty
}
foreach ($r in $results) {
It "$($r.name) Should be a schedule on an existing job" {
$($r.parent) | Should Be 'dbatoolsci_newschedule'
}
}
}
}
49 changes: 43 additions & 6 deletions tests/Remove-DbaAgentSchedule.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,52 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance','SqlCredential','Schedule','InputObject','EnableException','Force'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Schedule', 'InputObject', 'EnableException', 'Force'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
}
}
}
<#
Integration test should appear below and are custom to the command you are writing.
Read https://github.com/sqlcollaborative/dbatools/blob/development/contributing.md#tests
for more guidence.
#>

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$start = (Get-Date).AddDays(2).ToString('yyyyMMdd')
$end = (Get-Date).AddDays(4).ToString('yyyyMMdd')

foreach ($FrequencySubdayType in ('Time', 'Seconds', 'Minutes', 'Hours')) {
$variables = @{SqlInstance = $script:instance2
Schedule = "dbatoolsci_$FrequencySubdayType"
FrequencyRecurrenceFactor = '1'
FrequencySubdayInterval = '1'
FrequencySubdayType = $FrequencySubdayType
StartDate = $start
StartTime = '010000'
EndDate = $end
EndTime = '020000'
}
$null = New-DbaAgentSchedule @variables
}
}

Context "Should remove schedules" {
$results = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'}
It "Should find all created schedule" {
$results | Should Not BeNullOrEmpty
}

Remove-DbaAgentSchedule -SqlInstance $script:instance2 -Schedule dbatoolsci_MonthlyRelative -Confirm:$false
$results = Get-DbaAgentSchedule -SqlInstance $script:instance2 -Schedule dbatoolsci_MonthlyRelative
It "Should not find dbatoolsci_MonthlyRelative" {
$results | Should BeNullOrEmpty
}

$results = Get-DbaAgentSchedule -SqlInstance $script:instance2 |
Where-Object {$_.name -like 'dbatools*'} |
Remove-DbaAgentSchedule -Confirm:$false -Force
It "Should not find any created schedule" {
$results | Should BeNullOrEmpty
}
}
}
Loading

0 comments on commit fb5ff23

Please sign in to comment.