-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqlServer.tests.ps1
95 lines (92 loc) · 6.08 KB
/
sqlServer.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
# This was written to test against databases keeping call data records for Skype for business.
# At some point I will replace it with a more generic SQL database test.
Describe "Connect to and query SQL Server " {
BeforeAll {
$tableName = "CallType"
$fieldName1 = "CallType" #Must be a name used to test wild card
$fieldName2 = "CallTypeId" #Test for values 2,3,4
$dbName = "LcsCDR"
$sessionName = "LcsCDR"
$sqlconn = "bp1xeucc023"
$End = [datetime]::Now ;
$Start = $End.AddHours(-1)
$ArbitrarySQL = "exec dbo.CdrP2PSessionList @_StartTime ='" + $Start.ToString("yyyy-MM-dd HH:mm") + "', @_EndTime ='" + $End.ToString("yyyy-MM-dd HH:mm") + "'"
$session = Get-SQL -MSSqlServer -Connection $sqlconn -use $dbName -Session $sessionName -ForceNew }
It "Creates a PowerShell alias, matching the session name '$sessionName'" {
{Get-Alias -Name $sessionName} | Should -not -throw
(invoke-command -ScriptBlock ([scriptblock]::Create("$sessionname")) ).database | Should -Be $sessionName
}
It "Creates an open session in `$DBSessions, named '$sessionName'" {
$DbSessions["$sessionName"].State | Should -Be "Open"
}
It "Can select a database using the -USE Alias" {
$DbSessions["$sessionName"].database | Should -Be $dbName
}
It "Can show tables in the database" {
(Get-SQL -Session $sessionName -ShowTables ).Count | Should -BeGreaterThan 0
}
It "Can describe the fields in the table [$tableName]" {
(Get-SQL -Session $sessionName -Describe $tableName ).Count | Should -BeGreaterThan 0
}
It "Can return the [whole] table [$tableName]" {
(Get-SQL -Session $sessionName -Quiet -Table $tableName ).Count | Should -BeGreaterThan 0
}
It "Can run abritrary SQL as passed as via the pipe" {
($ArbitrarySQL | Get-SQL -Session $sessionName -Quiet ).Count | Should -BeGreaterThan 0
}
It "Can run abritrary SQL as passed as a parameter" {
(Get-SQL -Session $sessionName -Quiet $ArbitrarySQL ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with -Select, -Distinct, -OrderBy and -Where parameters" {
(Get-SQL -Session $sessionName -Quiet -Table $tableName -Select $fieldName1 `
-Distinct -OrderBy $fieldName1 -Where $fieldName2 -gt 0 ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with -Select, -Distinct, -OrderBy and -Where parameters, and values for where condition Piped " {
(2,3,4 |
Get-SQL -Session $sessionName -Quiet -Table $tableName -Select $fieldName1 `
-Distinct -OrderBy $fieldName1 -Where $fieldName2 -eq ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with -Select, -Distinct, -OrderBy and -Where parameters and where condition piped " {
("=2","=3",">=4" |
Get-SQL -Session $sessionName -Quiet -Table $tableName -Select $fieldName1 `
-Distinct -OrderBy $fieldName1 -Where $fieldName2 ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with -Select, -Distinct and -OrderBy parameters and WHERE... clause piped " {
("Where $fieldName2 =2","Where $fieldName2 =3","Where $fieldName2 >=4" |
Get-SQL -Session $sessionName -Quiet -Table $tableName -Select $fieldName1 `
-Distinct -OrderBy $fieldName1 ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with the WHERE... clause piped but no -Select, -Distinct or -OrderBy " {
("Where $fieldName2 =2","Where $fieldName2 =3","Where $fieldName2 >=4" |
Get-SQL -Session $sessionName -Quiet -Table $tableName ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with multiple fields in -Select and -OrderBy" {
( Get-SQL -Session $sessionName -Quiet -Table $tableName -Select $fieldName1,
$fieldName2 -OrderBy $fieldName1, $fieldName2 ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with -Select holding a 'Top' clause " {
( Get-SQL -Session $sessionName -Quiet -Table $tableName -Select "Top 5 *" `
-OrderBy $fieldName1,$fieldName2 ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with a different final clause (e.g. 'order by') as a parameter " {
( Get-SQL -Session $sessionName -Quiet `
-Table $tableName "order by $fieldName1 " ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with a different final clause piped " {
("order by $fieldName1 " |
Get-SQL -Session $sessionName -Quiet -Table $tableName ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT ... WHERE ... LIKE query with 'naked' syntax and translate * as a wildcard" {
( SQL -Session $sessionName -Quiet -Select CallType,CallTypeId `
-From CallType -Where CallType -Like audio* ).Count | Should -BeGreaterThan 0
}
It "Can run a SELECT query with a date object as a value for where, -GroupBy and both fieldName & aggreate function in -Select " {
( Get-SQL -Session $sessionname -Quiet -Table "Registration" -Select RegistrarId,
"Count(*) As total" -Where "RegisterTime" -GT ([datetime]::Today) `
-GroupBy "RegistrarId" ).Count | Should -BeGreaterThan 0
}
It "Can add a row to a table" {} -Pending
It "Can Delete a row from a table" {} -Pending
It "Can Change a row in a table" {} -Pending
AfterAll {Get-Sql -Session $sessionName -Close }
}