<commit_before>﻿$commandname = $MyInvocation.MyCommand.Name.Replace(".ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

try {
	$connstring = "Server=ADMIN:$script:instance1;Trusted_Connection=True"
	$server = New-Object Microsoft.SqlServer.Management.Smo.Server $script:instance1
	$server.ConnectionContext.ConnectionString = $connstring
	$server.ConnectionContext.Connect()
	$server.ConnectionContext.Disconnect()
	Clear-DbaSqlConnectionPool
}
catch {
	Write-Host "DAC not working this round, likely due to Appveyor resources"
	return
}

# One more for the road - clearing the connection pool is important for DAC since only one is allowed
Clear-DbaSqlConnectionPool

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
	Context "Create new linked server" {
		$LinkedServers = $script:Instances | Get-DbaLinkedServer -LinkedServer localhost, localhost2
		foreach ($LinkedServer in $LinkedServers) {
			$LinkedServer.Drop()
		}
		
		Clear-DbaSqlConnectionPool
		$server = Connect-DbaSqlServer -SqlInstance $script:instance1
		$server.Query("EXEC master.dbo.sp_addlinkedserver @server = N'localhost', @srvproduct=N'SQL Server'
		EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'localhost',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='<PASSWORD>'
		EXEC master.dbo.sp_addlinkedserver @server = N'localhost2', @srvproduct=N'SQL Server'
		EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'localhost2',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='<PASSWORD>'
		")
		
		Context "Copy Credential with the same properties." {
			It "Should copy successfully" {
				$results = Copy-DbaLinkedServer -Source $script:instance1 -Destination $script:instance2 -LinkedServer localhost
				$results.Status | Should Be "Successful"
			}
			
			It "Should retain its same properties" {
				
				$LinkedServer1 = Get-DbaLinkedServer -SqlInstance $script:instance1 -LinkedServer localhost
				$LinkedServer2 = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer localhost
				
				# Compare its value
				$LinkedServer1.Name | Should Be $LinkedServer2.Name
				$LinkedServer1.LinkedServer | Should Be $LinkedServer2.LinkedServer
			}
		}
		Clear-DbaSqlConnectionPool
		Context "No overwrite and cleanup" {
			$results = Copy-DbaLinkedServer -Source $script:instance1 -Destination $script:instance2 -LinkedServer localhostcred -WarningVariable warning 3>&1
			It "Should not attempt overwrite" {
				$warning | Should Match "exists"
				
			}
			# Finish up
			$LinkedServers = $script:Instances | Get-DbaLinkedServer -LinkedServer localhost, localhost2
			foreach ($LinkedServer in $LinkedServers) {
				$LinkedServer.Drop()
			}
		}
		Clear-DbaSqlConnectionPool
	}
}<commit_msg>updated test<commit_after>﻿$commandname = $MyInvocation.MyCommand.Name.Replace(".ps1", "")
Write-Host -Object "Running $PSCommandpath" -ForegroundColor Cyan
. "$PSScriptRoot\constants.ps1"

try {
	$connstring = "Server=ADMIN:$script:instance1;Trusted_Connection=True"
	$server = New-Object Microsoft.SqlServer.Management.Smo.Server $script:instance1
	$server.ConnectionContext.ConnectionString = $connstring
	$server.ConnectionContext.Connect()
	$server.ConnectionContext.Disconnect()
	Clear-DbaSqlConnectionPool
}
catch {
	Write-Host "DAC not working this round, likely due to Appveyor resources"
	#return
}

$dropsql = "EXEC master.dbo.sp_dropserver @server=N'dbatools-localhost', @droplogins='droplogins';
			EXEC master.dbo.sp_dropserver @server=N'dbatools-localhost2', @droplogins='droplogins'"

$createsql = "EXEC master.dbo.sp_addlinkedserver @server = N'dbatools-localhost', @srvproduct=N'SQL Server';
		EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatools-localhost',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='<PASSWORD>';
		EXEC master.dbo.sp_addlinkedserver @server = N'dbatools-localhost2', @srvproduct=N'SQL Server';
		EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'dbatools-localhost2',@useself=N'False',@locallogin=NULL,@rmtuser=N'testuser1',@rmtpassword='<PASSWORD>';"
# One more for the road - clearing the connection pool is important for DAC since only one is allowed
Clear-DbaSqlConnectionPool

Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
	Context "Create new linked server" {
		$server1 = Connect-DbaSqlServer -SqlInstance $script:instance1
		$server2 = Connect-DbaSqlServer -SqlInstance $script:instance2
		try {
			$server1.Query($dropsql)
			$server2.Query($dropsql)
		}
		catch {
			# dont care
		}
		
		$server1.Query($createsql)
	}
	
	Context "Copy Credential with the same properties." {
		It "Should copy successfully" {
			$results = Copy-DbaLinkedServer -Source $script:instance1 -Destination $script:instance2 -LinkedServer dbatools-localhost
			$results.Status | Should Be "Successful"
		}
		
		It "Should retain its same properties" {
			
			$LinkedServer1 = Get-DbaLinkedServer -SqlInstance $script:instance1 -LinkedServer dbatools-localhost
			$LinkedServer2 = Get-DbaLinkedServer -SqlInstance $script:instance2 -LinkedServer dbatools-localhost
			
			# Compare its value
			$LinkedServer1.Name | Should Be $LinkedServer2.Name
			$LinkedServer1.LinkedServer | Should Be $LinkedServer2.LinkedServer
		}
	}
	Clear-DbaSqlConnectionPool
	Context "No overwrite and cleanup" {
		$results = Copy-DbaLinkedServer -Source $script:instance1 -Destination $script:instance2 -LinkedServer dbatools-localhost -WarningVariable warning 3>&1
		It "Should not attempt overwrite" {
			$warning | Should Match "exists"
			
		}
		
		try {
			$server1.Query($dropsql)
			$server2.Query($dropsql)
		}
		catch {
			# dont care
		}
	}
	Clear-DbaSqlConnectionPool
}
{'fraction_non_alphanumeric': 0.10919540229885058, 'fraction_numerical': 0.009926854754440962, 'mean_word_length': 5.381111111111111, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '913962', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1802, 'n_words': 383}