Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL tests #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Public/Sql.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<#
.SYNOPSIS
Test sql connections
.DESCRIPTION
This function will test connectivity to a SQL server. It does not run any select statements.
.PARAMETER Target
The database computer name to test. Include the instance, if needed.
.PARAMETER Qualifier
The connection string uri
.PARAMETER Property
Specifies a property of the database to test
.PARAMETER Should
A Script Block defining a Pester Assertion.
.EXAMPLE
Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" AccessToken { Should Be $null }
.EXAMPLE
Sql "server60" $ConnectionString State { Should Match "Closed" }
.NOTES
Assertions: Be, BeExactly, Match, Exist
Hint: The connection string can be stored as variable in the declaration file.
$connectionString = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60"
Sql "server60" $ConnectionString AccessToken { Should Be $null }
#>

function Sql {
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=1)]
[Alias('Computername','Computername\Instance')]
[string]$Target,

[PARAMETER(Position=2)]
[Alias('ConnectionString')]
[string]$Qualifier,

[PARAMETER(Position=3)]
[ValidateSet('AccessToken', 'ClientConnectionId', 'ConnectionString', 'ConnectionTimeout', 'Container', 'Credential', 'Database', 'DataSource', 'FireInfoMessageEventOnUserErrors', 'PacketSize', 'ServerVersion', 'Site', 'State', 'StatisticsEnabled', 'WorkstationId')]
[string]$Property,

[Parameter(Mandatory,Position=4)]
[scriptblock]$Should
)

$ConnectionString = $Qualifier

$expression = {New-Object System.Data.SqlClient.SqlConnection '$ConnectionString'}

$params = Get-PoshspecParam -TestName Sql -TestExpression $expression @PSBoundParameters

Invoke-PoshspecExpression @params
}
13 changes: 13 additions & 0 deletions Tests/poshspec.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,19 @@ Describe 'Test Functions' {
$results.Expression | Should Be "GetFeature -Name Telnet-Client | Select-Object -ExpandProperty 'Installed' | Should be `$false"
}
}

Context 'Sql' {

$results = Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportDB;Data Source=server60" State { Should Match "Closed" }

It "Should return the correct test Name" {
$results.Name | Should Be "Sql property 'State' for 'server60' at 'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportDB;Data Source=server60' Should Match `"Closed`""
}

It "Should return the correct test Expression" {
$results.Expression | Should Be "New-Object System.Data.SqlClient.SqlConnection 'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportDB;Data Source=server60' | Select-Object -ExpandProperty 'State' | Should Match `"Closed`""
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions poshspecdemo.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ Describe 'Volume' {
Volume 'ShouldNotExist' { should BeNullOrEmpty}
}

Describe 'Sql' {
Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" Database { Should Be "ReportingDB" }
Sql "server60" "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ReportingDB;Data Source=server60" State { Should Match "Closed" }
}