Skip to content

Commit cfde731

Browse files
committed
✅ Add database export test (and testability)
1 parent 97dbf1b commit cfde731

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

Export-DatabaseScripts.ps1

+5-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ Outputs SQL scripts to files using the default options.
2727

2828
#Requires -Version 3
2929
#Requires -Modules dbatools
30+
using namespace Microsoft.SqlServer.Management.Smo
3031
[CmdletBinding()][OutputType([void])] Param(
3132
# The database from which to export scripts.
32-
[Parameter(ValueFromPipeline=$true,Mandatory=$true)]
33-
[Microsoft.SqlServer.Management.Smo.Database] $Database,
33+
[Parameter(ValueFromPipeline=$true,Mandatory=$true)][Database] $Database,
3434
# Controls how the scripts are generated.
35-
[Microsoft.SqlServer.Management.Smo.ScriptingOptions] $Options = (New-DbaScriptingOption)
35+
[ScriptingOptions] $Options = (New-DbaScriptingOption)
3636
)
3737
Begin
3838
{
@@ -61,8 +61,7 @@ Begin
6161
{
6262
[CmdletBinding()] Param(
6363
[Parameter(Position=0,Mandatory=$true)][string] $Subfolder,
64-
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
65-
[Microsoft.SqlServer.Management.Smo.ScriptNameObjectBase] $InputObject
64+
[Parameter(Mandatory=$true,ValueFromPipeline=$true)][ScriptNameObjectBase] $InputObject
6665
)
6766
if(!$InputObject -or ($InputObject |Test-SystemObject)) {return}
6867
$InputObject |Export-DbaScript -ScriptingOptionsObject $Options -FilePath ($InputObject |Get-ScriptName $Subfolder)
@@ -86,7 +85,7 @@ Begin
8685
$Database.AsymmetricKeys |Export-DatabaseScript 'Security/Asymmetric Keys'
8786
$Database.Certificates |Export-DatabaseScript 'Security/Certificates'
8887
$Database.Roles |
89-
Where-Object {$_ -isnot [Microsoft.SqlServer.Management.Smo.DatabaseRole] -or !$_.IsFixedRole} |
88+
Where-Object {$_ -isnot [DatabaseRole] -or !$_.IsFixedRole} |
9089
Export-DatabaseScript 'Security/Roles'
9190
$Database.Schemas |Export-DatabaseScript 'Security/Schemas'
9291
$Database.SymmetricKeys |Export-DatabaseScript 'Security/Symmetric Keys'

test/Add-NotebookCell.Tests.ps1

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Describe 'Add-NotebookCell' -Tag Add-NotebookCell -Skip:$skip {
1111
BeforeAll {
1212
$scriptsdir,$sep = (Split-Path $PSScriptRoot),[io.path]::PathSeparator
1313
if($scriptsdir -notin ($env:Path -split $sep)) {$env:Path += "$sep$scriptsdir"}
14-
Add-Type -TypeDefinition @'
14+
try {[void][Kernel]}
15+
catch {Add-Type -TypeDefinition @'
1516
using System.Collections.Generic;
1617
public record SendEditableCode(string Language, string Content);
1718
public static class Kernel
@@ -23,7 +24,7 @@ public static class Kernel
2324
}
2425
public static RootMock Root = new RootMock();
2526
}
26-
'@
27+
'@}
2728
}
2829
Context 'When run within a Polyglot Notebook, appends a cell to it' -Tag AddNoteboodCell,Add,NotebookCell,Notebook {
2930
It "Adding language '<Language>' code '<Code>' should happen" -TestCases @(

test/Export-DatabaseScripts.Tests.ps1

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<#
2+
.SYNOPSIS
3+
Tests exporting MS SQL database objects from the given server and database as files, into a consistent folder structure.
4+
#>
5+
6+
$basename = "$(($MyInvocation.MyCommand.Name -split '\.',2)[0])."
7+
$skip = !(Test-Path .changes -Type Leaf) ? $false :
8+
!@(Get-Content .changes |Get-Item |Select-Object -ExpandProperty Name |Where-Object {$_.StartsWith($basename)})
9+
if($skip) {Write-Information "No changes to $basename" -infa Continue}
10+
Describe 'Export-DatabaseScripts' -Tag Export-DatabaseScripts -Skip:$skip {
11+
BeforeAll {
12+
$scriptsdir,$sep = (Split-Path $PSScriptRoot),[io.path]::PathSeparator
13+
if($scriptsdir -notin ($env:Path -split $sep)) {$env:Path += "$sep$scriptsdir"}
14+
Mock Export-DbaScript {}
15+
try {[void][MockObject]}
16+
catch {Add-Type -TypeDefinition @'
17+
public class ScriptNameObjectBase {}
18+
public class MockObject : ScriptNameObjectBase
19+
{
20+
public string Name { get; } = $"ObjectName{System.DateTime.Now.Microsecond}";
21+
public string Schema { get; } = "dbo";
22+
public bool IsSystemObject { get; }
23+
}
24+
public class Database : MockObject
25+
{
26+
public bool ServiceBroker { get; }
27+
public MockObject[] Assemblies { get; } = new MockObject[] {};
28+
public MockObject[] Triggers { get; } = new MockObject[] {};
29+
public MockObject[] Defaults { get; } = new MockObject[] {};
30+
public MockObject[] ExtendedProperties { get; } = new MockObject[] {};
31+
public MockObject[] UserDefinedFunctions { get; } = new MockObject[] {};
32+
public MockObject[] Rules { get; } = new MockObject[] {};
33+
public MockObject[] AsymmetricKeys { get; } = new MockObject[] {};
34+
public MockObject[] Certificates { get; } = new MockObject[] {};
35+
public MockObject[] Roles { get; } = new MockObject[] {};
36+
public MockObject[] Schemas { get; } = new MockObject[] {};
37+
public MockObject[] SymmetricKeys { get; } = new MockObject[] {};
38+
public MockObject[] Users { get; } = new MockObject[] {};
39+
public MockObject[] Sequences { get; } = new MockObject[] {};
40+
public MockObject[] FullTextCatalogs { get; } = new MockObject[] {};
41+
public MockObject[] FullTextStopLists { get; } = new MockObject[] {};
42+
public MockObject[] PartitionFunctions { get; } = new MockObject[] {};
43+
public MockObject[] PartitionSchemes { get; } = new MockObject[] {};
44+
public MockObject[] StoredProcedures { get; } = new MockObject[] { new MockObject() };
45+
public MockObject[] Synonyms { get; } = new MockObject[] {};
46+
public MockObject[] Tables { get; } = new MockObject[] { new MockObject() };
47+
public MockObject[] UserDefinedDataTypes { get; } = new MockObject[] {};
48+
public MockObject[] XmlSchemaCollections { get; } = new MockObject[] {};
49+
public MockObject[] Views { get; } = new MockObject[] { new MockObject() };
50+
}
51+
'@}
52+
}
53+
Context 'Exports MS SQL database objects from the given server and database as files, into a consistent folder structure' `
54+
-Tag ExportDatabaseScripts,Export,DatabaseScripts,Database,SQL {
55+
It "Export scripts" {
56+
New-Object Database |Export-DatabaseScripts.ps1
57+
Assert-MockCalled -CommandName Export-DbaScript -Times 3
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)