Describe"$commandname Integration Tests"-Tags"IntegrationTests"{Context"Count system databases on localhost"{$results=Get-DbaDatabase-SqlInstance$script:instance1-ExcludeAllUserDbIt"reports the right number of databases"{$results.Count|ShouldBe4}}Context"Check that temppb database is in Simple recovery mode"{$results=Get-DbaDatabase-SqlInstance$script:instance1-DatabasetempdbIt"tempdb's recovery mode is Simple"{$results.RecoveryModel|ShouldBe"Simple"}}Context"Check that master database is accessible"{$results=Get-DbaDatabase-SqlInstance$script:instance1-DatabasemasterIt"master is accessible"{$results.IsAccessible|ShouldBe$true}}}

Describe"$commandname Unit Tests"-Tags"UnitTests",Get-DBADatabase{BeforeAll{## Ensure it is the module that is being coded that is in the session when running just this Pester test# Remove-Module dbatools -Force -ErrorAction SilentlyContinue# $Base = Split-Path -parent $PSCommandPath# Import-Module $Base\..\dbatools.psd1}Context"Input validation"{BeforeAll{MockStop-Function{}-ModuleNamedbatoolsMockTest-FunctionInterrupt{}-ModuleNamedbatools}It"Should Call Stop-Function if NoUserDbs and NoSystemDbs are specified"{Get-DbaDatabase-SqlInstanceDummy-ExcludeAllSystemDb-ExcludeAllUserDb-ErrorActionSilentlyContinue|ShouldBe}It"Validates that Stop Function Mock has been called"{## Nope I have no idea why it's two either - RMS$assertMockParams=@{'CommandName'='Stop-Function''Times'=2'Exactly'=$true'Module'='dbatools'}Assert-MockCalled@assertMockParams}It"Validates that Test-FunctionInterrupt Mock has been called"{$assertMockParams=@{'CommandName'='Test-FunctionInterrupt''Times'=1'Exactly'=$true'Module'='dbatools'}Assert-MockCalled@assertMockParams}}Context"Output"{It"Should have Last Read and Last Write Property when IncludeLastUsed switch is added"{MockConnect-SQLInstance-MockWith{[object]@{Name='SQLServerName';Databases=[object]@(@{Name='db1';Status='Normal';ReadOnly='false';IsSystemObject='false';RecoveryModel='Full';Owner='sa'});#databases}#object}-ModuleNamedbatools#mock connect-sqlserverfunctionInvoke-QueryDBlastUsed{}MockInvoke-QueryDBlastUsed-MockWith{[object]@{dbname='db1';last_read=(Get-Date).AddHours(-1);last_write=(Get-Date).AddHours(-1)}}-ModuleNamedbatools(Get-DbaDatabase-SqlInstanceSQLServerName-IncludeLastUsed).LastRead-ne$null|ShouldBe$true(Get-DbaDatabase-SqlInstanceSQLServerName-IncludeLastUsed).LastWrite-ne$null|ShouldBe$true}It"Validates that Connect-SqlInstance Mock has been called"{$assertMockParams=@{'CommandName'='Connect-SqlInstance''Times'=2'Exactly'=$true'Module'='dbatools'}Assert-MockCalled@assertMockParams}It"Validates that Invoke-QueryDBlastUsed Mock has been called"{$assertMockParams=@{'CommandName'='Invoke-QueryDBlastUsed''Times'=2'Exactly'=$true'Module'='dbatools'}Assert-MockCalled@assertMockParams}}}