//
// 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;
using System.Data.Common;
using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility
{
///
/// Tests for the ServiceHost Connection Service tests
///
public class TestObjects
{
public const string ScriptUri = "file://some/file.sql";
///
/// Creates a test connection service
///
public static ConnectionService GetTestConnectionService()
{
// use mock database connection
return new ConnectionService(new TestSqlConnectionFactory());
}
///
/// Creates a test connection info instance.
///
public static ConnectionInfo GetTestConnectionInfo()
{
return new ConnectionInfo(
new TestSqlConnectionFactory(),
ScriptUri,
GetTestConnectionDetails());
}
public static ConnectParams GetTestConnectionParams()
{
return new ConnectParams()
{
OwnerUri = ScriptUri,
Connection = GetTestConnectionDetails()
};
}
///
/// Creates a test connection details object
///
public static ConnectionDetails GetTestConnectionDetails()
{
return new ConnectionDetails()
{
UserName = "user",
Password = "password",
DatabaseName = "databaseName",
ServerName = "serverName"
};
}
///
/// Create a test language service instance
///
///
public static LanguageService GetTestLanguageService()
{
return new LanguageService();
}
///
/// Creates and returns a dummy TextDocumentPosition
///
public static TextDocumentPosition GetTestDocPosition()
{
return new TextDocumentPosition
{
TextDocument = new TextDocumentIdentifier { Uri = ScriptUri },
Position = new Position
{
Line = 0,
Character = 0
}
};
}
}
///
/// Test mock class for IDbCommand
///
public class TestSqlCommand : DbCommand
{
internal TestSqlCommand(TestResultSet[] data)
{
Data = data;
}
internal TestResultSet[] 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);
}
}
///
/// Test mock class for SqlConnection wrapper
///
public class TestSqlConnection : DbConnection
{
internal TestSqlConnection(TestResultSet[] data)
{
Data = data;
}
internal TestResultSet[] 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
}
}
///
/// Test mock class for SqlConnection factory
///
public class TestSqlConnectionFactory : ISqlConnectionFactory
{
public DbConnection CreateSqlConnection(string connectionString)
{
return new TestSqlConnection(null)
{
ConnectionString = connectionString
};
}
}
}