1
1
# Requires -Module @ { ModuleName = " Pester" ; ModuleVersion = " 5.0" }
2
2
param (
3
- $ModuleName = " dbatools" ,
4
- $PSDefaultParameterValues = ($TestConfig = Get-TestConfig ).Defaults
3
+ $ModuleName = " dbatools" ,
4
+ $CommandName = [System.IO.Path ]::GetFileName($PSCommandPath.Replace (' .Tests.ps1' , ' ' )),
5
+ $PSDefaultParameterValues = $TestConfig.Defaults
5
6
)
6
7
7
- Describe " Copy-DbaDbMail " - Tag " UnitTests" {
8
+ Describe $CommandName - Tag " UnitTests" {
8
9
Context " Parameter validation" {
9
10
BeforeAll {
10
- $command = Get-Command Copy-DbaDbMail
11
- $expected = $TestConfig.CommonParameters
12
- $expected += @ (
11
+ $hasParameters = ( Get-Command $CommandName ).Parameters.Values.Name | Where-Object { $_ -notin ( ' WhatIf ' , ' Confirm ' ) }
12
+ $expectedParameters = $TestConfig.CommonParameters
13
+ $expectedParameters += @ (
13
14
" Source" ,
14
15
" Destination" ,
15
16
" Type" ,
@@ -20,70 +21,55 @@ Describe "Copy-DbaDbMail" -Tag "UnitTests" {
20
21
)
21
22
}
22
23
23
- It " Has parameter: <_>" - ForEach $expected {
24
- $command | Should - HaveParameter $PSItem
25
- }
26
-
27
- It " Should have exactly the number of expected parameters ($ ( $expected.Count ) )" {
28
- $hasparms = $command.Parameters.Values.Name | Where-Object { $_ -notin (' whatif' , ' confirm' ) }
29
- Compare-Object - ReferenceObject $expected - DifferenceObject $hasparms | Should - BeNullOrEmpty
24
+ It " Should have the expected parameters" {
25
+ Compare-Object - ReferenceObject $expectedParameters - DifferenceObject $hasParameters | Should - BeNullOrEmpty
30
26
}
31
27
}
32
28
}
33
29
34
- Describe " Copy-DbaDbMail " - Tags " IntegrationTests" {
30
+ Describe $CommandName - Tags " IntegrationTests" {
35
31
BeforeAll {
36
- $servers = Connect-DbaInstance - SqlInstance $TestConfig.instance2 , $TestConfig.instance3
37
- foreach ($s in $servers ) {
38
- if ( (Get-DbaSpConfigure - SqlInstance $s - Name ' Database Mail XPs' ).RunningValue -ne 1 ) {
39
- Set-DbaSpConfigure - SqlInstance $s - Name ' Database Mail XPs' - Value 1
40
- }
41
- }
32
+ $PSDefaultParameterValues [' *-Dba*:EnableException' ] = $true
42
33
43
- $accountName = " dbatoolsci_test_$ ( get-random ) "
44
- $account_display_name = ' dbatoolsci mail alerts'
45
- $account_description = ' Mail account for email alerts'
46
- $profilename = " dbatoolsci_test_$ ( get-random ) "
47
- $profile_description = ' Mail profile for email alerts'
34
+ # TODO: Maybe remove "-EnableException:$false -WarningAction SilentlyContinue" when we can rely on the setting beeing 0 when entering the test
35
+ $null = Set-DbaSpConfigure - SqlInstance $TestConfig.instance2 , $TestConfig.instance3 - Name ' Database Mail XPs' - Value 1 - EnableException:$false - WarningAction SilentlyContinue
48
36
49
- $email_address = ' [email protected] '
50
- $mailserver_name = ' smtp.dbatools.io'
51
- $replyto_address = ' [email protected] '
52
- $mailaccountpriority = 1
37
+ $accountName = " dbatoolsci_test_$ ( get-random ) "
38
+ $profileName = " dbatoolsci_test_$ ( get-random ) "
53
39
54
40
$splat1 = @ {
55
41
SqlInstance = $TestConfig.instance2
56
42
Name = $accountName
57
- Description = $account_description
58
- EmailAddress = $email_address
59
- DisplayName = $account_display_name
60
- ReplyToAddress = $replyto_address
61
- MailServer = $mailserver_name
43
+ Description = ' Mail account for email alerts '
44
+ EmailAddress = ' [email protected] '
45
+ DisplayName = ' dbatoolsci mail alerts '
46
+ ReplyToAddress = ' [email protected] '
47
+ MailServer = ' smtp.dbatools.io '
62
48
}
63
49
$null = New-DbaDbMailAccount @splat1 - Force
64
50
65
51
$splat2 = @ {
66
52
SqlInstance = $TestConfig.instance2
67
- Name = $profilename
68
- Description = $profile_description
53
+ Name = $profileName
54
+ Description = ' Mail profile for email alerts '
69
55
MailAccountName = $accountName
70
- MailAccountPriority = $mailaccountpriority
56
+ MailAccountPriority = 1
71
57
}
72
58
$null = New-DbaDbMailProfile @splat2
59
+
60
+ $PSDefaultParameterValues.Remove (' *-Dba*:EnableException' )
73
61
}
74
62
75
63
AfterAll {
76
- $servers = Connect-DbaInstance - SqlInstance $TestConfig .instance2 , $TestConfig .instance3
64
+ $PSDefaultParameterValues [ ' *-Dba*:EnableException ' ] = $true
77
65
78
- foreach ($s in $servers ) {
79
- $mailAccountSettings = " EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$accountname ';"
80
- Invoke-DbaQuery - SqlInstance $s - Query $mailAccountSettings - Database msdb
81
- $mailProfileSettings = " EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name = '$profilename ';"
82
- Invoke-DbaQuery - SqlInstance $s - Query $mailProfileSettings - Database msdb
83
- }
66
+ Invoke-DbaQuery - SqlInstance $TestConfig.instance2 , $TestConfig.instance3 - Query " EXEC msdb.dbo.sysmail_delete_account_sp @account_name = '$accountName ';"
67
+ Invoke-DbaQuery - SqlInstance $TestConfig.instance2 , $TestConfig.instance3 - Query " EXEC msdb.dbo.sysmail_delete_profile_sp @profile_name = '$profileName ';"
68
+
69
+ $null = Set-DbaSpConfigure - SqlInstance $TestConfig.instance2 , $TestConfig.instance3 - Name ' Database Mail XPs' - Value 0
84
70
}
85
71
86
- Context " When copying DbMail to $ ( $TestConfig .instance3 ) " {
72
+ Context " When copying DbMail" {
87
73
BeforeAll {
88
74
$results = Copy-DbaDbMail - Source $TestConfig.instance2 - Destination $TestConfig.instance3
89
75
}
@@ -92,9 +78,32 @@ Describe "Copy-DbaDbMail" -Tags "IntegrationTests" {
92
78
$results | Should -Not - BeNullOrEmpty
93
79
}
94
80
95
- It " Should have copied <_.type> from source to destination" - ForEach ($results | Where-Object type -in @ (' Mail Configuration' , ' Mail Account' , ' Mail Profile' )) {
96
- $PSItem.SourceServer | Should - Be $TestConfig.instance2
97
- $PSItem.DestinationServer | Should - Be $TestConfig.instance3
81
+ It " Should have copied Mail Configuration from source to destination" {
82
+ $result = $results | Where-Object Type -eq ' Mail Configuration'
83
+ $result.SourceServer | Should - Be $TestConfig.instance2
84
+ $result.DestinationServer | Should - Be $TestConfig.instance3
85
+ $result.Status | Should - Be ' Successful'
86
+ }
87
+
88
+ It " Should have copied Mail Account from source to destination" {
89
+ $result = $results | Where-Object Type -eq ' Mail Account'
90
+ $result.SourceServer | Should - Be $TestConfig.instance2
91
+ $result.DestinationServer | Should - Be $TestConfig.instance3
92
+ $result.Status | Should - Be ' Successful'
93
+ }
94
+
95
+ It " Should have copied Mail Profile from source to destination" {
96
+ $result = $results | Where-Object Type -eq ' Mail Profile'
97
+ $result.SourceServer | Should - Be $TestConfig.instance2
98
+ $result.DestinationServer | Should - Be $TestConfig.instance3
99
+ $result.Status | Should - Be ' Successful'
100
+ }
101
+
102
+ It " Should have copied Mail Server from source to destination" {
103
+ $result = $results | Where-Object Type -eq ' Mail Server'
104
+ $result.SourceServer | Should - Be $TestConfig.instance2
105
+ $result.DestinationServer | Should - Be $TestConfig.instance3
106
+ $result.Status | Should - Be ' Successful'
98
107
}
99
108
}
100
109
@@ -107,10 +116,26 @@ Describe "Copy-DbaDbMail" -Tags "IntegrationTests" {
107
116
$results | Should -Not - BeNullOrEmpty
108
117
}
109
118
110
- It " Should have skipped <_.type> copy operations" - ForEach $results {
111
- $PSItem.SourceServer | Should - Be $TestConfig.instance2
112
- $PSItem.DestinationServer | Should - Be $TestConfig.instance3
113
- $PSItem.Status | Should - Be ' Skipped'
119
+ It " Should have not reported on Mail Configuration" {
120
+ $result = $results | Where-Object Type -eq ' Mail Configuration'
121
+ $result | Should - BeNullOrEmpty
122
+ }
123
+
124
+ It " Should have not reported on Mail Account" {
125
+ $result = $results | Where-Object Type -eq ' Mail Account'
126
+ $result | Should - BeNullOrEmpty
127
+ }
128
+
129
+ It " Should have not reported on Mail Profile" {
130
+ $result = $results | Where-Object Type -eq ' Mail Profile'
131
+ $result | Should - BeNullOrEmpty
132
+ }
133
+
134
+ It " Should have skipped Mail Server" {
135
+ $result = $results | Where-Object Type -eq ' Mail Server'
136
+ $result.SourceServer | Should - Be $TestConfig.instance2
137
+ $result.DestinationServer | Should - Be $TestConfig.instance3
138
+ $result.Status | Should - Be ' Skipped'
114
139
}
115
140
}
116
141
}
0 commit comments