Skip to content

Commit

Permalink
Merge branch 'development' into fourthbatch
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Oct 27, 2024
2 parents 7f46eb1 + 2740d7d commit 413d0df
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 120 deletions.
50 changes: 26 additions & 24 deletions .aider/prompts/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,27 @@ Describe "Add-DbaAgReplica" -Tag "IntegrationTests" {

```powershell
Describe "Get-DbaDatabase" -Tag "UnitTests" {
Context "Parameter validation" {
BeforeAll {
$command = Get-Command Get-DbaDatabase
$expected = $TestConfig.CommonParameters
$expected += @(
"SqlInstance",
"SqlCredential",
"Database",
"Confirm",
"WhatIf"
)
}
It "Has parameter: <_>" -ForEach $expected {
$command | Should -HaveParameter $PSItem
}
It "Should have exactly the number of expected parameters ($($expected.Count))" {
$hasparms = $command.Parameters.Values.Name
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
}
}
BeforeAll {
$command = Get-Command Get-DbaDatabase
$expected = $TestConfig.CommonParameters
$expected += @(
"SqlInstance",
"SqlCredential",
"Database",
"Confirm",
"WhatIf"
)
}
Context "Parameter validation" {
It "Has parameter: <_>" -ForEach $expected {
$command | Should -HaveParameter $PSItem
}
It "Should have exactly the number of expected parameters ($($expected.Count))" {
$hasparms = $command.Parameters.Values.Name
Compare-Object -ReferenceObject $expected -DifferenceObject $hasparms | Should -BeNullOrEmpty
}
}
}
```

Expand Down Expand Up @@ -177,7 +176,7 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
### Test Structure
- Parameter validation must be tagged as Unit Test
- No loose code outside of proper test blocks
- Maintain all instance reference comments (#TestConfig.instance3, etc.)
- Must maintain all instance reference comments (#TestConfig.instance3, etc.)

### Syntax Requirements
- Use $PSItem instead of $_
Expand All @@ -187,4 +186,7 @@ Describe "Get-DbaDatabase" -Tag "IntegrationTests" {
### Must Not Use
- $MyInvocation.MyCommand.Name for command names
- Old knownParameters validation approach
- Assumed parameter names - match original tests exactly
- Assumed parameter names - match original tests exactly

# Important
ALL comments must be preserved exactly as they appear in the original code, including seemingly unrelated or end-of-file comments. Even comments that appear to be development notes or temporary must be kept. This is especially important for comments related to CI/CD systems like AppVeyor.
60 changes: 22 additions & 38 deletions tests/Copy-DbaCustomError.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,61 +36,45 @@ Describe "Copy-DbaCustomError" -Tag "UnitTests" {

Describe "Copy-DbaCustomError" -Tag "IntegrationTests" {
BeforeAll {
$primaryServer = Connect-DbaInstance -SqlInstance $TestConfig.instance2 -Database master

# Add test messages in English and French
$messageParams = @{
msgnum = 60000
severity = 16
englishText = "The item named %s already exists in %s."
frenchText = "L'élément nommé %1! existe déjà dans %2!"
}

$primaryServer.Query("EXEC sp_addmessage @msgnum = $($messageParams.msgnum),
@severity = $($messageParams.severity),
@msgtext = N'$($messageParams.englishText)',
@lang = 'us_english'")

$primaryServer.Query("EXEC sp_addmessage @msgnum = $($messageParams.msgnum),
@severity = $($messageParams.severity),
@msgtext = N'$($messageParams.frenchText)',
@lang = 'French'")
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance2 -Database master
$server.Query("IF EXISTS (SELECT 1 FROM sys.messages WHERE message_id = 60000) EXEC sp_dropmessage @msgnum = 60000, @lang = 'all'")
$server.Query("EXEC sp_addmessage @msgnum = 60000, @severity = 16, @msgtext = N'The item named %s already exists in %s.', @lang = 'us_english'")
$server.Query("EXEC sp_addmessage @msgnum = 60000, @severity = 16, @msgtext = N'L''élément nommé %1! existe déjà dans %2!', @lang = 'French'")
}

AfterAll {
$serversToClean = @($TestConfig.instance2, $TestConfig.instance3)
foreach ($serverInstance in $serversToClean) {
$cleanupServer = Connect-DbaInstance -SqlInstance $serverInstance -Database master
$cleanupServer.Query("EXEC sp_dropmessage @msgnum = 60000, @lang = 'all'")
$cleanupServer.Query("IF EXISTS (SELECT 1 FROM sys.messages WHERE message_id = 60000) EXEC sp_dropmessage @msgnum = 60000, @lang = 'all'")
}
}

Context "When copying custom errors" {
It "Should successfully copy custom error messages" {
$copyResults = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000
BeforeEach {
# Clean destination before each test
$destServer = Connect-DbaInstance -SqlInstance $TestConfig.instance3 -Database master
$destServer.Query("IF EXISTS (SELECT 1 FROM sys.messages WHERE message_id = 60000) EXEC sp_dropmessage @msgnum = 60000, @lang = 'all'")
}

$copyResults.Name.Count | Should -Be 2
$copyResults.Name[0] | Should -BeExactly "60000:'us_english'"
# the French message broke Pester v5 encoding so we're using -Match instead of -BeExactly
# Expected @('60000:'us_english'', '60000:'Français''), but got @(60000:'us_english', 60000:'Français').
$copyResults.Name[1] | Should -Match "60000:'Fran"
$copyResults.Status | Should -BeExactly @('Successful', 'Successful')
It "Should successfully copy custom error messages" {
$results = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000
$results.Name[0] | Should -Be "60000:'us_english'"
$results.Name[1] | Should -Match "60000\:'Fran"
$results.Status | Should -Be @("Successful", "Successful")
}

It "Should skip existing custom errors" {
$duplicateResults = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000

$duplicateResults.Name.Count | Should -Be 2
$duplicateResults.Name[0] | Should -BeExactly "60000:'us_english'"
# the French message broke Pester v5 so we're using -Match instead of -BeExactly
# Expected @('60000:'us_english'', '60000:'Français''), but got @(60000:'us_english', 60000:'Français').
$duplicateResults.Name[1] | Should -Match "60000:'Fran"
$duplicateResults.Status | Should -BeExactly @('Skipped', 'Skipped')
Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000
$results = Copy-DbaCustomError -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -CustomError 60000
$results.Name[0] | Should -Be "60000:'us_english'"
$results.Name[1] | Should -Match "60000\:'Fran"
$results.Status | Should -Be @("Skipped", "Skipped")
}

It "Should verify custom error exists" {
$customErrors = Get-DbaCustomError -SqlInstance $TestConfig.instance2
$customErrors.ID | Should -Contain 60000
$results = Get-DbaCustomError -SqlInstance $TestConfig.instance2
$results.ID | Should -Contain 60000
}
}
}
29 changes: 14 additions & 15 deletions tests/Copy-DbaLinkedServer.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,20 @@ Describe "Copy-DbaLinkedServer" -Tag "IntegrationTests" {
$results.Status | Should -BeExactly "Skipped"
}

# SQLNCLI10 and SQLNCLI11 are not used on newer versions, not sure which versions, but skipping if later than 2017
It "Upgrades SQLNCLI provider based on what is registered" -Skip:($server1.VersionMajor -gt 14 -or $server2.VersionMajor -gt 14) {
$upgradeSplat = @{
Source = $TestConfig.instance2
Destination = $TestConfig.instance3
LinkedServer = 'dbatoolsci_localhost2'
UpgradeSqlClient = $true
}
$result = Copy-DbaLinkedServer @upgradeSplat
# SQLNCLI10 and SQLNCLI11 are not used on newer versions, not sure which versions, but skipping if later than 2017
Context "When upgrading SQL Client provider" -Skip:($server1.VersionMajor -gt 14 -or $server2.VersionMajor -gt 14) {
BeforeAll {
$result = Copy-DbaLinkedServer -Source $TestConfig.instance2 -Destination $TestConfig.instance3 -LinkedServer dbatoolsci_localhost2 -UpgradeSqlClient
$sourceLinked = Get-DbaLinkedServer -SqlInstance $TestConfig.instance2
$destLinked = Get-DbaLinkedServer -SqlInstance $TestConfig.instance3
}

$server1 = Connect-DbaInstance -SqlInstance $TestConfig.instance2
$server2 = Connect-DbaInstance -SqlInstance $TestConfig.instance3
It "Should retain SQLNCLI10 on source" {
$sourceLinked.Script() | Should -Match 'SQLNCLI10'
}

$server1.LinkedServers.Script() | Should -Match 'SQLNCLI10'
$server2.LinkedServers.Script() | Should -Match 'SQLNCLI11'
}
}
It "Should upgrade to SQLNCLI11 on destination" {
$destLinked.Script() | Should -Match 'SQLNCLI11'
}
}
}
35 changes: 16 additions & 19 deletions tests/Copy-DbaResourceGovernor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ param(
)

Describe "Copy-DbaResourceGovernor" -Tag "UnitTests" {
BeforeAll {
$command = Get-Command Copy-DbaResourceGovernor
$expected = $TestConfig.CommonParameters
$expected += @(
"Source",
"SourceSqlCredential",
"Destination",
"DestinationSqlCredential",
"ResourcePool",
"ExcludeResourcePool",
"Force",
"EnableException",
"Confirm",
"WhatIf"
)
}
Context "Parameter validation" {
BeforeAll {
$command = Get-Command Copy-DbaResourceGovernor
$expected = $TestConfig.CommonParameters
$expected += @(
"Source",
"SourceSqlCredential",
"Destination",
"DestinationSqlCredential",
"ResourcePool",
"ExcludeResourcePool",
"WorkloadGroup",
"ExcludeWorkloadGroup",
"Force",
"EnableException",
"Confirm",
"WhatIf"
)
}

It "Has parameter: <_>" -ForEach $expected {
$command | Should -HaveParameter $PSItem
}
Expand Down
43 changes: 19 additions & 24 deletions tests/Dismount-DbaDatabase.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@ param(
)

Describe "Dismount-DbaDatabase" -Tag "UnitTests" {
BeforeAll {
$command = Get-Command Dismount-DbaDatabase
$expected = $TestConfig.CommonParameters
$expected += @(
"SqlInstance",
"SqlCredential",
"Database",
"InputObject",
"UpdateStatistics",
"Force",
"EnableException",
"Confirm",
"WhatIf"
)
}
Context "Parameter validation" {
BeforeAll {
$command = Get-Command Dismount-DbaDatabase
$expected = $TestConfig.CommonParameters
$expected += @(
"SqlInstance",
"SqlCredential",
"Database",
"InputObject",
"UpdateStatistics",
"Force",
"EnableException",
"Confirm",
"WhatIf"
)
}

It "Has parameter: <_>" -ForEach $expected {
$command | Should -HaveParameter $PSItem
}
Expand Down Expand Up @@ -65,11 +64,6 @@ Describe "Dismount-DbaDatabase" -Tag "IntegrationTests" {
It "Should remove just one database" {
$results.Database | Should -Be $dbName
}

It "Should have the correct properties" {
$expectedProps = 'ComputerName', 'InstanceName', 'SqlInstance', 'Database', 'DatabaseID', 'DetachResult'
$results.PsObject.Properties.Name | Sort-Object | Should -Be ($expectedProps | Sort-Object)
}
}

Context "When detaching databases with snapshots" {
Expand Down Expand Up @@ -109,11 +103,12 @@ Describe "Dismount-DbaDatabase" -Tag "IntegrationTests" {
}

It "Should detach database without snapshots" {
# skip for now in appveyor, but when we do troubleshoot, maybe it just needs a sleep
Start-Sleep 3
$null = Stop-DbaProcess -SqlInstance $TestConfig.instance3 -Database $dbDetached
$null = Dismount-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached

$database = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached
$database | Should -BeNullOrEmpty
$result = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbDetached
$result | Should -BeNullOrEmpty
}
}
}
1 change: 1 addition & 0 deletions tests/pester.groups.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $TestsRunGroups = @{
'Remove-DbaAvailabilityGroup',
'Remove-DbaDatabaseSafely',
'Sync-DbaLoginPermission',
'Dismount-DbaDatabase',
# tests that fail locally against SQL Server 2022 instances and fail on AppVeyor
'Set-DbaAgentJobStep',
'New-DbaLogin',
Expand Down

0 comments on commit 413d0df

Please sign in to comment.