/
Publish-DatabaseDeployment.Tests.ps1
135 lines (126 loc) · 10.4 KB
/
Publish-DatabaseDeployment.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
#import module from repo
Import-Module (Join-Path $PSScriptRoot "..\PoshSSDTBuildDeploy") -Force
Describe "Publish-DatabaseDeployment" {
function Get-DbId ($databaseName, $serverInstanceName) {
@(Invoke-Sqlcmd -Query "select db_id('$databaseName') as DbId" -ServerInstance $serverInstanceName) | Select-Object -First 1 -ExpandProperty DbId
}
BeforeAll {
$instanceName = "poshssdtbuilddeploy"
sqllocaldb.exe create $instanceName 13.0 -s
sqllocaldb.exe info $instanceName
$serverInstance = "(localdb)\$instanceName"
$svrConnstring = "SERVER=$serverInstance;Integrated Security=True;Database=master"
$WWI_NAME = "WideWorldImporters"
$WWI = Join-Path $PSScriptRoot "wwi-dw-ssdt"
$WWI_SLN = Join-Path $WWI "\WideWorldImportersDW.sqlproj"
$WWI_DAC = Join-Path $WWI "\Microsoft.Data.Tools.Msbuild\lib\net46"
$WWI_DACFX = Join-Path $WWI_DAC "\Microsoft.SqlServer.Dac.dll"
$WWI_DACPAC = Join-Path $WWI "\bin\Debug\WideWorldImportersDW.dacpac"
$WWI_PUB = Join-Path $WWI "\bin\Debug\WideWorldImportersDW.publish.xml"
$DeploymentReportPathPattern = Join-path $WWI "*DeploymentReport_*.xml"
$DeploymentScriptPathPattern = Join-path $WWI "*DeployScript_*.sql"
$DeploymentSummaryPathPattern = Join-path $WWI "*DeploymentSummary_*.txt"
#Remove-Item $WWI_DACPAC -Force -ErrorAction SilentlyContinue
#Invoke-MsBuildSSDT -DatabaseSolutionFilePath $WWI_SLN -DataToolsFilePath $WWI_DAC
}
BeforeEach {
Remove-Item $DeploymentReportPathPattern -ErrorAction SilentlyContinue
Remove-Item $DeploymentScriptPathPattern -ErrorAction SilentlyContinue
Remove-Item $DeploymentSummaryPathPattern -ErrorAction SilentlyContinue
Invoke-Sqlcmd -Query "drop database if exists $WWI_NAME" -ServerInstance $serverInstance
}
it "Deploy the database and DeploymentScript is not generated and DeploymentReport is not generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeploymentReport $false -ScriptPath $WWI } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -Not -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Not -Exist
$DeploymentReportPathPattern | Should -Not -Exist
$DeploymentSummaryPathPattern | Should -Not -Exist
}
it "Deploy the database and DeploymentScript is not generated and DeploymentReport is not generated and Missing Variable is written to Host" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeploymentReport $false -ScriptPath $WWI -getSqlCmdVars } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -Not -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Not -Exist
$DeploymentReportPathPattern | Should -Not -Exist
$DeploymentSummaryPathPattern | Should -Not -Exist
}
it "Deploy the database and DeploymentScript is generated and DeploymentReport is not generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $true -GenerateDeploymentReport $false -ScriptPath $WWI } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -Not -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Exist
$DeploymentReportPathPattern | Should -Not -Exist
$DeploymentSummaryPathPattern | Should -Not -Exist
}
it "Deploy the database and DeploymentScript is not generated and DeploymentReport is generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeploymentReport $true -ScriptPath $WWI } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -Not -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Not -Exist
$DeploymentReportPathPattern | Should -Exist
$DeploymentSummaryPathPattern | Should -Exist
}
it "Deploy the database and DeploymentScript is generated and DeploymentReport is generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $true -GenerateDeploymentReport $true -ScriptPath $WWI } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -Not -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Exist
$DeploymentReportPathPattern | Should -Exist
$DeploymentSummaryPathPattern | Should -Exist
}
it "Database is not deployed and DeploymentScript is generated and DeploymentReport is not generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $true -GenerateDeploymentReport $false -ScriptPath $WWI -ScriptOnly } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Exist
$DeploymentReportPathPattern | Should -Not -Exist
$DeploymentSummaryPathPattern | Should -Not -Exist
}
it "Database is not deployed and DeploymentScript is not generated and DeploymentReport is generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeploymentReport $true -ScriptPath $WWI -ScriptOnly } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Not -Exist
$DeploymentReportPathPattern | Should -Exist
$DeploymentSummaryPathPattern | Should -Exist
}
it "Database is not deployed and DeploymentScript is generated and DeploymentReport is generated" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $true -GenerateDeploymentReport $true -ScriptPath $WWI -ScriptOnly } | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Exist
$DeploymentReportPathPattern | Should -Exist
$DeploymentSummaryPathPattern | Should -Exist
}
it "throws exception if not at least one of GenerateDeploymentScript or GenerateDeploymentReport is true when using ScriptOnly" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeployMentReport $false -ScriptPath $WWI -ScriptOnly} |
Should -Throw "Specify at least one of GenerateDeploymentScript or GenerateDeploymentReport to be true when using ScriptOnly!"
}
it "throws exception if Script Path is Invalid" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeployMentReport $false -ScriptPath "X:\bob" } |
Should -Throw "Script Path Invalid"
}
it "Throws exception that variable is not included in session" {
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeploymentReport $false -ScriptPath $WWI -getSqlCmdVars -FailOnMissingVars } |
Should -Throw
}
it "Variable is included in the session" {
$global:DeployTag = "PesterTest"
{Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $true -GenerateDeploymentReport $true -ScriptPath $WWI -getSqlCmdVars -FailOnMissingVars -Verbose } | Should -Not -Throw
$DeploymentScriptPathPattern | Should -Exist
$DeploymentReportPathPattern | Should -Exist
$DeploymentSummaryPathPattern | Should -Exist
}
it "Deploy the database and DeploymentScript is not generated and DeploymentReport is not generated and DeployTag is updated to PesterTest" {
{$DeployTag = "PesterTest"
Write-Host $DeployTag
Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetConnectionString $svrConnstring -targetDatabaseName $WWI_NAME -GenerateDeploymentScript $false -GenerateDeploymentReport $false -ScriptPath $WWI -getSqlCmdVars -Verbose} | Should -Not -Throw
Get-DbId -databaseName $WWI_NAME -serverInstanceName $serverInstance | Should -Not -BeNullOrEmpty
$DeploymentScriptPathPattern | Should -Not -Exist
$DeploymentReportPathPattern | Should -Not -Exist
$DeploymentSummaryPathPattern | Should -Not -Exist
}
# it "Connection String from publish.xml is used for publishing database." {
# {
# $WWI_PUB = Join-Path $WWI "\bin\Debug\WideWorldImportersDW_PesterTestLocalConnString.publish.xml"
# $instanceName = "poshssdtbuilddeploy2"
# sqllocaldb.exe create $instanceName 13.0 -s
# sqllocaldb.exe info $instanceName
# $serverInstance = "(localdb)\$instanceName"
# Publish-DatabaseDeployment -dacfxPath $WWI_DACFX -dacpac $WWI_DACPAC -publishXml $WWI_PUB -targetDatabaseName $WWI_NAME -ScriptPath $WWI -getSqlCmdVars -Verbose} | Should -Not -Throw
# Get-DbId -databaseName $WWI_NAME -serverInstanceName "(localdb)\poshssdtbuilddeploy2" | Should -Not -BeNullOrEmpty
# }
}