From 12cf17df40ea2b35df8366bb2b2a1173c47468f8 Mon Sep 17 00:00:00 2001 From: Chrissy LeMaire Date: Thu, 24 Oct 2024 22:40:51 +0200 Subject: [PATCH] sample (do Add-DbaAgDatabase) --- .aider/prompts/conventions.md | 7 +-- private/testing/Get-TestConfig.ps1 | 4 ++ tests/Add-DbaAgDatabase.Tests.ps1 | 75 ++++++++++++++++++------------ 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/.aider/prompts/conventions.md b/.aider/prompts/conventions.md index eb8bb021f3..9c9c0112ee 100644 --- a/.aider/prompts/conventions.md +++ b/.aider/prompts/conventions.md @@ -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", @@ -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 } diff --git a/private/testing/Get-TestConfig.ps1 b/private/testing/Get-TestConfig.ps1 index edc3823040..39f7441431 100644 --- a/private/testing/Get-TestConfig.ps1 +++ b/private/testing/Get-TestConfig.ps1 @@ -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 } \ No newline at end of file diff --git a/tests/Add-DbaAgDatabase.Tests.ps1 b/tests/Add-DbaAgDatabase.Tests.ps1 index 59717a15f6..466604ecdf 100644 --- a/tests/Add-DbaAgDatabase.Tests.ps1 +++ b/tests/Add-DbaAgDatabase.Tests.ps1 @@ -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