forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Export-DbaDacPackage.Tests.ps1
142 lines (135 loc) · 6.88 KB
/
Export-DbaDacPackage.Tests.ps1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"
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', 'ExcludeDatabase', 'AllUserDatabases', 'Path', 'FilePath', 'DacOption', 'ExtendedParameters', 'ExtendedProperties', 'Type', 'Table', '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 Integration Tests" -Tags "IntegrationTests" {
BeforeAll {
$random = Get-Random
$dbname = "dbatoolsci_exportdacpac_$random"
try {
$server = Connect-DbaInstance -SqlInstance $script:instance1
$null = $server.Query("Create Database [$dbname]")
$db = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname
$null = $db.Query("CREATE TABLE dbo.example (id int, PRIMARY KEY (id));
INSERT dbo.example
SELECT top 100 object_id
FROM sys.objects")
} catch { } # No idea why appveyor can't handle this
$testFolder = 'C:\Temp\dacpacs'
$dbName2 = "dbatoolsci:2_$random"
$dbName2Escaped = "dbatoolsci`$2_$random"
$null = New-DbaDatabase -SqlInstance $script:instance1 -Name $dbName2
}
AfterAll {
Remove-DbaDatabase -SqlInstance $script:instance1 -Database $dbname, $dbName2 -Confirm:$false
}
# See https://github.com/dataplat/dbatools/issues/7038
Context "Ensure the database name is part of the generated filename" {
It "Database name is included in the output filename" {
$result = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname
$result.Path | Should -BeLike "*$($dbName)*"
}
It "Database names with invalid filesystem chars are successfully exported" {
$result = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname, $dbName2
$result.Path.Count | Should -Be 2
$result.Path[0] | Should -BeLike "*$($dbName)*"
$result.Path[1] | Should -BeLike "*$($dbName2Escaped)*"
}
}
Context "Extract dacpac" {
BeforeEach {
New-Item $testFolder -ItemType Directory -Force
Push-Location $testFolder
}
AfterEach {
Pop-Location
Remove-Item $testFolder -Force -Recurse
}
if ((Get-DbaDbTable -SqlInstance $script:instance1 -Database $dbname -Table example)) {
# Sometimes appveyor bombs
It "exports a dacpac" {
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname
$results.Path | Should -Not -BeNullOrEmpty
Test-Path $results.Path | Should -Be $true
if (($results).Path) {
Remove-Item -Confirm:$false -Path ($results).Path -ErrorAction SilentlyContinue
}
}
It "exports to the correct directory" {
$relativePath = '.\'
$expectedPath = (Resolve-Path $relativePath).Path
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname -Path $relativePath
$results.Path | Split-Path | Should -Be $expectedPath
Test-Path $results.Path | Should -Be $true
}
It "exports dacpac with a table list" {
$relativePath = '.\extract.dacpac'
$expectedPath = Join-Path (Get-Item .) 'extract.dacpac'
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname -FilePath $relativePath -Table example
$results.Path | Should -Be $expectedPath
Test-Path $results.Path | Should -Be $true
if (($results).Path) {
Remove-Item -Confirm:$false -Path ($results).Path -ErrorAction SilentlyContinue
}
}
It "uses EXE to extract dacpac" {
$exportProperties = "/p:ExtractAllTableData=True"
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname -ExtendedProperties $exportProperties
$results.Path | Should -Not -BeNullOrEmpty
Test-Path $results.Path | Should -Be $true
if (($results).Path) {
Remove-Item -Confirm:$false -Path ($results).Path -ErrorAction SilentlyContinue
}
}
}
}
Context "Extract bacpac" {
BeforeEach {
New-Item $testFolder -ItemType Directory -Force
Push-Location $testFolder
}
AfterEach {
Pop-Location
Remove-Item $testFolder -Force -Recurse
}
if ((Get-DbaDbTable -SqlInstance $script:instance1 -Database $dbname -Table example)) {
# Sometimes appveyor bombs
It "exports a bacpac" {
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname -Type Bacpac
$results.Path | Should -Not -BeNullOrEmpty
Test-Path $results.Path | Should -Be $true
if (($results).Path) {
Remove-Item -Confirm:$false -Path ($results).Path -ErrorAction SilentlyContinue
}
}
It "exports bacpac with a table list" {
$relativePath = '.\extract.bacpac'
$expectedPath = Join-Path (Get-Item .) 'extract.bacpac'
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname -FilePath $relativePath -Table example -Type Bacpac
$results.Path | Should -Be $expectedPath
Test-Path $results.Path | Should -Be $true
if (($results).Path) {
Remove-Item -Confirm:$false -Path ($results).Path -ErrorAction SilentlyContinue
}
}
It "uses EXE to extract bacpac" {
$exportProperties = "/p:TargetEngineVersion=Default"
$results = Export-DbaDacPackage -SqlInstance $script:instance1 -Database $dbname -ExtendedProperties $exportProperties -Type Bacpac
$results.Path | Should -Not -BeNullOrEmpty
Test-Path $results.Path | Should -Be $true
if (($results).Path) {
Remove-Item -Confirm:$false -Path ($results).Path -ErrorAction SilentlyContinue
}
}
}
}
}