Skip to content

Commit

Permalink
refactor test (do Get-DbaEstimatedCompletionTime)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasjordan committed Oct 20, 2024
1 parent 29d9d2c commit a79aa3f
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions tests/Get-DbaEstimatedCompletionTime.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,63 @@ Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "$commandname Integration Tests" -Tags "IntegrationTests" {

BeforeAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Get-DbaDatabase -SqlInstance $server -Database checkdbTestDatabase | Remove-DbaDatabase -Confirm:$false
$null = Restore-DbaDatabase -SqlInstance $server -Path $script:appveyorlabrepo\sql2008-backups\db1\FULL\SQL2008_db1_FULL_20170518_041738.bak -DatabaseName checkdbTestDatabase
$null = New-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$null = New-DbaAgentJobStep -SqlInstance $server -Job checkdbTestJob -StepName checkdb -Subsystem TransactSql -Command "DBCC CHECKDB('checkdbTestDatabase')"
if ($script:bigDatabaseBackup) {
if (-not (Test-Path -Path $script:bigDatabaseBackup) -and $script:bigDatabaseBackupSourceUrl) {
Invoke-WebRequest -Uri $script:bigDatabaseBackupSourceUrl -OutFile $script:bigDatabaseBackup
}
$null = Restore-DbaDatabase -SqlInstance $script:instance2 -Path $script:bigDatabaseBackup -DatabaseName checkdbTestDatabase -WithReplace -ReplaceDbNameInFile
$null = New-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
$null = New-DbaAgentJobStep -SqlInstance $script:instance2 -Job checkdbTestJob -StepName checkdb -Subsystem TransactSql -Command "DBCC CHECKDB('checkdbTestDatabase')"
$skip = $false
} else {
$skip = $true
}
}

AfterAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Remove-DbaAgentJob -SqlInstance $server -Job checkdbTestJob -Confirm:$false
$null = Get-DbaDatabase -SqlInstance $server -Database checkdbTestDatabase | Remove-DbaDatabase -Confirm:$false
$null = Get-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob | Remove-DbaAgentJob -Confirm:$false
$null = Get-DbaDatabase -SqlInstance $script:instance2 -Database checkdbTestDatabase | Remove-DbaDatabase -Confirm:$false
}

Context "Gets Query Estimated Completion" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Start-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$results = Get-DbaEstimatedCompletionTime -SqlInstance $server
Start-Sleep -Seconds 5
It "Gets results" {
$results | Should Not Be $null
}
It "Should be SELECT" {
$results.Command | Should Match 'DBCC'
}
It "Should be login dbo" {
$results.login | Should Be 'dbo'
}
}
Context "Gets Query Estimated Completion when using -Database" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Start-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$results = Get-DbaEstimatedCompletionTime -SqlInstance $server -Database checkdbTestDatabase
Start-Sleep -Seconds 5
It "Gets results" {
$results | Should Not Be $null
}
It "Should be SELECT" {
$results.Command | Should Match 'DBCC'
Context "Gets correct results" {
It -Skip:$skip "Gets Query Estimated Completion" {
$job = Start-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
Start-Sleep -Seconds 1
$results = Get-DbaEstimatedCompletionTime -SqlInstance $script:instance2
while ($job.CurrentRunStatus -eq 'Executing') {
Start-Sleep -Seconds 1
$job.Refresh()
}

$results | Should -Not -BeNullOrEmpty
$results.Command | Should -Match 'DBCC'
$results.Database | Should -Be checkdbTestDatabase
}
It "Should be login dbo" {
$results.login | Should Be 'dbo'

It -Skip:$skip "Gets Query Estimated Completion when using -Database" {
$job = Start-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
Start-Sleep -Seconds 1
$results = Get-DbaEstimatedCompletionTime -SqlInstance $script:instance2 -Database checkdbTestDatabase
while ($job.CurrentRunStatus -eq 'Executing') {
Start-Sleep -Seconds 1
$job.Refresh()
}

$results | Should -Not -BeNullOrEmpty
$results.Command | Should -Match 'DBCC'
$results.Database | Should -Be checkdbTestDatabase
}
}
Context "Gets no Query Estimated Completion when using -ExcludeDatabase" {
$server = Connect-DbaInstance -SqlInstance $script:instance2
$null = Start-DbaAgentJob -SqlInstance $server -Job checkdbTestJob
$results = Get-DbaEstimatedCompletionTime -SqlInstance $server -ExcludeDatabase checkdbTestDatabase
Start-Sleep -Seconds 5
It "Gets no results" {
$results | Should Be $null

It -Skip:$skip "Gets no Query Estimated Completion when using -ExcludeDatabase" {
$job = Start-DbaAgentJob -SqlInstance $script:instance2 -Job checkdbTestJob
Start-Sleep -Seconds 1
$results = Get-DbaEstimatedCompletionTime -SqlInstance $script:instance2 -ExcludeDatabase checkdbTestDatabase
while ($job.CurrentRunStatus -eq 'Executing') {
Start-Sleep -Seconds 1
$job.Refresh()
}

$results | Should -BeNullOrEmpty
}
}
}

0 comments on commit a79aa3f

Please sign in to comment.