-
-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathInvoke-SQLiteQuery.Tests.ps1
More file actions
164 lines (118 loc) · 6.89 KB
/
Copy pathInvoke-SQLiteQuery.Tests.ps1
File metadata and controls
164 lines (118 loc) · 6.89 KB
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
#handle PS2
if(-not $PSScriptRoot)
{
$PSScriptRoot = Split-Path $MyInvocation.MyCommand.Path -Parent
}
$Verbose = @{}
if($env:APPVEYOR_REPO_BRANCH -and $env:APPVEYOR_REPO_BRANCH -notlike "master")
{
$Verbose.add("Verbose",$True)
}
$PSVersion = $PSVersionTable.PSVersion.Major
Import-Module $PSScriptRoot\..\PSSQLite -Force
$SQLiteFile = "$PSScriptRoot\Working.SQLite"
Remove-Item $SQLiteFile -force -ErrorAction SilentlyContinue
Copy-Item $PSScriptRoot\Names.SQLite $PSScriptRoot\Working.SQLite -force
Describe "New-SQLiteConnection PS$PSVersion" {
Context 'Strict mode' {
Set-StrictMode -Version latest
It 'should create a connection' {
$Script:Connection = New-SQLiteConnection @Verbose -DataSource :MEMORY:
$Script:Connection.ConnectionString | Should be "Data Source=:MEMORY:;"
$Script:Connection.State | Should be "Open"
}
}
}
Describe "Invoke-SQLiteQuery PS$PSVersion" {
Context 'Strict mode' {
Set-StrictMode -Version latest
It 'should take file input' {
$Out = @( Invoke-SqliteQuery @Verbose -DataSource $SQLiteFile -InputFile $PSScriptRoot\Test.SQL )
$Out.count | Should be 2
$Out[1].OrderID | Should be 500
}
It 'should take query input' {
$Out = @( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "PRAGMA table_info(NAMES)" -ErrorAction Stop )
$Out.count | Should Be 4
$Out[0].Name | SHould Be "fullname"
}
It 'should support parameterized queries' {
$Out = @( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "SELECT * FROM NAMES WHERE BirthDate >= @Date" -SqlParameters @{
Date = (Get-Date -Year 2012 -Month 3 -Day 13)
} -ErrorAction Stop )
$Out.count | Should Be 1
$Out[0].fullname | Should Be "Cookie Monster"
$Out = @( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "SELECT * FROM NAMES WHERE BirthDate >= @Date" -SqlParameters @{
Date = (Get-Date -Year 2012 -Month 3 -Day 15)
} -ErrorAction Stop )
$Out.count | Should Be 0
}
It 'should use existing SQLiteConnections' {
Invoke-SqliteQuery @Verbose -SQLiteConnection $Script:Connection -Query "CREATE TABLE OrdersToNames (OrderID INT PRIMARY KEY, fullname TEXT);"
Invoke-SqliteQuery @Verbose -SQLiteConnection $Script:Connection -Query "INSERT INTO OrdersToNames (OrderID, fullname) VALUES (1,'Cookie Monster');"
@( Invoke-SqliteQuery @Verbose -SQLiteConnection $Script:Connection -Query "PRAGMA STATS" ) |
Select -first 1 -ExpandProperty table |
Should be 'OrdersToNames'
$Script:COnnection.State | Should Be Open
$Script:Connection.close()
}
It 'should respect PowerShell expectations for null' {
#The SQL folks out there might be annoyed by this, but we want to treat DBNulls as null to allow expected PowerShell operator behavior.
$Connection = New-SQLiteConnection -DataSource :MEMORY:
Invoke-SqliteQuery @Verbose -SQLiteConnection $Connection -Query "CREATE TABLE OrdersToNames (OrderID INT PRIMARY KEY, fullname TEXT);"
Invoke-SqliteQuery @Verbose -SQLiteConnection $Connection -Query "INSERT INTO OrdersToNames (OrderID, fullname) VALUES (1,'Cookie Monster');"
Invoke-SqliteQuery @Verbose -SQLiteConnection $Connection -Query "INSERT INTO OrdersToNames (OrderID) VALUES (2);"
@( Invoke-SqliteQuery @Verbose -SQLiteConnection $Connection -Query "SELECT * FROM OrdersToNames" -As DataRow | Where{$_.fullname}).count |
Should Be 2
@( Invoke-SqliteQuery @Verbose -SQLiteConnection $Connection -Query "SELECT * FROM OrdersToNames" | Where{$_.fullname} ).count |
Should Be 1
}
}
}
Describe "Out-DataTable PS$PSVersion" {
Context 'Strict mode' {
Set-StrictMode -Version latest
It 'should create a DataTable' {
$Script:DataTable = 1..1000 | %{
New-Object -TypeName PSObject -property @{
fullname = "Name $_"
surname = "Name"
givenname = "$_"
BirthDate = (Get-Date).Adddays(-$_)
} | Select fullname, surname, givenname, birthdate
} | Out-DataTable @Verbose
$Script:DataTable.GetType().Fullname | Should Be 'System.Data.DataTable'
@($Script:DataTable.Rows).Count | Should Be 1000
$Columns = $Script:DataTable.Columns | Select -ExpandProperty ColumnName
$Columns[0] | Should Be 'fullname'
$Columns[3] | Should Be 'BirthDate'
$Script:DataTable.columns[3].datatype.fullname | Should Be 'System.DateTime'
}
}
}
Describe "Invoke-SQLiteBulkCopy PS$PSVersion" {
Context 'Strict mode' {
Set-StrictMode -Version latest
It 'should insert data' {
Invoke-SQLiteBulkCopy @Verbose -DataTable $Script:DataTable -DataSource $SQLiteFile -Table Names -NotifyAfter 100 -force
@( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "SELECT fullname FROM NAMES WHERE surname = 'Name'" ).count | Should Be 1000
}
It "should adhere to ConflictCause" {
#Basic set of tests, need more...
#Try adding same data
{ Invoke-SQLiteBulkCopy @Verbose -DataTable $Script:DataTable -DataSource $SQLiteFile -Table Names -NotifyAfter 100 -force } | Should Throw
#Change a known row's prop we can test to ensure it does or does not change
$Script:DataTable.Rows[0].surname = "Name 1"
{ Invoke-SQLiteBulkCopy @Verbose -DataTable $Script:DataTable -DataSource $SQLiteFile -Table Names -NotifyAfter 100 -force } | Should Throw
$Result = @( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "SELECT surname FROM NAMES WHERE fullname = 'Name 1'")
$Result[0].surname | Should Be 'Name'
{ Invoke-SQLiteBulkCopy @Verbose -DataTable $Script:DataTable -DataSource $SQLiteFile -Table Names -NotifyAfter 100 -ConflictClause Rollback -Force } | Should Throw
$Result = @( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "SELECT surname FROM NAMES WHERE fullname = 'Name 1'")
$Result[0].surname | Should Be 'Name'
Invoke-SQLiteBulkCopy @Verbose -DataTable $Script:DataTable -DataSource $SQLiteFile -Table Names -NotifyAfter 100 -ConflictClause Replace -Force
$Result = @( Invoke-SQLiteQuery @Verbose -Database $SQLiteFile -Query "SELECT surname FROM NAMES WHERE fullname = 'Name 1'")
$Result[0].surname | Should Be 'Name 1'
}
}
}
Remove-Item $SQLiteFile -force -ErrorAction SilentlyContinue