using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Threading.Tasks; using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol; using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts; using Microsoft.SqlTools.ServiceLayer.QueryExecution; using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts; using Microsoft.SqlTools.ServiceLayer.Test.Utility; using Moq; using Moq.Protected; namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution { public class Common { public const string OwnerUri = "testFile"; public static readonly Dictionary[] StandardTestData = { new Dictionary { {"col1", "val11"}, { "col2", "val12"}, { "col3", "val13"}, { "col4", "col14"} }, new Dictionary { {"col1", "val21"}, { "col2", "val22"}, { "col3", "val23"}, { "col4", "col24"} }, new Dictionary { {"col1", "val31"}, { "col2", "val32"}, { "col3", "val33"}, { "col4", "col34"} }, new Dictionary { {"col1", "val41"}, { "col2", "val42"}, { "col3", "val43"}, { "col4", "col44"} }, new Dictionary { {"col1", "val51"}, { "col2", "val52"}, { "col3", "val53"}, { "col4", "col54"} }, }; public static Dictionary[] GetTestData(int columns, int rows) { Dictionary[] output = new Dictionary[rows]; for (int row = 0; row < rows; row++) { Dictionary rowDictionary = new Dictionary(); for (int column = 0; column < columns; column++) { rowDictionary.Add(string.Format("column{0}", column), string.Format("val{0}{1}", column, row)); } output[row] = rowDictionary; } return output; } public static Query GetBasicExecutedQuery() { Query query = new Query("SIMPLE QUERY", CreateTestConnectionInfo(new[] { StandardTestData }, false)); query.Execute().Wait(); return query; } #region DbConnection Mocking public static DbCommand CreateTestCommand(Dictionary[][] data, bool throwOnRead) { var commandMock = new Mock { CallBase = true }; var commandMockSetup = commandMock.Protected() .Setup("ExecuteDbDataReader", It.IsAny()); // Setup the expected behavior if (throwOnRead) { commandMockSetup.Throws(new Mock().Object); } else { commandMockSetup.Returns(new TestDbDataReader(data)); } return commandMock.Object; } public static DbConnection CreateTestConnection(Dictionary[][] data, bool throwOnRead) { var connectionMock = new Mock { CallBase = true }; connectionMock.Protected() .Setup("CreateDbCommand") .Returns(CreateTestCommand(data, throwOnRead)); return connectionMock.Object; } public static ISqlConnectionFactory CreateMockFactory(Dictionary[][] data, bool throwOnRead) { var mockFactory = new Mock(); mockFactory.Setup(factory => factory.CreateSqlConnection(It.IsAny())) .Returns(CreateTestConnection(data, throwOnRead)); return mockFactory.Object; } public static ConnectionInfo CreateTestConnectionInfo(Dictionary[][] data, bool throwOnRead) { // Create connection info ConnectionDetails connDetails = new ConnectionDetails { UserName = "sa", Password = "Yukon900", DatabaseName = "AdventureWorks2016CTP3_2", ServerName = "sqltools11" }; return new ConnectionInfo(CreateMockFactory(data, throwOnRead), "test://test", connDetails); } #endregion #region Service Mocking public static ConnectionDetails GetTestConnectionDetails() { return new ConnectionDetails { DatabaseName = "123", Password = "456", ServerName = "789", UserName = "012" }; } public static QueryExecutionService GetPrimedExecutionService(ISqlConnectionFactory factory, bool isConnected) { var connectionService = new ConnectionService(factory); if (isConnected) { connectionService.Connect(new ConnectParams { Connection = GetTestConnectionDetails(), OwnerUri = OwnerUri }); } return new QueryExecutionService(connectionService); } #endregion #region Request Mocking public static Mock> GetQueryExecuteResultContextMock( Action resultCallback, Action, QueryExecuteCompleteParams> eventCallback, Action errorCallback) { var requestContext = new Mock>(); // Setup the mock for SendResult var sendResultFlow = requestContext .Setup(rc => rc.SendResult(It.IsAny())) .Returns(Task.FromResult(0)); if (resultCallback != null) { sendResultFlow.Callback(resultCallback); } // Setup the mock for SendEvent var sendEventFlow = requestContext.Setup(rc => rc.SendEvent( It.Is>(m => m == QueryExecuteCompleteEvent.Type), It.IsAny())) .Returns(Task.FromResult(0)); if (eventCallback != null) { sendEventFlow.Callback(eventCallback); } // Setup the mock for SendError var sendErrorFlow = requestContext.Setup(rc => rc.SendError(It.IsAny())) .Returns(Task.FromResult(0)); if (errorCallback != null) { sendErrorFlow.Callback(errorCallback); } return requestContext; } #endregion } }