Move Integration Tests to dedicated project (#201)

Add IntegrationTests project. Move all tests ifdef'd with LIVE_CONNECTION_TESTS to IntegrationTests project. Delete files that have no remaining code. Update codecoverage.bat to run integration tests
This commit is contained in:
Connor Quagliana
2017-01-04 11:37:57 -08:00
committed by GitHub
parent 5b41538d22
commit 3ad7cc1a8b
16 changed files with 647 additions and 435 deletions

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="xunit.methodDisplay" value="method"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>

View File

@@ -0,0 +1,8 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]

View File

@@ -0,0 +1,952 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Threading;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
using static Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.ReliableConnectionHelper;
using static Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.RetryPolicy;
using static Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.RetryPolicy.TimeBasedRetryPolicy;
using static Microsoft.SqlTools.ServiceLayer.Connection.ReliableConnection.SqlSchemaModelErrorCodes;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.Connection
{
/// <summary>
/// Tests for the ReliableConnection module.
/// These tests all assume a live connection to a database on localhost using integrated auth.
/// </summary>
public class ReliableConnectionTests
{
internal class TestDataTransferErrorDetectionStrategy : DataTransferErrorDetectionStrategy
{
public bool InvokeCanRetrySqlException(SqlException exception)
{
return CanRetrySqlException(exception);
}
}
internal class TestSqlAzureTemporaryAndIgnorableErrorDetectionStrategy : SqlAzureTemporaryAndIgnorableErrorDetectionStrategy
{
public TestSqlAzureTemporaryAndIgnorableErrorDetectionStrategy()
: base (new int[] { 100 })
{
}
public bool InvokeCanRetrySqlException(SqlException exception)
{
return CanRetrySqlException(exception);
}
public bool InvokeShouldIgnoreSqlException(SqlException exception)
{
return ShouldIgnoreSqlException(exception);
}
}
internal class TestFixedDelayPolicy : FixedDelayPolicy
{
public TestFixedDelayPolicy(
IErrorDetectionStrategy strategy,
int maxRetryCount,
TimeSpan intervalBetweenRetries)
: base(strategy,
maxRetryCount,
intervalBetweenRetries)
{
}
public bool InvokeShouldRetryImpl(RetryState retryStateObj)
{
return ShouldRetryImpl(retryStateObj);
}
public void DoOnIgnoreErrorOccurred(RetryState retryState)
{
OnIgnoreErrorOccurred(retryState);
}
}
internal class TestProgressiveRetryPolicy : ProgressiveRetryPolicy
{
public TestProgressiveRetryPolicy(
IErrorDetectionStrategy strategy,
int maxRetryCount,
TimeSpan initialInterval,
TimeSpan increment)
: base(strategy,
maxRetryCount,
initialInterval,
increment)
{
}
public bool InvokeShouldRetryImpl(RetryState retryStateObj)
{
return ShouldRetryImpl(retryStateObj);
}
}
internal class TestTimeBasedRetryPolicy : TimeBasedRetryPolicy
{
public TestTimeBasedRetryPolicy(
IErrorDetectionStrategy strategy,
TimeSpan minTotalRetryTimeLimit,
TimeSpan maxTotalRetryTimeLimit,
double totalRetryTimeLimitRate,
TimeSpan minInterval,
TimeSpan maxInterval,
double intervalFactor)
: base(
strategy,
minTotalRetryTimeLimit,
maxTotalRetryTimeLimit,
totalRetryTimeLimitRate,
minInterval,
maxInterval,
intervalFactor)
{
}
public bool InvokeShouldRetryImpl(RetryState retryStateObj)
{
return ShouldRetryImpl(retryStateObj);
}
}
[Fact]
public void FixedDelayPolicyTest()
{
TestFixedDelayPolicy policy = new TestFixedDelayPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
maxRetryCount: 3,
intervalBetweenRetries: TimeSpan.FromMilliseconds(100));
var retryState = new RetryStateEx();
bool shouldRety = policy.InvokeShouldRetryImpl(retryState);
policy.DoOnIgnoreErrorOccurred(retryState);
Assert.True(shouldRety);
}
[Fact]
public void FixedDelayPolicyExecuteActionTest()
{
TestFixedDelayPolicy policy = new TestFixedDelayPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
maxRetryCount: 3,
intervalBetweenRetries: TimeSpan.FromMilliseconds(20));
// execute an action that throws a retry limit exception
CancellationToken token = new CancellationToken();
Assert.Equal(policy.ExecuteAction<int>((s) => { throw new RetryLimitExceededException(); }, token), default(int));
// execute an action that throws a retry limit exeception with an inner exception
Assert.Throws<Exception>(() =>
{
policy.ExecuteAction<int>((s) =>
{
var e = new RetryLimitExceededException("retry", new Exception());
throw e;
});
});
}
[Fact]
public void IsRetryableExceptionTest()
{
TestFixedDelayPolicy policy = new TestFixedDelayPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
maxRetryCount: 3,
intervalBetweenRetries: TimeSpan.FromMilliseconds(20));
Assert.False(policy.IsRetryableException(new Exception()));
}
[Fact]
public void ProgressiveRetryPolicyTest()
{
TestProgressiveRetryPolicy policy = new TestProgressiveRetryPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
maxRetryCount: 3,
initialInterval: TimeSpan.FromMilliseconds(100),
increment: TimeSpan.FromMilliseconds(100));
bool shouldRety = policy.InvokeShouldRetryImpl(new RetryStateEx());
Assert.True(shouldRety);
Assert.NotNull(policy.CommandTimeoutInSeconds);
policy.ShouldIgnoreOnFirstTry = false;
Assert.False(policy.ShouldIgnoreOnFirstTry);
}
[Fact]
public void TimeBasedRetryPolicyTest()
{
TestTimeBasedRetryPolicy policy = new TestTimeBasedRetryPolicy(
strategy: new NetworkConnectivityErrorDetectionStrategy(),
minTotalRetryTimeLimit: TimeSpan.FromMilliseconds(100),
maxTotalRetryTimeLimit: TimeSpan.FromMilliseconds(100),
totalRetryTimeLimitRate: 100,
minInterval: TimeSpan.FromMilliseconds(100),
maxInterval: TimeSpan.FromMilliseconds(100),
intervalFactor: 1);
bool shouldRety = policy.InvokeShouldRetryImpl(new RetryStateEx());
Assert.True(shouldRety);
}
[Fact]
public void GetErrorNumberWithNullExceptionTest()
{
Assert.Null(RetryPolicy.GetErrorNumber(null));
}
/// <summary>
/// Environment variable that stores the name of the test server hosting the SQL Server instance.
/// </summary>
public static string TestServerEnvironmentVariable
{
get { return "TEST_SERVER"; }
}
private static Lazy<string> testServerName = new Lazy<string>(() => Environment.GetEnvironmentVariable(TestServerEnvironmentVariable));
/// <summary>
/// Name of the test server hosting the SQL Server instance.
/// </summary>
public static string TestServerName
{
get { return testServerName.Value; }
}
/// <summary>
/// Helper method to create an integrated auth connection builder for testing.
/// </summary>
private SqlConnectionStringBuilder CreateTestConnectionStringBuilder()
{
SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder();
csb.DataSource = TestServerName;
csb.IntegratedSecurity = true;
return csb;
}
/// <summary>
/// Helper method to create an integrated auth reliable connection for testing.
/// </summary>
private DbConnection CreateTestConnection()
{
SqlConnectionStringBuilder csb = CreateTestConnectionStringBuilder();
RetryPolicy connectionRetryPolicy = RetryPolicyFactory.CreateDefaultConnectionRetryPolicy();
RetryPolicy commandRetryPolicy = RetryPolicyFactory.CreateDefaultConnectionRetryPolicy();
ReliableSqlConnection connection = new ReliableSqlConnection(csb.ConnectionString, connectionRetryPolicy, commandRetryPolicy);
return connection;
}
/// <summary>
/// Test ReliableConnectionHelper.GetDefaultDatabaseFilePath()
/// </summary>
[Fact]
public void TestGetDefaultDatabaseFilePath()
{
TestUtils.RunIfWindows(() =>
{
var connectionBuilder = CreateTestConnectionStringBuilder();
Assert.NotNull(connectionBuilder);
string filePath = string.Empty;
string logPath = string.Empty;
ReliableConnectionHelper.OpenConnection(
connectionBuilder,
usingConnection: (conn) =>
{
filePath = ReliableConnectionHelper.GetDefaultDatabaseFilePath(conn);
logPath = ReliableConnectionHelper.GetDefaultDatabaseLogPath(conn);
},
catchException: null,
useRetry: false);
Assert.False(string.IsNullOrWhiteSpace(filePath));
Assert.False(string.IsNullOrWhiteSpace(logPath));
});
}
/// <summary>
/// Test ReliableConnectionHelper.GetServerVersion()
/// </summary>
[Fact]
public void TestGetServerVersion()
{
TestUtils.RunIfWindows(() =>
{
using (var connection = CreateTestConnection())
{
Assert.NotNull(connection);
connection.Open();
ReliableConnectionHelper.ServerInfo serverInfo = ReliableConnectionHelper.GetServerVersion(connection);
ReliableConnectionHelper.ServerInfo serverInfo2;
using (var connection2 = CreateTestConnection())
{
connection2.Open();
serverInfo2 = ReliableConnectionHelper.GetServerVersion(connection);
}
Assert.NotNull(serverInfo);
Assert.NotNull(serverInfo2);
Assert.True(serverInfo.ServerMajorVersion != 0);
Assert.True(serverInfo.ServerMajorVersion == serverInfo2.ServerMajorVersion);
Assert.True(serverInfo.ServerMinorVersion == serverInfo2.ServerMinorVersion);
Assert.True(serverInfo.ServerReleaseVersion == serverInfo2.ServerReleaseVersion);
Assert.True(serverInfo.ServerEdition == serverInfo2.ServerEdition);
Assert.True(serverInfo.IsCloud == serverInfo2.IsCloud);
Assert.True(serverInfo.AzureVersion == serverInfo2.AzureVersion);
Assert.True(serverInfo.IsAzureV1 == serverInfo2.IsAzureV1);
}
});
}
/// <summary>
/// Tests ReliableConnectionHelper.GetCompleteServerName()
/// </summary>
[Fact]
public void TestGetCompleteServerName()
{
string name = ReliableConnectionHelper.GetCompleteServerName(@".\SQL2008");
Assert.True(name.Contains(Environment.MachineName));
name = ReliableConnectionHelper.GetCompleteServerName(@"(local)");
Assert.True(name.Contains(Environment.MachineName));
}
/// <summary>
/// Tests ReliableConnectionHelper.IsDatabaseReadonly()
/// </summary>
[Fact]
public void TestIsDatabaseReadonly()
{
var connectionBuilder = CreateTestConnectionStringBuilder();
Assert.NotNull(connectionBuilder);
bool isReadOnly = ReliableConnectionHelper.IsDatabaseReadonly(connectionBuilder);
Assert.False(isReadOnly);
}
/// <summary>
/// /// Tests ReliableConnectionHelper.IsDatabaseReadonly() with null builder parameter
/// </summary>
[Fact]
public void TestIsDatabaseReadonlyWithNullBuilder()
{
Assert.Throws<ArgumentNullException>(() => ReliableConnectionHelper.IsDatabaseReadonly(null));
}
/// <summary>
/// Verify ANSI_NULL and QUOTED_IDENTIFIER settings can be set and retrieved for a session
/// </summary>
[Fact]
public void VerifyAnsiNullAndQuotedIdentifierSettingsReplayed()
{
TestUtils.RunIfWindows(() =>
{
using (ReliableSqlConnection conn = (ReliableSqlConnection)ReliableConnectionHelper.OpenConnection(CreateTestConnectionStringBuilder(), useRetry: true))
{
VerifySessionSettings(conn, true);
VerifySessionSettings(conn, false);
}
});
}
private void VerifySessionSettings(ReliableSqlConnection conn, bool expectedSessionValue)
{
Tuple<string, bool>[] settings = null;
using (IDbCommand cmd = conn.CreateCommand())
{
if (expectedSessionValue)
{
cmd.CommandText = "SET ANSI_NULLS, QUOTED_IDENTIFIER ON";
}
else
{
cmd.CommandText = "SET ANSI_NULLS, QUOTED_IDENTIFIER OFF";
}
cmd.ExecuteNonQuery();
//baseline assertion
AssertSessionValues(cmd, ansiNullsValue: expectedSessionValue, quotedIdentifersValue: expectedSessionValue);
// verify the initial values are correct
settings = conn.CacheOrReplaySessionSettings(cmd, settings);
// assert no change is session settings
AssertSessionValues(cmd, ansiNullsValue: expectedSessionValue, quotedIdentifersValue: expectedSessionValue);
// assert cached settings are correct
Assert.Equal("ANSI_NULLS", settings[0].Item1);
Assert.Equal(expectedSessionValue, settings[0].Item2);
Assert.Equal("QUOTED_IDENTIFIER", settings[1].Item1);
Assert.Equal(expectedSessionValue, settings[1].Item2);
// invert session values and assert we reset them
if (expectedSessionValue)
{
cmd.CommandText = "SET ANSI_NULLS, QUOTED_IDENTIFIER OFF";
}
else
{
cmd.CommandText = "SET ANSI_NULLS, QUOTED_IDENTIFIER ON";
}
cmd.ExecuteNonQuery();
// baseline assertion
AssertSessionValues(cmd, ansiNullsValue: !expectedSessionValue, quotedIdentifersValue: !expectedSessionValue);
// replay cached value
settings = conn.CacheOrReplaySessionSettings(cmd, settings);
// assert session settings correctly set
AssertSessionValues(cmd, ansiNullsValue: expectedSessionValue, quotedIdentifersValue: expectedSessionValue);
}
}
private void AssertSessionValues(IDbCommand cmd, bool ansiNullsValue, bool quotedIdentifersValue)
{
// assert session was updated
cmd.CommandText = "SELECT SESSIONPROPERTY ('ANSI_NULLS'), SESSIONPROPERTY ('QUOTED_IDENTIFIER')";
using (IDataReader reader = cmd.ExecuteReader())
{
Assert.True(reader.Read(), "Missing session settings");
bool actualAnsiNullsOnValue = ((int)reader[0] == 1);
bool actualQuotedIdentifierOnValue = ((int)reader[1] == 1);
Assert.Equal(ansiNullsValue, actualAnsiNullsOnValue);
Assert.Equal(quotedIdentifersValue, actualQuotedIdentifierOnValue);
}
}
/// <summary>
/// Test that the retry policy factory constructs all possible types of policies successfully.
/// </summary>
[Fact]
public void RetryPolicyFactoryConstructsPoliciesSuccessfully()
{
TestUtils.RunIfWindows(() =>
{
Assert.NotNull(RetryPolicyFactory.CreateColumnEncryptionTransferRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDatabaseCommandRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDataScriptUpdateRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDefaultConnectionRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDefaultDataConnectionRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDefaultDataSqlCommandRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDefaultDataTransferRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateDefaultSchemaCommandRetryPolicy(true));
Assert.NotNull(RetryPolicyFactory.CreateDefaultSchemaConnectionRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateElementCommandRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateFastDataRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateNoRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreatePrimaryKeyCommandRetryPolicy());
Assert.NotNull(RetryPolicyFactory.CreateSchemaCommandRetryPolicy(6));
Assert.NotNull(RetryPolicyFactory.CreateSchemaConnectionRetryPolicy(6));
});
}
/// <summary>
/// ReliableConnectionHelper.IsCloud() should be false for a local server
/// </summary>
[Fact]
public void TestIsCloudIsFalseForLocalServer()
{
TestUtils.RunIfWindows(() =>
{
using (var connection = CreateTestConnection())
{
Assert.NotNull(connection);
connection.Open();
Assert.False(ReliableConnectionHelper.IsCloud(connection));
}
});
}
/// <summary>
/// Tests that ReliableConnectionHelper.OpenConnection() opens a connection if it is closed
/// </summary>
[Fact]
public void TestOpenConnectionOpensConnection()
{
TestUtils.RunIfWindows(() =>
{
using (var connection = CreateTestConnection())
{
Assert.NotNull(connection);
Assert.True(connection.State == ConnectionState.Closed);
ReliableConnectionHelper.OpenConnection(connection);
Assert.True(connection.State == ConnectionState.Open);
}
});
}
/// <summary>
/// Tests that ReliableConnectionHelper.ExecuteNonQuery() runs successfully
/// </summary>
[Fact]
public void TestExecuteNonQuery()
{
TestUtils.RunIfWindows(() =>
{
var result = ReliableConnectionHelper.ExecuteNonQuery(
CreateTestConnectionStringBuilder(),
"SET NOCOUNT ON; SET NOCOUNT OFF;",
ReliableConnectionHelper.SetCommandTimeout,
null,
true
);
Assert.NotNull(result);
});
}
/// <summary>
/// Test that TryGetServerVersion() gets server information
/// </summary>
[Fact]
public void TestTryGetServerVersion()
{
TestUtils.RunIfWindows(() =>
{
ReliableConnectionHelper.ServerInfo info = null;
Assert.True(ReliableConnectionHelper.TryGetServerVersion(CreateTestConnectionStringBuilder().ConnectionString, out info));
Assert.NotNull(info);
Assert.NotNull(info.ServerVersion);
Assert.NotEmpty(info.ServerVersion);
});
}
/// <summary>
/// Test that TryGetServerVersion() fails with invalid connection string
/// </summary>
[Fact]
public void TestTryGetServerVersionInvalidConnectionString()
{
TestUtils.RunIfWindows(() =>
{
ReliableConnectionHelper.ServerInfo info = null;
Assert.False(ReliableConnectionHelper.TryGetServerVersion("this is not a valid connstr", out info));
});
}
/// <summary>
/// Validate ambient static settings
/// </summary>
[Fact]
public void AmbientSettingsStaticPropertiesTest()
{
var defaultSettings = AmbientSettings.DefaultSettings;
Assert.NotNull(defaultSettings);
var masterReferenceFilePath = AmbientSettings.MasterReferenceFilePath;
var maxDataReaderDegreeOfParallelism = AmbientSettings.MaxDataReaderDegreeOfParallelism;
var tableProgressUpdateInterval = AmbientSettings.TableProgressUpdateInterval;
var traceRowCountFailure = AmbientSettings.TraceRowCountFailure;
var useOfflineDataReader = AmbientSettings.UseOfflineDataReader;
var streamBackingStoreForOfflineDataReading = AmbientSettings.StreamBackingStoreForOfflineDataReading;
var disableIndexesForDataPhase = AmbientSettings.DisableIndexesForDataPhase;
var reliableDdlEnabled = AmbientSettings.ReliableDdlEnabled;
var importModelDatabase = AmbientSettings.ImportModelDatabase;
var supportAlwaysEncrypted = AmbientSettings.SupportAlwaysEncrypted;
var alwaysEncryptedWizardMigration = AmbientSettings.AlwaysEncryptedWizardMigration;
var skipObjectTypeBlocking =AmbientSettings.SkipObjectTypeBlocking;
var doNotSerializeQueryStoreSettings = AmbientSettings.DoNotSerializeQueryStoreSettings;
var lockTimeoutMilliSeconds = AmbientSettings.LockTimeoutMilliSeconds;
var queryTimeoutSeconds = AmbientSettings.QueryTimeoutSeconds;
var longRunningQueryTimeoutSeconds = AmbientSettings.LongRunningQueryTimeoutSeconds;
var alwaysRetryOnTransientFailure = AmbientSettings.AlwaysRetryOnTransientFailure;
var connectionRetryMessageHandler = AmbientSettings.ConnectionRetryMessageHandler;
using (var settingsContext = AmbientSettings.CreateSettingsContext())
{
var settings = settingsContext.Settings;
Assert.NotNull(settings);
}
}
/// <summary>
/// Validate ambient settings populate
/// </summary>
[Fact]
public void AmbientSettingsPopulateTest()
{
var data = new AmbientSettings.AmbientData();
var masterReferenceFilePath = data.MasterReferenceFilePath;
data.MasterReferenceFilePath = masterReferenceFilePath;
var lockTimeoutMilliSeconds = data.LockTimeoutMilliSeconds;
data.LockTimeoutMilliSeconds = lockTimeoutMilliSeconds;
var queryTimeoutSeconds = data.QueryTimeoutSeconds;
data.QueryTimeoutSeconds = queryTimeoutSeconds;
var longRunningQueryTimeoutSeconds = data.LongRunningQueryTimeoutSeconds;
data.LongRunningQueryTimeoutSeconds = longRunningQueryTimeoutSeconds;
var alwaysRetryOnTransientFailure = data.AlwaysRetryOnTransientFailure;
data.AlwaysRetryOnTransientFailure = alwaysRetryOnTransientFailure;
var connectionRetryMessageHandler = data.ConnectionRetryMessageHandler;
data.ConnectionRetryMessageHandler = connectionRetryMessageHandler;
var traceRowCountFailure = data.TraceRowCountFailure;
data.TraceRowCountFailure = traceRowCountFailure;
var tableProgressUpdateInterval = data.TableProgressUpdateInterval;
data.TableProgressUpdateInterval = tableProgressUpdateInterval;
var useOfflineDataReader = data.UseOfflineDataReader;
data.UseOfflineDataReader = useOfflineDataReader;
var streamBackingStoreForOfflineDataReading = data.StreamBackingStoreForOfflineDataReading;
data.StreamBackingStoreForOfflineDataReading = streamBackingStoreForOfflineDataReading;
var disableIndexesForDataPhase = data.DisableIndexesForDataPhase;
data.DisableIndexesForDataPhase = disableIndexesForDataPhase;
var reliableDdlEnabled = data.ReliableDdlEnabled;
data.ReliableDdlEnabled = reliableDdlEnabled;
var importModelDatabase = data.ImportModelDatabase;
data.ImportModelDatabase = importModelDatabase;
var supportAlwaysEncrypted = data.SupportAlwaysEncrypted;
data.SupportAlwaysEncrypted = supportAlwaysEncrypted;
var alwaysEncryptedWizardMigration = data.AlwaysEncryptedWizardMigration;
data.AlwaysEncryptedWizardMigration = alwaysEncryptedWizardMigration;
var skipObjectTypeBlocking = data.SkipObjectTypeBlocking;
data.SkipObjectTypeBlocking = skipObjectTypeBlocking;
var doNotSerializeQueryStoreSettings = data.DoNotSerializeQueryStoreSettings;
data.DoNotSerializeQueryStoreSettings = doNotSerializeQueryStoreSettings;
Dictionary<string, object> settings = new Dictionary<string, object>();
settings.Add("LockTimeoutMilliSeconds", 10000);
data.PopulateSettings(settings);
settings["LockTimeoutMilliSeconds"] = 15000;
data.PopulateSettings(settings);
data.TraceSettings();
}
[Fact]
public void RaiseAmbientRetryMessageTest()
{
bool handlerCalled = false;
var data = new AmbientSettings.AmbientData();
data.ConnectionRetryMessageHandler = (a) => handlerCalled = true;
AmbientSettings._defaultSettings = data;
RetryPolicyUtils.RaiseAmbientRetryMessage(new RetryStateEx() { LastError = new Exception() }, 100);
Assert.True(handlerCalled);
}
[Fact]
public void RaiseAmbientIgnoreMessageTest()
{
bool handlerCalled = false;
var data = new AmbientSettings.AmbientData();
data.ConnectionRetryMessageHandler = (a) => handlerCalled = true;
AmbientSettings._defaultSettings = data;
RetryPolicyUtils.RaiseAmbientIgnoreMessage(new RetryStateEx() { LastError = new Exception() }, 100);
Assert.True(handlerCalled);
}
[Fact]
public void RetryPolicyFactoryTest()
{
Assert.NotNull(RetryPolicyFactory.NoRetryPolicy);
Assert.NotNull(RetryPolicyFactory.PrimaryKeyViolationRetryPolicy);
RetryPolicy noRetyPolicy = RetryPolicyFactory.CreateDefaultSchemaCommandRetryPolicy(useRetry: false);
var retryState = new RetryStateEx();
retryState.LastError = new Exception();
RetryPolicyFactory.DataConnectionFailureRetry(retryState);
RetryPolicyFactory.CommandFailureRetry(retryState, "command");
RetryPolicyFactory.CommandFailureIgnore(retryState, "command");
RetryPolicyFactory.ElementCommandFailureIgnore(retryState);
RetryPolicyFactory.ElementCommandFailureRetry(retryState);
RetryPolicyFactory.CreateDatabaseCommandFailureIgnore(retryState);
RetryPolicyFactory.CreateDatabaseCommandFailureRetry(retryState);
RetryPolicyFactory.CommandFailureIgnore(retryState);
RetryPolicyFactory.CommandFailureRetry(retryState);
var transientPolicy = new RetryPolicyFactory.TransientErrorIgnoreStrategy();
Assert.False(transientPolicy.CanRetry(new Exception()));
Assert.False(transientPolicy.ShouldIgnoreError(new Exception()));
}
[Fact]
public void ReliableConnectionHelperTest()
{
ScriptFile scriptFile;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(connInfo.SqlConnection));
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(builder));
ReliableConnectionHelper.TryAddAlwaysOnConnectionProperties(builder, new SqlConnectionStringBuilder());
Assert.NotNull(ReliableConnectionHelper.GetServerName(connInfo.SqlConnection));
Assert.NotNull(ReliableConnectionHelper.ReadServerVersion(connInfo.SqlConnection));
Assert.NotNull(ReliableConnectionHelper.GetAsSqlConnection(connInfo.SqlConnection));
ServerInfo info = ReliableConnectionHelper.GetServerVersion(connInfo.SqlConnection);
Assert.NotNull(ReliableConnectionHelper.IsVersionGreaterThan2012RTM(info));
}
[Fact]
public void DataSchemaErrorTests()
{
var error = new DataSchemaError();
Assert.NotNull(error);
var isOnDisplay = error.IsOnDisplay;
var isBuildErrorCodeDefined = error.IsBuildErrorCodeDefined;
var buildErrorCode = error.BuildErrorCode;
var isPriorityEditable = error.IsPriorityEditable;
var message = error.Message;
var exception = error.Exception;
var prefix = error.Prefix;
var column = error.Column;
var line =error.Line;
var errorCode =error.ErrorCode;
var severity = error.Severity;
var document = error.Document;
Assert.NotNull(error.ToString());
Assert.NotNull(DataSchemaError.FormatErrorCode("ex", 1));
}
[Fact]
public void InitReliableSqlConnectionTest()
{
ScriptFile scriptFile;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
var connection = connInfo.SqlConnection as ReliableSqlConnection;
var command = new ReliableSqlConnection.ReliableSqlCommand(connection);
Assert.NotNull(command.Connection);
var retryPolicy = connection.CommandRetryPolicy;
connection.CommandRetryPolicy = retryPolicy;
Assert.True(connection.CommandRetryPolicy == retryPolicy);
connection.ChangeDatabase("master");
Assert.True(connection.ConnectionTimeout > 0);
connection.ClearPool();
}
[Fact]
public void ThrottlingReasonTests()
{
var reason = RetryPolicy.ThrottlingReason.Unknown;
Assert.NotNull(reason.ThrottlingMode);
Assert.NotNull(reason.ThrottledResources);
try
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.InitialCatalog = "master";
builder.IntegratedSecurity = false;
builder.DataSource = "localhost";
builder.UserID = "invalid";
builder.Password = "..";
SqlConnection conn = new SqlConnection(builder.ToString());
conn.Open();
}
catch (SqlException sqlException)
{
var exceptionReason = RetryPolicy.ThrottlingReason.FromException(sqlException);
Assert.NotNull(exceptionReason);
var errorReason = RetryPolicy.ThrottlingReason.FromError(sqlException.Errors[0]);
Assert.NotNull(errorReason);
var detectionStrategy = new TestDataTransferErrorDetectionStrategy();
Assert.True(detectionStrategy.InvokeCanRetrySqlException(sqlException));
Assert.True(detectionStrategy.CanRetry(new InvalidOperationException()));
Assert.False(detectionStrategy.ShouldIgnoreError(new InvalidOperationException()));
var detectionStrategy2 = new TestSqlAzureTemporaryAndIgnorableErrorDetectionStrategy();
Assert.NotNull(detectionStrategy2.InvokeCanRetrySqlException(sqlException));
Assert.NotNull(detectionStrategy2.InvokeShouldIgnoreSqlException(sqlException));
Batch batch = new Batch(Common.StandardQuery, Common.SubsectionDocument, Common.Ordinal, Common.GetFileStreamFactory(null));
batch.UnwrapDbException(sqlException);
}
var unknownCodeReason = RetryPolicy.ThrottlingReason.FromReasonCode(-1);
var codeReason = RetryPolicy.ThrottlingReason.FromReasonCode(2601);
Assert.NotNull(codeReason);
Assert.NotNull(codeReason.IsThrottledOnDataSpace);
Assert.NotNull(codeReason.IsThrottledOnLogSpace);
Assert.NotNull(codeReason.IsThrottledOnLogWrite);
Assert.NotNull(codeReason.IsThrottledOnDataRead);
Assert.NotNull(codeReason.IsThrottledOnCPU);
Assert.NotNull(codeReason.IsThrottledOnDatabaseSize);
Assert.NotNull(codeReason.IsThrottledOnWorkerThreads);
Assert.NotNull(codeReason.IsUnknown);
Assert.NotNull(codeReason.ToString());
}
[Fact]
public void RetryErrorsTest()
{
var sqlServerRetryError = new SqlServerRetryError(
"test message", new Exception(),
1, 200, ErrorSeverity.Warning);
Assert.True(sqlServerRetryError.RetryCount == 1);
Assert.NotNull(SqlServerRetryError.FormatRetryMessage(1, TimeSpan.FromSeconds(15), new Exception()));
Assert.NotNull(SqlServerRetryError.FormatIgnoreMessage(1, new Exception()));
var sqlServerError1 = new SqlServerError("test message", "document", ErrorSeverity.Warning);
var sqlServerError2 = new SqlServerError("test message", "document", 1, ErrorSeverity.Warning);
var sqlServerError3 = new SqlServerError(new Exception(), "document",1, ErrorSeverity.Warning);
var sqlServerError4 = new SqlServerError("test message", "document", 1, 2, ErrorSeverity.Warning);
var sqlServerError5 = new SqlServerError(new Exception(), "document", 1, 2, 3, ErrorSeverity.Warning);
var sqlServerError6 = new SqlServerError("test message", "document", 1, 2, 3, ErrorSeverity.Warning);
var sqlServerError7 = new SqlServerError("test message", new Exception(), "document", 1, 2, 3, ErrorSeverity.Warning);
Assert.True(SqlSchemaModelErrorCodes.IsParseErrorCode(46010));
Assert.True(SqlSchemaModelErrorCodes.IsInterpretationErrorCode(Interpretation.InterpretationBaseCode+ 1));
Assert.True(SqlSchemaModelErrorCodes.IsStatementFilterError(StatementFilter.StatementFilterBaseCode + 1));
}
[Fact]
public void RetryCallbackEventArgsTest()
{
var exception = new Exception();
var timespan = TimeSpan.FromMinutes(1);
// Given a RetryCallbackEventArgs object with certain parameters
var args = new RetryCallbackEventArgs(5, exception, timespan);
// If I check the properties on the object
// Then I expect the values to be the same as the values I passed into the constructor
Assert.Equal(5, args.RetryCount);
Assert.Equal(exception, args.Exception);
Assert.Equal(timespan, args.Delay);
}
[Fact]
public void CheckStaticVariables()
{
Assert.NotNull(ReliableConnectionHelper.BuilderWithDefaultApplicationName);
}
[Fact]
public void SetLockAndCommandTimeoutThrowsOnNull()
{
Assert.Throws(typeof(ArgumentNullException), () => ReliableConnectionHelper.SetLockAndCommandTimeout(null));
}
[Fact]
public void StandardExceptionHandlerTests()
{
Assert.True(ReliableConnectionHelper.StandardExceptionHandler(new InvalidCastException()));
Assert.False(ReliableConnectionHelper.StandardExceptionHandler(new Exception()));
}
[Fact]
public void GetConnectionStringBuilderNullConnectionString()
{
SqlConnectionStringBuilder builder;
Assert.False(ReliableConnectionHelper.TryGetConnectionStringBuilder(null, out builder));
}
[Fact]
public void GetConnectionStringBuilderExceptionTests()
{
SqlConnectionStringBuilder builder;
// throws ArgumentException
Assert.False(ReliableConnectionHelper.TryGetConnectionStringBuilder("IntegratedGoldFish=True", out builder));
// throws FormatException
Assert.False(ReliableConnectionHelper.TryGetConnectionStringBuilder("rabbits**frogs**lizards", out builder));
}
[Fact]
public void GetCompleteServerNameTests()
{
Assert.Null(ReliableConnectionHelper.GetCompleteServerName(null));
Assert.NotNull(ReliableConnectionHelper.GetCompleteServerName("localhost"));
Assert.NotNull(ReliableConnectionHelper.GetCompleteServerName("mytestservername"));
}
[Fact]
public void ReliableSqlCommandConstructorTests()
{
// verify default constructor doesn't throw
Assert.NotNull(new ReliableSqlConnection.ReliableSqlCommand());
// verify constructor with null connection doesn't throw
Assert.NotNull(new ReliableSqlConnection.ReliableSqlCommand(null));
}
[Fact]
public void ReliableSqlCommandProperties()
{
var command = new ReliableSqlConnection.ReliableSqlCommand();
command.CommandText = "SELECT 1";
Assert.Equal(command.CommandText, "SELECT 1");
Assert.NotNull(command.CommandTimeout);
Assert.NotNull(command.CommandType);
command.DesignTimeVisible = true;
Assert.True(command.DesignTimeVisible);
command.UpdatedRowSource = UpdateRowSource.None;
Assert.Equal(command.UpdatedRowSource, UpdateRowSource.None);
Assert.NotNull(command.GetUnderlyingCommand());
Assert.Throws<InvalidOperationException>(() => command.ValidateConnectionIsSet());
command.Prepare();
Assert.NotNull(command.CreateParameter());
command.Cancel();
}
[Fact]
public void ReliableConnectionResourcesTests()
{
Assert.NotNull(Resources.ConnectionPassedToIsCloudShouldBeOpen);
Assert.NotNull(Resources.ExceptionCannotBeRetried);
Assert.NotNull(Resources.FailedToCacheIsCloud);
Assert.NotNull(Resources.FailedToParseConnectionString);
Assert.NotNull(Resources.InvalidCommandType);
Assert.NotNull(Resources.InvalidConnectionType);
Assert.NotNull(Resources.OnlyReliableConnectionSupported);
Assert.NotNull(Resources.UnableToAssignValue);
Assert.NotNull(Resources.UnableToRetrieveAzureSessionId);
}
[Fact]
public void CalcExponentialRetryDelayWithSchemaDefaultsTest()
{
Assert.NotNull(RetryPolicyUtils.CalcExponentialRetryDelayWithSchemaDefaults(1));
}
[Fact]
public void IsSupportedCommandNullCommandTest()
{
Assert.False(DbCommandWrapper.IsSupportedCommand(null));
}
[Fact]
public void StatementCompletedTests()
{
bool handlerCalled = false;
StatementCompletedEventHandler handler = (s, e) => handlerCalled = true;
var command = new DbCommandWrapper(new SqlCommand());
command.StatementCompleted += handler;
command.StatementCompleted -= handler;
}
}
}

View File

@@ -0,0 +1,139 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using Microsoft.SqlServer.Management.SqlParser.Parser;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer
{
/// <summary>
/// Tests for the ServiceHost Language Service tests
/// </summary>
public class LanguageServiceTests
{
private static void GetLiveAutoCompleteTestObjects(
out TextDocumentPosition textDocument,
out ScriptFile scriptFile,
out ConnectionInfo connInfo)
{
textDocument = new TextDocumentPosition
{
TextDocument = new TextDocumentIdentifier { Uri = TestObjects.ScriptUri },
Position = new Position
{
Line = 0,
Character = 0
}
};
connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
}
/// <summary>
/// Test the service initialization code path and verify nothing throws
/// </summary>
[Fact]
public void ServiceInitialization()
{
try
{
TestObjects.InitializeTestServices();
}
catch (System.ArgumentException)
{
}
Assert.True(LanguageService.Instance.Context != null);
Assert.True(LanguageService.ConnectionServiceInstance != null);
Assert.True(LanguageService.Instance.CurrentSettings != null);
Assert.True(LanguageService.Instance.CurrentWorkspace != null);
}
/// <summary>
/// Test the service initialization code path and verify nothing throws
/// </summary>
[Fact]
public void PrepopulateCommonMetadata()
{
ScriptFile scriptFile;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = true };
AutoCompleteHelper.PrepopulateCommonMetadata(connInfo, scriptInfo, null);
}
// This test currently requires a live database connection to initialize
// SMO connected metadata provider. Since we don't want a live DB dependency
// in the CI unit tests this scenario is currently disabled.
[Fact]
public void AutoCompleteFindCompletions()
{
TextDocumentPosition textDocument;
ConnectionInfo connInfo;
ScriptFile scriptFile;
GetLiveAutoCompleteTestObjects(out textDocument, out scriptFile, out connInfo);
textDocument.Position.Character = 7;
scriptFile.Contents = "select ";
var autoCompleteService = LanguageService.Instance;
var completions = autoCompleteService.GetCompletionItems(
textDocument,
scriptFile,
connInfo);
Assert.True(completions.Length > 0);
}
/// <summary>
/// Verify that GetSignatureHelp returns not null when the provided TextDocumentPosition
/// has an associated ScriptParseInfo and the provided query has a function that should
/// provide signature help.
/// </summary>
[Fact]
public async void GetSignatureHelpReturnsNotNullIfParseInfoInitialized()
{
// When we make a connection to a live database
ScriptFile scriptFile;
Hosting.ServiceHost.SendEventIgnoreExceptions = true;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
// And we place the cursor after a function that should prompt for signature help
string queryWithFunction = "EXEC sys.fn_isrolemember ";
scriptFile.Contents = queryWithFunction;
TextDocumentPosition textDocument = new TextDocumentPosition
{
TextDocument = new TextDocumentIdentifier
{
Uri = scriptFile.ClientFilePath
},
Position = new Position
{
Line = 0,
Character = queryWithFunction.Length
}
};
// If the SQL has already been parsed
var service = LanguageService.Instance;
await service.UpdateLanguageServiceOnConnection(connInfo);
// We should get back a non-null ScriptParseInfo
ScriptParseInfo parseInfo = service.GetScriptParseInfo(scriptFile.ClientFilePath);
Assert.NotNull(parseInfo);
// And we should get back a non-null SignatureHelp
SignatureHelp signatureHelp = service.GetSignatureHelp(textDocument, scriptFile);
Assert.NotNull(signatureHelp);
}
}
}

View File

@@ -0,0 +1,344 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.SqlParser.Binder;
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
using Microsoft.SqlServer.Management.SqlParser.Parser;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.SqlContext;
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
using Microsoft.SqlTools.ServiceLayer.Workspace;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Test.Utility;
using Moq;
using Xunit;
using Location = Microsoft.SqlTools.ServiceLayer.Workspace.Contracts.Location;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServices
{
/// <summary>
/// Tests for the language service peek definition/ go to definition feature
/// </summary>
public class PeekDefinitionTests
{
private const int TaskTimeout = 30000;
private readonly string testScriptUri = TestObjects.ScriptUri;
private readonly string testConnectionKey = "testdbcontextkey";
private Mock<ConnectedBindingQueue> bindingQueue;
private Mock<WorkspaceService<SqlToolsSettings>> workspaceService;
private Mock<RequestContext<Location[]>> requestContext;
private Mock<IBinder> binder;
private TextDocumentPosition textDocument;
private const string OwnerUri = "testFile1";
/// <summary>
/// Test get definition for a table object with active connection
/// </summary>
[Fact]
public void GetValidTableDefinitionTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "spt_monitor";
string schemaName = null;
string objectType = "TABLE";
// Get locations for valid table object
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetTableScripts, objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
/// <summary>
/// Test get definition for a invalid table object with active connection
/// </summary>
[Fact]
public void GetTableDefinitionInvalidObjectTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "test_invalid";
string schemaName = null;
string objectType = "TABLE";
// Get locations for invalid table object
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetTableScripts, objectName, schemaName, objectType);
Assert.Null(locations);
}
/// <summary>
/// Test get definition for a valid table object with schema and active connection
/// </summary>
[Fact]
public void GetTableDefinitionWithSchemaTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "spt_monitor";
string schemaName = "dbo";
string objectType = "TABLE";
// Get locations for valid table object with schema name
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetTableScripts, objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
/// <summary>
/// Test GetDefinition with an unsupported type(schema - dbo). Expect a error result.
/// </summary>
[Fact]
public void GetUnsupportedDefinitionErrorTest()
{
ScriptFile scriptFile;
TextDocumentPosition textDocument = new TextDocumentPosition
{
TextDocument = new TextDocumentIdentifier { Uri = OwnerUri },
Position = new Position
{
Line = 0,
// test for 'dbo'
Character = 16
}
};
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
scriptFile.Contents = "select * from dbo.func ()";
var languageService = new LanguageService();
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = true };
languageService.ScriptParseInfoMap.Add(OwnerUri, scriptInfo);
// When I call the language service
var result = languageService.GetDefinition(textDocument, scriptFile, connInfo);
// Then I expect null locations and an error to be reported
Assert.NotNull(result);
Assert.True(result.IsErrorResult);
}
/// <summary>
/// Get Definition for a object with no definition. Expect a error result
/// </summary>
[Fact]
public void GetDefinitionWithNoResultsFoundError()
{
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "from";
List<Declaration> declarations = new List<Declaration>();
DefinitionResult result = peekDefinition.GetScript(declarations, objectName, null);
Assert.NotNull(result);
Assert.True(result.IsErrorResult);
Assert.Equal(SR.PeekDefinitionNoResultsError, result.Message);
}
/// <summary>
/// Test GetDefinition with a forced timeout. Expect a error result.
/// </summary>
[Fact]
public void GetDefinitionTimeoutTest()
{
// Given a binding queue that will automatically time out
var languageService = new LanguageService();
Mock<ConnectedBindingQueue> queueMock = new Mock<ConnectedBindingQueue>();
languageService.BindingQueue = queueMock.Object;
ManualResetEvent mre = new ManualResetEvent(true); // Do not block
Mock<QueueItem> itemMock = new Mock<QueueItem>();
itemMock.Setup(i => i.ItemProcessed).Returns(mre);
DefinitionResult timeoutResult = null;
queueMock.Setup(q => q.QueueBindingOperation(
It.IsAny<string>(),
It.IsAny<Func<IBindingContext, CancellationToken, object>>(),
It.IsAny<Func<IBindingContext, object>>(),
It.IsAny<int?>(),
It.IsAny<int?>()))
.Callback<string, Func<IBindingContext, CancellationToken, object>, Func<IBindingContext, object>, int?, int?>(
(key, bindOperation, timeoutOperation, blah, blah2) =>
{
timeoutResult = (DefinitionResult) timeoutOperation((IBindingContext)null);
itemMock.Object.Result = timeoutResult;
})
.Returns(() => itemMock.Object);
ScriptFile scriptFile;
TextDocumentPosition textDocument = new TextDocumentPosition
{
TextDocument = new TextDocumentIdentifier { Uri = OwnerUri },
Position = new Position
{
Line = 0,
Character = 20
}
};
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
scriptFile.Contents = "select * from dbo.func ()";
ScriptParseInfo scriptInfo = new ScriptParseInfo { IsConnected = true };
languageService.ScriptParseInfoMap.Add(OwnerUri, scriptInfo);
// When I call the language service
var result = languageService.GetDefinition(textDocument, scriptFile, connInfo);
// Then I expect null locations and an error to be reported
Assert.NotNull(result);
Assert.True(result.IsErrorResult);
// Check timeout message
Assert.Equal(SR.PeekDefinitionTimedoutError, result.Message);
}
/// <summary>
/// Test get definition for a view object with active connection
/// </summary>
[Fact]
public void GetValidViewDefinitionTest()
{
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "objects";
string schemaName = "sys";
string objectType = "VIEW";
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetViewScripts, objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
/// <summary>
/// Test get definition for an invalid view object with no schema name and with active connection
/// </summary>
[Fact]
public void GetViewDefinitionInvalidObjectTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "objects";
string schemaName = null;
string objectType = "VIEW";
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetViewScripts, objectName, schemaName, objectType);
Assert.Null(locations);
}
/// <summary>
/// Test get definition for a stored procedure object with active connection
/// </summary>
[Fact]
public void GetStoredProcedureDefinitionTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "sp_MSrepl_startup";
string schemaName = "dbo";
string objectType = "PROCEDURE";
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetStoredProcedureScripts, objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
/// <summary>
/// Test get definition for a stored procedure object that does not exist with active connection
/// </summary>
[Fact]
public void GetStoredProcedureDefinitionFailureTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "SP2";
string schemaName = "dbo";
string objectType = "PROCEDURE";
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetStoredProcedureScripts, objectName, schemaName, objectType);
Assert.Null(locations);
}
/// <summary>
/// Test get definition for a stored procedure object with active connection and no schema
/// </summary>
[Fact]
public void GetStoredProcedureDefinitionWithoutSchemaTest()
{
// Get live connectionInfo and serverConnection
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfoForDefinition();
ServerConnection serverConnection = TestObjects.InitLiveServerConnectionForDefinition(connInfo);
PeekDefinition peekDefinition = new PeekDefinition(serverConnection, connInfo);
string objectName = "sp_MSrepl_startup";
string schemaName = null;
string objectType = "PROCEDURE";
Location[] locations = peekDefinition.GetSqlObjectDefinition(peekDefinition.GetStoredProcedureScripts, objectName, schemaName, objectType);
Assert.NotNull(locations);
Cleanup(locations);
}
/// <summary>
/// Helper method to clean up script files
/// </summary>
private void Cleanup(Location[] locations)
{
Uri fileUri = new Uri(locations[0].Uri);
if (File.Exists(fileUri.LocalPath))
{
try
{
File.Delete(fileUri.LocalPath);
}
catch(Exception)
{
}
}
}
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>08af0209-d598-47bb-9dfd-fc9e74c0fe56</ProjectGuid>
<RootNamespace>Microsoft.SqlTools.ServiceLayer.IntegrationTests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'==''">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@@ -0,0 +1,41 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("SqlToolsEditorServices.Test.Transport.Stdio")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SqlToolsEditorServices.Test.Transport.Stdio")]
[assembly: AssemblyCopyright("Copyright <20> 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("07137FCA-76D0-4CE7-9764-C21DB7A57093")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,106 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Data.Common;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.QueryExecution.DataStorage;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.QueryExecution.DataStorage
{
public class StorageDataReaderTests
{
private StorageDataReader GetTestStorageDataReader(out DbDataReader reader, string query)
{
ScriptFile scriptFile;
ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);
var command = connInfo.SqlConnection.CreateCommand();
command.CommandText = query;
reader = command.ExecuteReader();
return new StorageDataReader(reader);
}
/// <summary>
/// Validate GetBytesWithMaxCapacity
/// </summary>
[Fact]
public void GetBytesWithMaxCapacityTest()
{
DbDataReader reader;
var storageReader = GetTestStorageDataReader(
out reader,
"SELECT CAST([name] as TEXT) As TextName FROM sys.all_columns");
reader.Read();
Assert.False(storageReader.IsDBNull(0));
byte[] bytes = storageReader.GetBytesWithMaxCapacity(0, 100);
Assert.NotNull(bytes);
}
/// <summary>
/// Validate GetCharsWithMaxCapacity
/// </summary>
[Fact]
public void GetCharsWithMaxCapacityTest()
{
DbDataReader reader;
var storageReader = GetTestStorageDataReader(
out reader,
"SELECT name FROM sys.all_columns");
reader.Read();
Assert.False(storageReader.IsDBNull(0));
Assert.NotNull(storageReader.GetValue(0));
string shortName = storageReader.GetCharsWithMaxCapacity(0, 2);
Assert.True(shortName.Length == 2);
Assert.Throws<ArgumentOutOfRangeException>(() => storageReader.GetBytesWithMaxCapacity(0, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => storageReader.GetCharsWithMaxCapacity(0, 0));
}
/// <summary>
/// Validate GetXmlWithMaxCapacity
/// </summary>
[Fact]
public void GetXmlWithMaxCapacityTest()
{
DbDataReader reader;
var storageReader = GetTestStorageDataReader(
out reader,
"SELECT CAST('<xml>Test XML context</xml>' AS XML) As XmlColumn");
reader.Read();
Assert.False(storageReader.IsDBNull(0));
string shortXml = storageReader.GetXmlWithMaxCapacity(0, 2);
Assert.True(shortXml.Length == 3);
}
/// <summary>
/// Validate StringWriterWithMaxCapacity Write test
/// </summary>
[Fact]
public void StringWriterWithMaxCapacityTest()
{
var writer = new StorageDataReader.StringWriterWithMaxCapacity(null, 4);
string output = "...";
writer.Write(output);
Assert.True(writer.ToString().Equals(output));
writer.Write('.');
Assert.True(writer.ToString().Equals(output + '.'));
writer.Write(output);
writer.Write('.');
Assert.True(writer.ToString().Equals(output + '.'));
}
}
}

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net461" />
<package id="xunit" version="2.1.0" targetFramework="net45" />
<package id="xunit.abstractions" version="2.0.0" targetFramework="net45" />
<package id="xunit.assert" version="2.1.0" targetFramework="net45" />
<package id="xunit.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.core" version="2.1.0" targetFramework="net45" />
<package id="xunit.extensibility.execution" version="2.1.0" targetFramework="net45" />
<package id="xunit.runner.visualstudio" version="2.1.0" targetFramework="net45" />
</packages>

View File

@@ -0,0 +1,47 @@
{
"name": "Microsoft.SqlTools.ServiceLayer.IntegrationTests",
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
},
"configurations": {
"Integration": {
"buildOptions": {
"define": [
"WINDOWS_ONLY_BUILD" ]
}
}
},
"dependencies": {
"Newtonsoft.Json": "9.0.1",
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.4.0-sqltools-24613-04",
"Microsoft.SqlServer.Smo": "140.1.12",
"System.Security.SecureString": "4.0.0",
"System.Collections.Specialized": "4.0.1",
"System.ComponentModel.TypeConverter": "4.1.0",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-192208-24",
"Microsoft.SqlTools.ServiceLayer": {
"target": "project"
},
"Moq": "4.6.36-alpha",
"Microsoft.SqlTools.ServiceLayer.Test": "1.0.0-*"
},
"testRunner": "xunit",
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
}
},
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
}
}