forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Out-DbaDataTable.Tests.ps1
250 lines (222 loc) · 10.9 KB
/
Out-DbaDataTable.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
Describe "Testing data table output when using a complex object" {
$obj = New-Object -TypeName psobject -Property @{
guid = [system.guid]'32ccd4c4-282a-4c0d-997c-7b5deb97f9e0'
timespan = New-TimeSpan -Start 2016-10-30 -End 2017-04-30
datetime = Get-Date -Year 2016 -Month 10 -Day 30 -Hour 5 -Minute 52 -Second 0 -Millisecond 0
char = [System.Char]'T'
true = $true
false = $false
null = [bool]$null
string = "it's a boy!"
UInt64 = [System.UInt64]123456
}
$innedobj = New-Object -TypeName psobject -Property @{
Mission = 'Keep Hank alive'
}
Add-Member -Force -InputObject $obj -MemberType NoteProperty -Name myobject -Value $innedobj
$result = Out-DbaDataTable -InputObject $obj
Context "Property: guid" {
It 'Has a column called "guid"' {
$result.Columns.ColumnName.Contains('guid') | Should Be $true
}
It 'Has a [guid] data type on the column "guid"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'guid' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'guid'
}
It 'Has the following guid: "32ccd4c4-282a-4c0d-997c-7b5deb97f9e0"' {
$result.guid | Should Be '32ccd4c4-282a-4c0d-997c-7b5deb97f9e0'
}
}
Context "Property: timespan" {
It 'Has a column called "timespan"' {
$result.Columns.ColumnName.Contains('timespan') | Should Be $true
}
It 'Has a [long] data type on the column "timespan"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'timespan' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'Int64'
}
It "Has the following timespan: 15724800000" {
$result.timespan | Should Be 15724800000
}
}
Context "Property: datetime" {
It 'Has a column called "datetime"' {
$result.Columns.ColumnName.Contains('datetime') | Should Be $true
}
It 'Has a [datetime] data type on the column "datetime"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'datetime' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'datetime'
}
It "Has the following datetime: 2016-10-30 05:52:00.000" {
$date = Get-Date -Year 2016 -Month 10 -Day 30 -Hour 5 -Minute 52 -Second 0 -Millisecond 0
$result.datetime -eq $date | Should Be $true
}
}
Context "Property: char" {
It 'Has a column called "char"' {
$result.Columns.ColumnName.Contains('char') | Should Be $true
}
It 'Has a [char] data type on the column "char"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'char' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'char'
}
It "Has the following char: T" {
$result.char | Should Be "T"
}
}
Context "Property: true" {
It 'Has a column called "true"' {
$result.Columns.ColumnName.Contains('true') | Should Be $true
}
It 'Has a [bool] data type on the column "true"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'true' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'boolean'
}
It "Has the following bool: true" {
$result.true | Should Be $true
}
}
Context "Property: false" {
It 'Has a column called "false"' {
$result.Columns.ColumnName.Contains('false') | Should Be $true
}
It 'Has a [bool] data type on the column "false"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'false' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'boolean'
}
It "Has the following bool: false" {
$result.false | Should Be $false
}
}
Context "Property: null" {
It 'Has a column called "null"' {
$result.Columns.ColumnName.Contains('null') | Should Be $true
}
It 'Has a [bool] data type on the column "null"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'null' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'boolean'
}
It "Has the following bool: false" {
$result.null | Should Be $false #should actually be $null but its hard to compare :)
}
}
Context "Property: string" {
It 'Has a column called "string"' {
$result.Columns.ColumnName.Contains('string') | Should Be $true
}
It 'Has a [string] data type on the column "string"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'string' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'string'
}
It "Has the following string: it's a boy!" {
$result.string | Should Be "it's a boy!"
}
}
Context "Property: UInt64" {
It 'Has a column called "UInt64"' {
$result.Columns.ColumnName.Contains('UInt64') | Should Be $true
}
It 'Has a [UInt64] data type on the column "UInt64"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'UInt64' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'UInt64'
}
It "Has the following number: 123456" {
$result.UInt64 | Should Be 123456
}
}
Context "Property: myobject" {
It 'Has a column called "myobject"' {
$result.Columns.ColumnName.Contains('myobject') | Should Be $true
}
It 'Has a [string] data type on the column "myobject"' {
$result.Columns | Where-Object -Property 'ColumnName' -eq 'myobject' | Select-Object -ExpandProperty 'DataType' | Select-Object -ExpandProperty Name | Should Be 'String'
}
It "Has no value" {
# not sure if this is a feaure. Should probably be changed in the future
$result.myobject.GetType().FullName | Should Be "System.DBNull"
}
}
}
Describe "Testing input parameters" {
$obj = New-Object -TypeName psobject -Property @{
timespan = New-TimeSpan -Start 2017-01-01 -End 2017-01-02
}
Context "Verifying TimeSpanType" {
It "Should return '1.00:00:00' when String is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType String).Timespan | Should Be '1.00:00:00'
}
It "Should return 864000000000 when Ticks is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType Ticks).Timespan | Should Be 864000000000
}
It "Should return 1 when TotalDays is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType TotalDays).Timespan | Should Be 1
}
It "Should return 24 when TotalHours is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType TotalHours).Timespan | Should Be 24
}
It "Should return 86400000 when TotalMilliseconds is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType TotalMilliseconds).Timespan | Should Be 86400000
}
It "Should return 1440 when TotalMinutes is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType TotalMinutes).Timespan | Should Be 1440
}
It "Should return 86400 when TotalSeconds is used" {
(Out-DbaDataTable -InputObject $obj -TimeSpanType TotalSeconds).Timespan | Should Be 86400
}
}
Context "Verifying IgnoreNull" {
# To be able to force null
function returnnull {
[CmdletBinding()]
param ()
New-Object -TypeName psobject -Property @{ Name = [int]1 }
$null
New-Object -TypeName psobject -Property @{ Name = [int]3 }
}
function returnOnlynull {
[CmdletBinding()]
param ()
$null
}
It "Returns message if Null value in pipeline when IgnoreNull is set" {
$null = returnnull | Out-DbaDataTable -IgnoreNull -WarningVariable warn -WarningAction SilentlyContinue
$warn.message | Should Match 'The InputObject from the pipe is null'
}
It "Returns warning if Null value in array when IgnoreNull is set" {
$null = Out-DbaDataTable -InputObject (returnnull) -IgnoreNull -WarningVariable warn -WarningAction SilentlyContinue
$warn.message | Should Match 'Object in array is null'
}
It "Does not create row if null is in array when IgnoreNull is set" {
$result = Out-DbaDataTable -InputObject (returnnull) -IgnoreNull -WarningAction SilentlyContinue
$result.Rows.Count | Should Be 2
}
It "Does not create row if null is in pipeline when IgnoreNull is set" {
$result = returnnull | Out-DbaDataTable -IgnoreNull -WarningAction SilentlyContinue
$result.Rows.Count | Should Be 2
}
It "Returns empty row when null value is provided (without IgnoreNull)" {
$result = Out-DbaDataTable -InputObject (returnnull)
$result.Name[0] | Should Be 1
$result.Name[1].GetType().FullName | Should Be 'System.DBNull'
$result.Name[2] | Should Be 3
}
It "Returns empty row when null value is passed in pipe (without IgnoreNull)" {
$result = returnnull | Out-DbaDataTable
$result.Name[0] | Should Be 1
$result.Name[1].GetType().FullName | Should Be 'System.DBNull'
$result.Name[2] | Should Be 3
}
}
Context "Verifying Silent" {
# To be able to force null
function returnnull {
New-Object -TypeName psobject -Property @{ Name = 1 }
$null
New-Object -TypeName psobject -Property @{ Name = 3 }
}
It "Suppresses warning messages when Silent is used" {
$null = Out-DbaDataTable -InputObject (returnnull) -IgnoreNull -Silent -WarningVariable warn -WarningAction SilentlyContinue
$warn.message -eq $null | Should Be $true
}
}
Context "Verifying script properties returning null" {
It "Returns string column if a script property returns null" {
$myobj = New-Object -TypeName psobject -Property @{ Name = 'Test' }
$myobj | Add-Member -Force -MemberType ScriptProperty -Name ScriptNothing -Value { $null }
$r = Out-DbaDataTable -InputObject $myobj
($r.Columns | Where-Object ColumnName -eq ScriptNothing | Select-Object -ExpandProperty DataType).ToString() | Should Be 'System.String'
}
}
}