Skip to content

Commit

Permalink
xSQLServer: Fixed typos and use of wrong parameters in unit tests (#777)
Browse files Browse the repository at this point in the history
- Changes to xSQLServer
  - Fixed typos and the use of wrong parameters in unit tests which was
    found after release of new version of Pester (issue #773).
  • Loading branch information
johlju authored Aug 19, 2017
1 parent f844cc7 commit 4734f66
Show file tree
Hide file tree
Showing 16 changed files with 763 additions and 756 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
1-WaitForASingleClusterGroup.ps1, 2-WaitForMultipleClusterGroups.ps1.
- Updated year to 2017 in license file ([issue #711](https://github.com/PowerShell/xFailOverCluster/issues/711)).
- Code style clean-up throughout the module to align against the Style Guideline.
- Fixed typos and the use of wrong parameters in unit tests which was found
after release of new version of Pester ([issue #773](https://github.com/PowerShell/xFailOverCluster/issues/773)).
- Changes to xSQLServerAlwaysOnService
- Added resource description in README.md.
- Updated parameters descriptions in comment-based help, schema.mof and README.md.
Expand Down
126 changes: 63 additions & 63 deletions Tests/Unit/MSFT_xSQLServerAlias.Tests.ps1

Large diffs are not rendered by default.

470 changes: 235 additions & 235 deletions Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroup.Tests.ps1

Large diffs are not rendered by default.

188 changes: 94 additions & 94 deletions Tests/Unit/MSFT_xSQLServerAlwaysOnAvailabilityGroupReplica.Tests.ps1

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ try
BeforeEach {
$testParameters = $defaultParameters

Mock -CommandName Connect-SQL -MockWith $mockConnectSql -Verifiable -Scope It
Mock -CommandName Connect-SQL -MockWith $mockConnectSql -Verifiable
Mock -CommandName New-SqlAvailabilityGroupListener -MockWith {} -Verifiable
Mock -CommandName Set-SqlAvailabilityGroupListener -MockWith {} -Verifiable
Mock -CommandName Add-SqlAvailabilityGroupListenerStaticIp -MockWith {} -Verifiable
Expand Down
20 changes: 10 additions & 10 deletions Tests/Unit/MSFT_xSQLServerConfiguration.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Import-Module (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\
$TestEnvironment = Initialize-TestEnvironment `
-DSCModuleName $script:DSCModuleName `
-DSCResourceName $script:DSCResourceName `
-TestType Unit
-TestType Unit

$defaultState = @{
SQLServer = 'CLU01'
Expand Down Expand Up @@ -74,7 +74,7 @@ try
Context 'The system is not in the desired state' {

Mock -CommandName Connect-SQL -MockWith {
$mock = New-Object PSObject -Property @{
$mock = New-Object PSObject -Property @{
Configuration = @{
Properties = @(
@{
Expand All @@ -93,7 +93,7 @@ try

## Get the current state
$result = Get-TargetResource @desiredState

It 'Should return the same values as passed' {
$result.SQLServer | Should Be $desiredState.SQLServer
$result.SQLInstanceName | Should Be $desiredState.SQLInstanceName
Expand All @@ -111,7 +111,7 @@ try
Context 'The system is in the desired state' {

Mock -CommandName Connect-SQL -MockWith {
$mock = New-Object PSObject -Property @{
$mock = New-Object PSObject -Property @{
Configuration = @{
Properties = @(
@{
Expand Down Expand Up @@ -148,7 +148,7 @@ try
Context 'Invalid data is supplied' {

Mock -CommandName Connect-SQL -MockWith {
$mock = New-Object PSObject -Property @{
$mock = New-Object PSObject -Property @{
Configuration = @{
Properties = @(
@{
Expand All @@ -174,11 +174,11 @@ try
}

Describe "$($script:DSCResourceName)\Test-TargetResource" {

Mock -CommandName New-VerboseMessage -MockWith {} -ModuleName $script:DSCResourceName

Mock -CommandName Connect-SQL -MockWith {
$mock = New-Object PSObject -Property @{
$mock = New-Object PSObject -Property @{
Configuration = @{
Properties = @(
@{
Expand Down Expand Up @@ -210,7 +210,7 @@ try
Mock -CommandName New-TerminatingError -MockWith {} -ModuleName $script:DSCResourceName

Mock -CommandName Connect-SQL -MockWith {
$mock = New-Object PSObject -Property @{
$mock = New-Object PSObject -Property @{
Configuration = @{
Properties = @(
@{
Expand All @@ -229,7 +229,7 @@ try
} -ModuleName $script:DSCResourceName -Verifiable -ParameterFilter { $SQLServer -eq 'CLU01' }

Mock -CommandName Connect-SQL -MockWith {
$mock = New-Object PSObject -Property @{
$mock = New-Object PSObject -Property @{
Configuration = @{
Properties = @(
@{
Expand Down Expand Up @@ -261,7 +261,7 @@ try
Set-TargetResource @desiredStateRestart
Assert-MockCalled -ModuleName $script:DSCResourceName -CommandName Restart-SqlService -Scope It -Times 1 -Exactly
}

It 'Should warn about restart when required, but not requested' {
Set-TargetResource @desiredState

Expand Down
40 changes: 20 additions & 20 deletions Tests/Unit/MSFT_xSQLServerDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ try
SQLInstanceName = $mockSqlServerInstanceName
SQLServer = $mockSqlServerName
}

#region Function mocks

$mockConnectSQL = {
return @(
(
Expand All @@ -58,14 +58,14 @@ try
Add-Member -MemberType NoteProperty -Name ComputerNamePhysicalNetBIOS -Value $mockSqlServerName -PassThru |
Add-Member -MemberType ScriptProperty -Name Databases -Value {
return @{
$mockSqlDatabaseName = ( New-Object Object |
$mockSqlDatabaseName = ( New-Object Object |
Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseName -PassThru |
Add-Member -MemberType ScriptMethod -Name Drop -Value {
if ($mockInvalidOperationForDropMethod)
{
throw 'Mock Drop Method was called with invalid operation.'
}

if ( $this.Name -ne $mockExpectedDatabaseNameToDrop )
{
throw "Called mocked Drop() method without dropping the right database. Expected '{0}'. But was '{1}'." `
Expand All @@ -74,7 +74,7 @@ try
} -PassThru
)
}
} -PassThru -Force
} -PassThru -Force
)
)
}
Expand All @@ -89,7 +89,7 @@ try
{
throw 'Mock Create Method was called with invalid operation.'
}

if ( $this.Name -ne $mockExpectedDatabaseNameToCreate )
{
throw "Called mocked Create() method without adding the right database. Expected '{0}'. But was '{1}'." `
Expand Down Expand Up @@ -127,7 +127,7 @@ try
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}
}

Context 'When the system is in the desired state for a database' {
It 'Should return the state as present' {
$testParameters = $mockDefaultParameters
Expand All @@ -152,7 +152,7 @@ try

Assert-VerifiableMocks
}

Describe "MSFT_xSQLServerDatabase\Test-TargetResource" -Tag 'Test'{
BeforeEach {
Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable
Expand All @@ -169,7 +169,7 @@ try
$result = Test-TargetResource @testParameters
$result | Should Be $false
}

It 'Should call the mock function Connect-SQL' {
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}
Expand Down Expand Up @@ -228,7 +228,7 @@ try

Assert-VerifiableMocks
}

Describe "MSFT_xSQLServerDatabase\Set-TargetResource" -Tag 'Set'{
BeforeEach {
Mock -CommandName Connect-SQL -MockWith $mockConnectSQL -Verifiable
Expand All @@ -250,13 +250,13 @@ try

{ Set-TargetResource @testParameters } | Should Not Throw
}

It 'Should call the mock function Connect-SQL' {
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}

It 'Should call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.Database' {
Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter {
Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter {
$TypeName -eq 'Microsoft.SqlServer.Management.Smo.Database'
} -Scope Context
}
Expand All @@ -275,7 +275,7 @@ try

{ Set-TargetResource @testParameters } | Should Not Throw
}

It 'Should call the mock function Connect-SQL' {
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}
Expand All @@ -290,19 +290,19 @@ try
Name = 'NewDatabase'
Ensure = 'Present'
}

$throwInvalidOperation = ('InnerException: Exception calling "Create" ' + `
'with "0" argument(s): "Mock Create Method was called with invalid operation."')
{ Set-TargetResource @testParameters } | Should Throw $throwInvalidOperation

{ Set-TargetResource @testParameters } | Should Throw $throwInvalidOperation
}

It 'Should call the mock function Connect-SQL' {
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}

It 'Should call the mock function New-Object with TypeName equal to Microsoft.SqlServer.Management.Smo.Database' {
Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter {
Assert-MockCalled New-Object -Exactly -Times 1 -ParameterFilter {
$TypeName -eq 'Microsoft.SqlServer.Management.Smo.Database'
} -Scope Context
}
Expand All @@ -317,12 +317,12 @@ try
Name = 'AdventureWorks'
Ensure = 'Absent'
}

It 'Should throw the correct error when Drop() method was called with invalid operation' {
$throwInvalidOperation = ('InnerException: Exception calling "Drop" ' + `
'with "0" argument(s): "Mock Drop Method was called with invalid operation."')
{ Set-TargetResource @testParameters } | Should Throw $throwInvalidOperation

{ Set-TargetResource @testParameters } | Should Throw $throwInvalidOperation
}

It 'Should call the mock function Connect-SQL' {
Expand Down
28 changes: 16 additions & 12 deletions Tests/Unit/MSFT_xSQLServerDatabaseOwner.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ try
SQLInstanceName = $mockSqlServerInstanceName
SQLServer = $mockSqlServerName
}
#region Function mocks

#region Function mocks
$mockConnectSQL = {
return @(
(
Expand All @@ -57,16 +57,16 @@ try
Add-Member -MemberType NoteProperty -Name ComputerNamePhysicalNetBIOS -Value $mockSqlServerName -PassThru |
Add-Member -MemberType ScriptProperty -Name Databases -Value {
return @{
$mockSqlDatabaseName = @((
New-Object Object |
$mockSqlDatabaseName = @((
New-Object Object |
Add-Member -MemberType NoteProperty -Name Name -Value $mockSqlDatabaseName -PassThru |
Add-Member -MemberType NoteProperty -Name Owner -Value $mockDatabaseOwner -PassThru |
Add-Member -MemberType ScriptMethod -Name SetOwner -Value {
if ($mockInvalidOperationForSetOwnerMethod)
{
throw 'Mock of method SetOwner() was called with invalid operation.'
}

if ( $this.Owner -ne $mockExpectedDatabaseOwner )
{
throw "Called mocked SetOwner() method without setting the right login. Expected '{0}'. But was '{1}'." `
Expand All @@ -79,11 +79,11 @@ try
Add-Member -MemberType ScriptProperty -Name Logins -Value {
return @{
$mockSqlServerLogin = @((
New-Object Object |
Add-Member -MemberType NoteProperty -Name LoginType -Value $mockSqlServerLoginType -PassThru
New-Object Object |
Add-Member -MemberType NoteProperty -Name LoginType -Value $mockSqlServerLoginType -PassThru
))
}
} -PassThru -Force
} -PassThru -Force
)
)
}
Expand Down Expand Up @@ -158,7 +158,7 @@ try
$result = Test-TargetResource @testParameters
$result | Should Be $false
}

It 'Should call the mock function Connect-SQL' {
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}
Expand Down Expand Up @@ -255,8 +255,12 @@ try
Database = $mockSqlDatabaseName
Name = $mockSqlServerLogin
}

$throwInvalidOperation = ('Failed to set owner named Zebes\SamusAran of the database ' + ` 'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + ` 'Exception calling "SetOwner" with "1" argument(s): "Called mocked ' + ` 'SetOwner() method without setting the right login. ' + ` "Expected 'Zebes\SamusAran'. But was 'Elysia\Chozo'.")

$throwInvalidOperation = ('Failed to set owner named Zebes\SamusAran of the database ' + `
'named AdventureWorks on localhost\MSSQLSERVER. InnerException: ' + `
'Exception calling "SetOwner" with "1" argument(s): "Called mocked ' + `
'SetOwner() method without setting the right login. ' + `
"Expected 'Zebes\SamusAran'. But was 'Elysia\Chozo'.")

{ Set-TargetResource @testParameters } | Should Throw $throwInvalidOperation
}
Expand All @@ -265,7 +269,7 @@ try
Assert-MockCalled Connect-SQL -Exactly -Times 1 -Scope Context
}
}

Context 'When the system is not in the desired state' {
It 'Should throw the correct error when the method SetOwner() was called' {
$mockInvalidOperationForSetOwnerMethod = $true
Expand Down
Loading

0 comments on commit 4734f66

Please sign in to comment.