forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExport-DbaSysDbUserObject.Tests.ps1
95 lines (92 loc) · 5.06 KB
/
Export-DbaSysDbUserObject.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
$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', 'IncludeDependencies', 'BatchSeparator', 'Path', 'FilePath', 'NoPrefix', 'ScriptingOptionsObject', 'NoClobber', 'PassThru', '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
$tableName = "dbatoolsci_UserTable_$random"
$viewName = "dbatoolsci_View_$random"
$procName = "dbatoolsci_SP_$random"
$triggerName = "[dbatoolsci_Trigger_$random]"
$tableFunctionName = "[dbatoolsci_TableFunction_$random]"
$scalarFunctionName = "[dbatoolsci_ScalarFunction_$random]"
$ruleName = "[dbatoolsci_Rule_$random]"
$server = Connect-DbaInstance -SqlInstance $script:instance2 -SqlCredential $SqlCredential
$server.query("CREATE TABLE dbo.$tableName (Col1 int);", "master")
$server.query("CREATE VIEW dbo.$viewName AS SELECT 1 as Col1;", "master")
$server.query("CREATE PROCEDURE dbo.$procName as select 1;", "master")
$server.query("CREATE TRIGGER $triggerName ON DATABASE FOR DROP_SYNONYM AS RAISERROR ('You must disable Trigger safety to drop synonyms!', 10, 1)", "master")
$server.query("CREATE FUNCTION dbo.$tableFunctionName () RETURNS TABLE AS RETURN SELECT 1 as test", "master")
$server.query("CREATE FUNCTION dbo.$scalarFunctionName (@int int) RETURNS INT AS BEGIN RETURN @int END", "master")
$server.query("CREATE RULE dbo.$ruleName AS @range>= 1 AND @range <10;", "master")
}
AfterAll {
$server = Connect-DbaInstance -SqlInstance $script:instance2 -SqlCredential $SqlCredential
$server.query("DROP TABLE dbo.$tableName", "master")
$server.query("DROP VIEW dbo.$viewName", "master")
$server.query("DROP PROCEDURE dbo.$procName", "master")
$server.query("DROP TRIGGER $triggerName ON DATABASE", "master")
$server.query("DROP FUNCTION dbo.$tableFunctionName", "master")
$server.query("DROP FUNCTION dbo.$scalarFunctionName", "master")
$server.query("DROP RULE dbo.$ruleName", "master")
}
Context "works as expected with passthru" {
$script = Export-DbaSysDbUserObject -SqlInstance $script:instance2 -PassThru | Out-String
It "should export text matching table name '$tableName'" {
$script -match $tableName | Should be $true
}
It "should export text matching view name '$viewName'" {
$script -match $viewName | Should be $true
}
It "should export text matching stored procedure name '$procName'" {
$script -match $procName | Should be $true
}
It "should export text matching trigger name '$triggerName'" {
$script -match $triggerName | Should be $true
}
It "should export text matching table function name '$tableFunctionName'" {
$script -match $tableFunctionName | Should be $true
}
It "should export text matching scalar function name '$scalarFunctionName'" {
$script -match $scalarFunctionName | Should be $true
}
It "should export text matching rule name '$ruleName'" {
$script -match $ruleName | Should be $true
}
}
Context "works as expected with filename" {
$null = Export-DbaSysDbUserObject -SqlInstance $script:instance2 -FilePath "C:\Temp\objects_$random.sql"
$file = get-content "C:\Temp\objects_$random.sql" | Out-String
It "should export text matching table name '$tableName'" {
$file -match $tableName | Should be $true
}
It "should export text matching view name '$viewName'" {
$file -match $viewName | Should be $true
}
It "should export text matching stored procedure name '$procName'" {
$file -match $procName | Should be $true
}
It "should export text matching trigger name '$triggerName'" {
$file -match $triggerName | Should be $true
}
It "should export text matching table function name '$tableFunctionName'" {
$file -match $tableFunctionName | Should be $true
}
It "should export text matching scalar function name '$scalarFunctionName'" {
$file -match $scalarFunctionName | Should be $true
}
It "should export text matching scalar function name '$ruleName'" {
$file -match $ruleName | Should be $true
}
}
}