Skip to content

Commit

Permalink
Attached unit tests for Start-SqlMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
msorens committed Nov 15, 2015
1 parent 19d323a commit 45c91cf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions Tests/StartSqlMonitor.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Import-Module "$PSScriptRoot\..\MonitorFactory.psd1"
. "$PSScriptRoot\..\Wrappers\StartSqlMonitor.ps1"

Describe "Start-SqlMonitor" {

Mock Start-Monitor
Context "validates path property" {

It "throws error if path does not exist" {
{ Start-SqlMonitor -path 'C:\doesnotexist.sql' -server a -database b } | Should Throw 'Cannot find path'
Assert-MockCalled Start-Monitor 0 -Scope It
}
It "throws error if file is empty" {
Mock Get-Content { return "" }
{ Start-SqlMonitor -path 'C:\any.sql' -server a -database b } | Should Throw 'no content found'
Assert-MockCalled Start-Monitor 0 -Scope It
}
}
Context "validates other properties" {
It "throws error if server not specified" {
{ Start-SqlMonitor -path 'C:\any.sql' -database b } | Should Throw 'Server must be specified'
}
It "throws error if database not specified" {
{ Start-SqlMonitor -path 'C:\any.sql' -server a } | Should Throw 'Database must be specified'
}
}
Context "uses query" {
It "from file content if specified" {
$content = 'select foo from bar1'
$targetFile = 'C:\any.sql'
Mock Get-Content { return $content } -param { $Path -eq $targetFile }
Start-SqlMonitor -path $targetFile -server a -database b
Assert-MockCalled Start-Monitor 1 -Scope It { $ArgumentList[0] -eq $content }
}
It "from literal if specified" {
$content = 'select foo from bar2'
Start-SqlMonitor -query $content -server a -database b
Assert-MockCalled Start-Monitor 1 -Scope It { $ArgumentList[0] -eq $content }
}
}
}

4 changes: 2 additions & 2 deletions Wrappers/StartSqlMonitor.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
function Start-SqlMonitor (
[Parameter(ParameterSetName='FileQuery', Mandatory)][string]$path,
[Parameter(ParameterSetName='InlineQuery', Mandatory)][string]$query,
[Parameter(Mandatory)][string]$server,
[Parameter(Mandatory)][string]$database,
[string]$server = $(throw 'Server must be specified'),
[string]$database = $(throw 'Database must be specified'),
[string]$title = '',
[string]$interval = '5sec'
)
Expand Down

0 comments on commit 45c91cf

Please sign in to comment.