Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first pester5 test (do Invoke-DbaQuery) #9525

Merged
merged 12 commits into from
Oct 23, 2024
33 changes: 19 additions & 14 deletions tests/Invoke-DbaQuery.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig
#HaveParameter - yeah, I know.

BeforeAll {
$CommandName = (Get-Item $PSCommandPath).Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
$global:TestConfig = Get-TestConfig
}


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', 'Database', 'Query', 'QueryTimeout', 'File', 'SqlObject', 'As', 'SqlParameter', 'AppendServerInstance', 'MessagesToOutput', 'InputObject', 'ReadOnly', 'EnableException', 'CommandType', 'NoExec'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
BeforeAll {
$command = Get-Command Invoke-DbaQuery
}
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should Be 0
[object[]]$params = $command.Parameters.Keys | Where-Object { $_ -notin ('whatif', 'confirm') }
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'Database', 'Query', 'QueryTimeout', 'File', 'SqlObject', 'As', 'SqlParameter', 'AppendServerInstance', 'MessagesToOutput', 'InputObject', 'ReadOnly', 'EnableException', 'CommandType', 'NoExec'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object { $_ }) -DifferenceObject $params).Count ) | Should -Be 0
}
}
Context "Validate alias" {
Expand Down Expand Up @@ -65,7 +73,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
}
It "stops when piped databases and -Database" {
$dbs = Get-DbaDatabase -SqlInstance $TestConfig.instance2, $TestConfig.instance3
{ $dbs | Invoke-DbaQuery -Query "Select 'hello' as TestColumn, DB_NAME() as dbname" -Database tempdb -EnableException } | Should Throw "You can't"
{ $dbs | Invoke-DbaQuery -Query "Select 'hello' as TestColumn, DB_NAME() as dbname" -Database tempdb -EnableException } | Should -Throw "You can't*"
}
It "supports reading files" {
$testPath = "TestDrive:\dbasqlquerytest.txt"
Expand Down Expand Up @@ -108,7 +116,7 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" {
$null = Invoke-DbaQuery -SqlInstance $TestConfig.instance3 -Database tempdb -SqlObject $smoobj
$check = "SELECT name FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CommandLog]') AND type in (N'U')"
$results = Invoke-DbaQuery -SqlInstance $TestConfig.instance3 -Database tempdb -Query $check
$results.Name | Should Be 'CommandLog'
$results.Name | Should -Be 'CommandLog'
$null = Invoke-DbaQuery -SqlInstance $TestConfig.instance2, $TestConfig.instance3 -Database tempdb -Query $cleanup
}
<#
Expand Down Expand Up @@ -187,7 +195,7 @@ SELECT @@servername as dbname
}
It "Executes stored procedures with parameters" {
$results = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "dbatoolsci_procedure_example" -SqlParameters @{p1 = 1 } -CommandType StoredProcedure
$results.TestColumn | Should Be 1
$results.TestColumn | Should -Be 1
}
It "Executes script file with a relative path (see #6184)" {
Set-Content -Path ".\hellorelative.sql" -Value "Select 'hello' as TestColumn, DB_NAME() as dbname"
Expand Down Expand Up @@ -324,7 +332,7 @@ SELECT 2
$result = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query $q -NoExec
$result | Should -BeNullOrEmpty

{ Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "SELEC p FROM c" -NoExec -EnableException } | Should -Throw "Incorrect syntax near 'selec'"
{ Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Database tempdb -Query "SELEC p FROM c" -NoExec -EnableException } | Should -Throw "Incorrect syntax near 'SELEC'."
}

It "supports dropping temp objects (#8472)" {
Expand All @@ -345,7 +353,4 @@ SELECT 2
$results = Invoke-DbaQuery -SqlInstance $TestConfig.instance2 -Query "select cast(null as hierarchyid)"
$results.Column1 | Should -Be "NULL"
}


}

8 changes: 5 additions & 3 deletions tests/appveyor.pester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ if (-not $Finalize) {
$outcome = "Passed"
if ($PesterRun.FailedCount -gt 0) {
$trialno += 1
Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Failed" -Duration $PesterRun.Time.TotalMilliseconds
Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Failed" -Duration $PesterRun.Duration.TotalMilliseconds
} else {
Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Passed" -Duration $PesterRun.Time.TotalMilliseconds
Update-AppveyorTest -Name $appvTestName -Framework NUnit -FileName $f.FullName -Outcome "Passed" -Duration $PesterRun.Duration.TotalMilliseconds
break
}
}
Expand Down Expand Up @@ -331,7 +331,8 @@ if (-not $Finalize) {
Get-ChildItem $ModuleBase\dbatools_messages_and_errors.xml.zip | ForEach-Object { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
}
#$totalcount = $results | Select-Object -ExpandProperty TotalCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
$failedcount = $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
$failedcount = 0
$failedcount += $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
if ($failedcount -gt 0) {
# pester 4 output
$faileditems = $results | Select-Object -ExpandProperty TestResult | Where-Object { $_.Passed -notlike $True }
Expand All @@ -353,6 +354,7 @@ if (-not $Finalize) {


$results5 = @(Get-ChildItem -Path "$ModuleBase\Pester5Results*.xml" | Import-Clixml)
$failedcount += $results | Select-Object -ExpandProperty FailedCount | Measure-Object -Sum | Select-Object -ExpandProperty Sum
# pester 5 output
$faileditems = $results5 | Select-Object -ExpandProperty Tests | Where-Object { $_.Passed -notlike $True }
if ($faileditems) {
Expand Down
9 changes: 5 additions & 4 deletions tests/appveyor.post.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ Add-AppveyorTest -Name "appveyor.post" -Framework NUnit -FileName "appveyor.post
$sw = [system.diagnostics.stopwatch]::startNew()
Write-Host -Object "appveyor.post: Sending coverage data (pester 4)" -ForeGroundColor DarkGreen
Push-AppveyorArtifact PesterResultsCoverage.json -FileName "PesterResultsCoverage"
codecov -f PesterResultsCoverage.json --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null
codecov -f PesterResultsCoverage.json --flag "pester4_$($env:SCENARIO.ToLowerInvariant())" | Out-Null

Write-Host -Object "appveyor.post: Sending coverage data (pester 5)" -ForeGroundColor DarkGreen
$ProjectRoot = $env:APPVEYOR_BUILD_FOLDER,
$ModuleBase = $ProjectRoot,
$ProjectRoot = $env:APPVEYOR_BUILD_FOLDER
$ModuleBase = $ProjectRoot
$pester5CoverageFiles = Get-ChildItem -Path "$ModuleBase\Pester5Coverage*.xml"
foreach($coverageFile in $pester5CoverageFiles)
{
Write-Host -Object "appveyor.post: Sending $($coverageFile.FullName)" -ForeGroundColor DarkGreen
Push-AppveyorArtifact $coverageFile.FullName -FileName $coverageFile.Name
codecov -f $coverageFile.FullName --flag "ps,$($env:SCENARIO.ToLowerInvariant())" | Out-Null
codecov -f $coverageFile.FullName --flag "pester5_$($env:SCENARIO.ToLowerInvariant())" | Out-Null
}

$sw.Stop()
Expand Down
Loading