Skip to content

Commit

Permalink
sample (do Add-DbaAgDatabase)
Browse files Browse the repository at this point in the history
  • Loading branch information
potatoqualitee committed Oct 24, 2024
1 parent b7a87f5 commit 12cf17d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
7 changes: 4 additions & 3 deletions .aider/prompts/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ Be literal, even with `Describe "$($TestConfig.CommandName)"`
param($ModuleName = 'dbatools')
$global:TestConfig = Get-TestConfig
Describe "Get-CommandName" {
Describe $TestConfig.CommandName {
Context "Validate parameters" {
BeforeAll {
$command = Get-Command $global:TestConfig.CommandName
$command = Get-Command $TestConfig.CommandName
}
$parms = @(
"SqlInstance",
Expand Down Expand Up @@ -110,8 +110,9 @@ Describe "Get-CommandName" {
```powershell
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
param($ModuleName = 'dbatools')
$global:TestConfig = Get-TestConfig
Describe "Add-Numbers" {
Describe $TestConfig.CommandName {
It "Should calculate the correct result" -ForEach @(
@{ Input1 = 1; Input2 = 2; Expected = 3 }
@{ Input1 = 2; Input2 = 3; Expected = 5 }
Expand Down
4 changes: 4 additions & 0 deletions private/testing/Get-TestConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,9 @@ function Get-TestConfig {
# derive the command name from the CALLING script's filename
$config['CommandName'] = ($MyInvocation.MyCommand.Name | Split-Path -Leaf).Replace(".Tests.ps1", "")

if (-not $config['CommandName']) {
$config['CommandName'] = "Unknown"
}

[pscustomobject]$config
}
75 changes: 46 additions & 29 deletions tests/Add-DbaAgDatabase.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
$CommandName = $MyInvocation.MyCommand.Name.Replace(".Tests.ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
#Requires -Module @{ ModuleName="Pester"; ModuleVersion="5.0"}
param($ModuleName = 'dbatools')
$global:TestConfig = Get-TestConfig

Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Describe "Add-DbaAgDatabase" {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'AvailabilityGroup', 'Database', 'Secondary', 'SecondarySqlCredential', 'InputObject', 'SeedingMode', 'SharedPath', 'UseLastBackup', 'AdvancedBackupParams', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
BeforeAll {
$command = Get-Command Add-DbaAgDatabase
}
$parms = @(
"SqlInstance",
"SqlCredential",
"AvailabilityGroup",
"Database",
"Secondary",
"SecondarySqlCredential",
"InputObject",
"SeedingMode",
"SharedPath",
"UseLastBackup",
"AdvancedBackupParams",
"EnableException",
"WhatIf",
"Confirm"
)
It "Has required parameter: <_>" -ForEach $parms {
$command | Should -HaveParameter $PSItem
}
}
}

Describe "$commandname Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$null = Get-DbaProcess -SqlInstance $TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $TestConfig.instance3
$agname = "dbatoolsci_addagdb_agroup"
$dbname = "dbatoolsci_addagdb_agroupdb"
$newdbname = "dbatoolsci_addag_agroupdb_2"
$server.Query("create database $dbname")
$backup = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $dbname | Backup-DbaDatabase
$ag = New-DbaAvailabilityGroup -Primary $TestConfig.instance3 -Name $agname -ClusterType None -FailoverMode Manual -Database $dbname -Confirm:$false -Certificate dbatoolsci_AGCert
}
AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname -Confirm:$false
$null = Remove-DbaDatabase -SqlInstance $server -Database $dbname, $newdbname -Confirm:$false
}
Context "adds ag db" {
It "returns proper results" {
Context "Integration Tests" -Tag "IntegrationTests" {
BeforeAll {
$null = Get-DbaProcess -SqlInstance $global:TestConfig.instance3 -Program 'dbatools PowerShell module - dbatools.io' | Stop-DbaProcess -WarningAction SilentlyContinue
$server = Connect-DbaInstance -SqlInstance $global:TestConfig.instance3
$agname = "dbatoolsci_addagdb_agroup"
$dbname = "dbatoolsci_addagdb_agroupdb"
$newdbname = "dbatoolsci_addag_agroupdb_2"
$server.Query("create database $dbname")
$backup = Get-DbaDatabase -SqlInstance $global:TestConfig.instance3 -Database $dbname | Backup-DbaDatabase
$ag = New-DbaAvailabilityGroup -Primary $global:TestConfig.instance3 -Name $agname -ClusterType None -FailoverMode Manual -Database $dbname -Confirm:$false -Certificate dbatoolsci_AGCert
}

AfterAll {
$null = Remove-DbaAvailabilityGroup -SqlInstance $server -AvailabilityGroup $agname -Confirm:$false
$null = Remove-DbaDatabase -SqlInstance $server -Database $dbname, $newdbname -Confirm:$false
}

It "adds ag db and returns proper results" {
$server.Query("create database $newdbname")
$backup = Get-DbaDatabase -SqlInstance $TestConfig.instance3 -Database $newdbname | Backup-DbaDatabase
$results = Add-DbaAgDatabase -SqlInstance $TestConfig.instance3 -AvailabilityGroup $agname -Database $newdbname -Confirm:$false
$backup = Get-DbaDatabase -SqlInstance $global:TestConfig.instance3 -Database $newdbname | Backup-DbaDatabase
$results = Add-DbaAgDatabase -SqlInstance $global:TestConfig.instance3 -AvailabilityGroup $agname -Database $newdbname -Confirm:$false
$results.AvailabilityGroup | Should -Be $agname
$results.Name | Should -Be $newdbname
$results.IsJoined | Should -Be $true
}
}
} #$TestConfig.instance2 for appveyor
}
#$global:TestConfig.instance2 for appveyor

0 comments on commit 12cf17d

Please sign in to comment.