mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:59:48 -05:00
* Initial commit for GitHub IO pages * Add initial doxfx content * Update manifest.json * Update manifest.json * Set theme jekyll-theme-cayman * Set theme jekyll-theme-slate * Set theme jekyll-theme-minimal * Set theme jekyll-theme-tactile * Clear out theme setting * Remove API docs * Revert "Adding Milliseconds to DateTime fields (#173)" (#197) This reverts commit431dfa4156. * ported new BatchParser * added BatchParser tests * fixing merge conflicts * fix build issues * cleaned code and addressed comments from code review * addressed code review and made BatchParser logic more efficient * fixed batch parser tests * changed class name to fix build issues * fixed merge conflicts * added path for lab mode baseline tests * changed env path for lab mode * added env variable to appveyor * testing env variable for appveyor * fixed lab build * debug appveyor build * testing changes for appveyor * changed trace env path * debugging appveyor build * changed baseline env path * debugging * debugging * debugging * switched on trace flag * debugging * debugging * changed build config * changed baseline files * checking baseline output * changed baseline files * debug baseline tests * debugging baseline * debugging * debugging * debug * debugging * testing baseline format * debug * debug * debug * debug * debug * newline debug * changed baseline file * debug * test * try new way to read * added execution engine tests * change test * testing file encoding * moved execution engine tests to integration * try compare without spaces * removed no op test * added env variable for travis * put batch parser tests to integration too * put batch parser in integration * try new baseline string match * compare baseline test logic changed * changed baseline logic as well as cleaned tests * fix build for travis CI * fix travis CI issues * fixed highlighting bugs on vscode * code review changes * ported new BatchParser * added BatchParser tests * Initial commit for GitHub IO pages * Add initial doxfx content * Update manifest.json * Update manifest.json * Set theme jekyll-theme-cayman * Set theme jekyll-theme-slate * Set theme jekyll-theme-minimal * Set theme jekyll-theme-tactile * Clear out theme setting * Remove API docs * Revert "Adding Milliseconds to DateTime fields (#173)" (#197) This reverts commit431dfa4156. * fixing merge conflicts * fix build issues * cleaned code and addressed comments from code review * addressed code review and made BatchParser logic more efficient * fixed batch parser tests * changed class name to fix build issues * fixed merge conflicts * added path for lab mode baseline tests changed env path for lab mode added env variable to appveyor testing env variable for appveyor fixed lab build debug appveyor build testing changes for appveyor changed trace env path debugging appveyor build changed baseline env path debugging debugging debugging switched on trace flag debugging debugging changed build config changed baseline files checking baseline output changed baseline files debug baseline tests debugging baseline debugging debugging debug debugging testing baseline format debug debug debug debug debug newline debug changed baseline file debug test try new way to read added execution engine tests change test testing file encoding moved execution engine tests to integration try compare without spaces removed no op test added env variable for travis * put batch parser tests to integration too * put batch parser in integration try new baseline string match * compare baseline test logic changed * changed baseline logic as well as cleaned tests * fix build for travis CI * fix travis CI issues * fixed highlighting bugs on vscode * code review changes * fixed filestream writer test * added localization string * added localization string * generated new string files again * code review changes
320 lines
10 KiB
C#
320 lines
10 KiB
C#
//
|
|
// 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.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using Microsoft.SqlServer.Management.Common;
|
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
|
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.Test.Utility
|
|
{
|
|
/// <summary>
|
|
/// Tests for the ServiceHost Connection Service tests
|
|
/// </summary>
|
|
public class TestObjects
|
|
{
|
|
public const string ScriptUriTemplate = "file://some/{0}.sql";
|
|
public const string ScriptUri = "file://some/file.sql";
|
|
private static TestServiceProvider _serviceProvider = TestServiceProvider.Instance;
|
|
|
|
/// <summary>
|
|
/// Creates a test connection service
|
|
/// </summary>
|
|
public static ConnectionService GetTestConnectionService()
|
|
{
|
|
// use mock database connection
|
|
return new ConnectionService(new TestSqlConnectionFactory());
|
|
}
|
|
|
|
public static ConnectionService GetLiveTestConnectionService()
|
|
{
|
|
// connect to a real server instance
|
|
return ConnectionService.Instance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a test connection info instance.
|
|
/// </summary>
|
|
public static ConnectionInfo GetTestConnectionInfo()
|
|
{
|
|
return new ConnectionInfo(
|
|
GetTestSqlConnectionFactory(),
|
|
ScriptUri,
|
|
GetTestConnectionDetails());
|
|
}
|
|
|
|
public static ConnectParams GetTestConnectionParams()
|
|
{
|
|
return new ConnectParams()
|
|
{
|
|
OwnerUri = ScriptUri,
|
|
Connection = GetTestConnectionDetails()
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a test connection details object
|
|
/// </summary>
|
|
public static ConnectionDetails GetTestConnectionDetails()
|
|
{
|
|
return new ConnectionDetails()
|
|
{
|
|
UserName = "user",
|
|
Password = "password",
|
|
DatabaseName = "databaseName",
|
|
ServerName = "serverName"
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a test language service instance
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static LanguageService GetTestLanguageService()
|
|
{
|
|
return new LanguageService();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a test sql connection factory instance
|
|
/// </summary>
|
|
public static ISqlConnectionFactory GetTestSqlConnectionFactory()
|
|
{
|
|
// use mock database connection
|
|
return new TestSqlConnectionFactory();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a test sql connection factory instance
|
|
/// </summary>
|
|
public static ISqlConnectionFactory GetLiveTestSqlConnectionFactory()
|
|
{
|
|
// connect to a real server instance
|
|
return ConnectionService.Instance.ConnectionFactory;
|
|
}
|
|
|
|
|
|
|
|
public static string GetTestSqlFile()
|
|
{
|
|
string filePath = Path.Combine(
|
|
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
|
|
"sqltest.sql");
|
|
|
|
if (File.Exists(filePath))
|
|
{
|
|
File.Delete(filePath);
|
|
}
|
|
|
|
File.WriteAllText(filePath, "SELECT * FROM sys.objects\n");
|
|
|
|
return filePath;
|
|
}
|
|
|
|
public static TestConnectionResult InitLiveConnectionInfo()
|
|
{
|
|
string sqlFilePath = GetTestSqlFile();
|
|
ScriptFile scriptFile = TestServiceProvider.Instance.WorkspaceService.Workspace.GetFile(sqlFilePath);
|
|
ConnectParams connectParams = TestServiceProvider.Instance.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem);
|
|
|
|
string ownerUri = scriptFile.ClientFilePath;
|
|
var connectionService = TestObjects.GetLiveTestConnectionService();
|
|
var connectionResult =
|
|
connectionService
|
|
.Connect(new ConnectParams()
|
|
{
|
|
OwnerUri = ownerUri,
|
|
Connection = connectParams.Connection
|
|
});
|
|
|
|
connectionResult.Wait();
|
|
|
|
ConnectionInfo connInfo = null;
|
|
connectionService.TryFindConnection(ownerUri, out connInfo);
|
|
return new TestConnectionResult () { ConnectionInfo = connInfo, ScriptFile = scriptFile };
|
|
}
|
|
|
|
public static ConnectionInfo InitLiveConnectionInfoForDefinition(string databaseName = null)
|
|
{
|
|
ConnectParams connectParams = _serviceProvider.ConnectionProfileService.GetConnectionParameters(TestServerType.OnPrem, databaseName);
|
|
|
|
string ownerUri = string.Format(CultureInfo.InvariantCulture, ScriptUriTemplate, string.IsNullOrEmpty(databaseName) ? "file" : databaseName);
|
|
var connectionService = TestObjects.GetLiveTestConnectionService();
|
|
var connectionResult =
|
|
connectionService
|
|
.Connect(new ConnectParams()
|
|
{
|
|
OwnerUri = ownerUri,
|
|
Connection = connectParams.Connection
|
|
});
|
|
|
|
connectionResult.Wait();
|
|
|
|
ConnectionInfo connInfo = null;
|
|
connectionService.TryFindConnection(ownerUri, out connInfo);
|
|
|
|
Assert.NotNull(connInfo);
|
|
return connInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates and returns a dummy TextDocumentPosition
|
|
/// </summary>
|
|
public static TextDocumentPosition GetTestDocPosition()
|
|
{
|
|
return new TextDocumentPosition
|
|
{
|
|
TextDocument = new TextDocumentIdentifier { Uri = ScriptUri },
|
|
Position = new Position
|
|
{
|
|
Line = 0,
|
|
Character = 0
|
|
}
|
|
};
|
|
}
|
|
|
|
public static ServerConnection InitLiveServerConnectionForDefinition(ConnectionInfo connInfo)
|
|
{
|
|
SqlConnection sqlConn = new SqlConnection(ConnectionService.BuildConnectionString(connInfo.ConnectionDetails));
|
|
return new ServerConnection(sqlConn);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test mock class for IDbCommand
|
|
/// </summary>
|
|
public class TestSqlCommand : DbCommand
|
|
{
|
|
internal TestSqlCommand(Dictionary<string, string>[][] data)
|
|
{
|
|
Data = data;
|
|
}
|
|
|
|
internal Dictionary<string, string>[][] Data { get; set; }
|
|
|
|
public override void Cancel()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override int ExecuteNonQuery()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override object ExecuteScalar()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override void Prepare()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override string CommandText { get; set; }
|
|
public override int CommandTimeout { get; set; }
|
|
public override CommandType CommandType { get; set; }
|
|
public override UpdateRowSource UpdatedRowSource { get; set; }
|
|
protected override DbConnection DbConnection { get; set; }
|
|
protected override DbParameterCollection DbParameterCollection { get; }
|
|
protected override DbTransaction DbTransaction { get; set; }
|
|
public override bool DesignTimeVisible { get; set; }
|
|
|
|
protected override DbParameter CreateDbParameter()
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
|
|
{
|
|
return new TestDbDataReader(Data);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test mock class for SqlConnection wrapper
|
|
/// </summary>
|
|
public class TestSqlConnection : DbConnection
|
|
{
|
|
internal TestSqlConnection(Dictionary<string, string>[][] data)
|
|
{
|
|
Data = data;
|
|
}
|
|
|
|
internal Dictionary<string, string>[][] Data { get; set; }
|
|
|
|
protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public override void Close()
|
|
{
|
|
// No Op
|
|
}
|
|
|
|
public override void Open()
|
|
{
|
|
// No Op, unless credentials are bad
|
|
if(ConnectionString.Contains("invalidUsername"))
|
|
{
|
|
throw new Exception("Invalid credentials provided");
|
|
}
|
|
}
|
|
|
|
public override string ConnectionString { get; set; }
|
|
public override string Database { get; }
|
|
public override ConnectionState State { get; }
|
|
public override string DataSource { get; }
|
|
public override string ServerVersion { get; }
|
|
|
|
protected override DbCommand CreateDbCommand()
|
|
{
|
|
return new TestSqlCommand(Data);
|
|
}
|
|
|
|
public override void ChangeDatabase(string databaseName)
|
|
{
|
|
// No Op
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Test mock class for SqlConnection factory
|
|
/// </summary>
|
|
public class TestSqlConnectionFactory : ISqlConnectionFactory
|
|
{
|
|
public DbConnection CreateSqlConnection(string connectionString)
|
|
{
|
|
return new TestSqlConnection(null)
|
|
{
|
|
ConnectionString = connectionString
|
|
};
|
|
}
|
|
}
|
|
|
|
public class TestConnectionResult
|
|
{
|
|
public ConnectionInfo ConnectionInfo { get; set; }
|
|
|
|
public ScriptFile ScriptFile { get; set; }
|
|
|
|
public TextDocumentPosition TextDocumentPosition { get; set; }
|
|
}
|
|
}
|