# http://stackoverflow.com/questions/1183183/path-of-currently-executing-powershell-script
$root = Split-Path $MyInvocation.MyCommand.Path -Parent;

if($script:InvokePester -eq $false -or $null -eq $script:InvokePester) {
	Import-Module DatabaseModule -Force
	Import-Module Pester
}

Describe -Tag "Unit","Public" -Name "Convert-DbUpdateOutputToErrors" {

    
	BeforeAll {
		$script:originalConnectionString = $global:DatabaseModule.ConnectionString;
		$global:DatabaseModule.ConnectionString = "localhost/somethingsomething"
	}
	
	InModuleScope "DatabaseModule" {

        It "Errors" {
			$outputString = @"
Switching database context to 'iSIS'.
RuntimeException: Msg 156, Level 15, State 1, Server XXXXX, Line 8
Incorrect syntax near the keyword 'select'.
"@
			$output = $outputString -split [Environment]::NewLine

			$result = Convert-DbUpdateOutputToErrors -Output $output
			
			@($result).Count | Should Be 1
			$result[0].ErrorMessage | Should Be "Incorrect syntax near the keyword 'select'."
		}

		It "Errors - start in middle" {
			$outputString = @"
Changed database context to 'ODS_Ext'. 
Msg 2714, Level 16, State 3, Server XXXXX, Procedure usp_XXXXX_FlagAllGradStudentsByStartQuarter_AllQueues, Line 2 
There is already an object named 'usp_XXXXX_FlagAllGradStudentsByStartQuarter_AllQueues' in the database.
"@

			$output = $outputString -split [Environment]::NewLine

			$result = Convert-DbUpdateOutputToErrors -Output $output
			
			@($result).Count | Should Be 1
			$result[0].ErrorMessage | Should Be "There is already an object named 'usp_XXXXX_FlagAllGradStudentsByStartQuarter_AllQueues' in the database."
		}

		It "No Errors" {
			$outputString = @"
Switching database context to 'XXXX'.
"@
			$output = $outputString -split [Environment]::NewLine

			$result = Convert-DbUpdateOutputToErrors -Output $output
			
			@($result).Count | Should Be 0
		}

	AfterAll {
		$global:DatabaseModule.ConnectionString = $script:originalConnectionString;
	}

}