Skip to content

Refactor and enhance Pester tests batch #9758

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

Open
wants to merge 12 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions tests/Get-DbaDbMail.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,47 +1,69 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaDbMail", # Static command name for dbatools
$PSDefaultParameterValues = $TestConfig.Defaults
)

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', '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
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"SqlInstance",
"SqlCredential",
"EnableException"
)
}

It "Should have the expected parameters" {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true

# Set variables. They are available in all the It blocks.
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$mailSettings = @{
AccountRetryAttempts = '1'
AccountRetryDelay = '60'
DatabaseMailExeMinimumLifeTime = '600'
DefaultAttachmentEncoding = 'MIME'
LoggingLevel = '2'
MaxFileSize = '1000'
ProhibitedExtensions = 'exe,dll,vbs,js'
AccountRetryAttempts = "1"
AccountRetryDelay = "60"
DatabaseMailExeMinimumLifeTime = "600"
DefaultAttachmentEncoding = "MIME"
LoggingLevel = "2"
MaxFileSize = "1000"
ProhibitedExtensions = "exe,dll,vbs,js"
}
foreach ($m in $mailSettings.GetEnumerator()) {
$server.query("exec msdb.dbo.sysmail_configure_sp '$($m.key)','$($m.value)';")
}

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
}

Context "Gets DbMail Settings" {
$results = Get-DbaDbMail -SqlInstance $TestConfig.instance2
BeforeAll {
$results = Get-DbaDbMail -SqlInstance $TestConfig.instance2
}

It "Gets results" {
$results | Should Not Be $null
$results | Should -Not -BeNullOrEmpty
}
Foreach ($row in $($results.ConfigurationValues)) {
It "Should have ConfiguredValues of $($row.name)" {
$row.name | Should Bein $mailSettings.keys
}
It "Should have ConfiguredValues settings for $($row.name) of $($row.value)" {
$row.value | Should Bein $mailSettings.values

It "Should have the expected mail settings" {
foreach ($row in $results.ConfigurationValues) {
$row.name | Should -BeIn $mailSettings.Keys
$row.value | Should -BeIn $mailSettings.Values
}
}
}
}
}
121 changes: 86 additions & 35 deletions tests/Get-DbaDbMailAccount.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,84 +1,135 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0" }
param(
$ModuleName = "dbatools",
$CommandName = "Get-DbaDbMailAccount",
$PSDefaultParameterValues = $TestConfig.Defaults
)

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', 'Account', 'ExcludeAccount', 'InputObject', '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
Describe $CommandName -Tag UnitTests {
Context "Parameter validation" {
BeforeAll {
$hasParameters = (Get-Command $CommandName).Parameters.Values.Name | Where-Object { $PSItem -notin ("WhatIf", "Confirm") }
$expectedParameters = $TestConfig.CommonParameters
$expectedParameters += @(
"SqlInstance",
"SqlCredential",
"Account",
"ExcludeAccount",
"InputObject",
"EnableException"
)
}

It "Should have the expected parameters" {
Compare-Object -ReferenceObject $expectedParameters -DifferenceObject $hasParameters | Should -BeNullOrEmpty
}
}
}

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
Describe $CommandName -Tag IntegrationTests {
BeforeAll {
$accountname = "dbatoolsci_test_$(get-random)"
# We want to run all commands in the BeforeAll block with EnableException to ensure that the test fails if the setup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true

# Set variables. They are available in all the It blocks.
$accountName = "dbatoolsci_test_$(Get-Random)"
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$mailAccountSettings = "EXEC msdb.dbo.sysmail_add_account_sp
@account_name='$accountname',
@account_name='$accountName',
@description='Mail account for email alerts',
@email_address='[email protected]',
@display_name ='dbatoolsci mail alerts',
@mailserver_name='smtp.dbatools.io',
@replyto_address='[email protected]';"
$server.query($mailAccountSettings)
$server.Query($mailAccountSettings)

# We want to run all commands outside of the BeforeAll block without EnableException to be able to test for specific warnings.
$PSDefaultParameterValues.Remove('*-Dba*:EnableException')
}

AfterAll {
# We want to run all commands in the AfterAll block with EnableException to ensure that the test fails if the cleanup fails.
$PSDefaultParameterValues['*-Dba*:EnableException'] = $true

# Cleanup all created object.
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$mailAccountSettings = "EXEC msdb.dbo.sysmail_delete_account_sp
@account_name = '$accountname';"
$server.query($mailAccountSettings)
@account_name = '$accountName';"
$server.Query($mailAccountSettings)

# As this is the last block we do not need to reset the $PSDefaultParameterValues.
}

Context "Gets DbMail Account" {
$results = Get-DbaDbMailAccount -SqlInstance $TestConfig.instance2 | Where-Object {$_.Name -eq "$accountname"}
BeforeAll {
$results = Get-DbaDbMailAccount -SqlInstance $TestConfig.instance2 | Where-Object Name -eq $accountName
}

It "Gets results" {
$results | Should Not Be $null
$results | Should -Not -BeNullOrEmpty
}
It "Should have Name of $accounName" {
$results.name | Should Be $accountname

It "Should have Name of $accountName" {
$results.Name | Should -Be $accountName
}
It "Should have Desctiption of 'Mail account for email alerts' " {
$results.description | Should Be 'Mail account for email alerts'

It "Should have Description of 'Mail account for email alerts' " {
$results.Description | Should -Be "Mail account for email alerts"
}

It "Should have EmailAddress of '[email protected]' " {
$results.EmailAddress | Should Be '[email protected]'
$results.EmailAddress | Should -Be "[email protected]"
}

It "Should have ReplyToAddress of '[email protected]' " {
$results.ReplyToAddress | Should Be '[email protected]'
$results.ReplyToAddress | Should -Be "[email protected]"
}

It "Should have MailServer of '[smtp.dbatools.io]' " {
$results.MailServers | Should Be '[smtp.dbatools.io]'
$results.MailServers | Should -Be "[smtp.dbatools.io]"
}
}

Context "Gets DbMail when using -Account" {
$results = Get-DbaDbMailAccount -SqlInstance $TestConfig.instance2 -Account $accountname
BeforeAll {
$results = Get-DbaDbMailAccount -SqlInstance $TestConfig.instance2 -Account $accountName
}

It "Gets results" {
$results | Should Not Be $null
$results | Should -Not -BeNullOrEmpty
}
It "Should have Name of $accounName" {
$results.name | Should Be $accountname

It "Should have Name of $accountName" {
$results.Name | Should -Be $accountName
}
It "Should have Desctiption of 'Mail account for email alerts' " {
$results.description | Should Be 'Mail account for email alerts'

It "Should have Description of 'Mail account for email alerts' " {
$results.Description | Should -Be "Mail account for email alerts"
}

It "Should have EmailAddress of '[email protected]' " {
$results.EmailAddress | Should Be '[email protected]'
$results.EmailAddress | Should -Be "[email protected]"
}

It "Should have ReplyToAddress of '[email protected]' " {
$results.ReplyToAddress | Should Be '[email protected]'
$results.ReplyToAddress | Should -Be "[email protected]"
}

It "Should have MailServer of '[smtp.dbatools.io]' " {
$results.MailServers | Should Be '[smtp.dbatools.io]'
$results.MailServers | Should -Be "[smtp.dbatools.io]"
}
}

Context "Gets no DbMail when using -ExcludeAccount" {
$results = Get-DbaDbMailAccount -SqlInstance $TestConfig.instance2 -ExcludeAccount $accountname
BeforeAll {
$results = Get-DbaDbMailAccount -SqlInstance $TestConfig.instance2 -ExcludeAccount $accountName
}

It "Gets no results" {
$results | Should Be $null
$results | Should -BeNullOrEmpty
}
}
}
}
Loading
Loading