mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Isolate Shared Test Code (#252)
The goal of this make sure that test code is correctly organized to ensure that test suites aren't dependent on each other.
* UnitTests get their own project now (renaming Microsoft.SqlTools.ServiceLayer.Test to Microsoft.SqlTools.ServiceLayer.UnitTests) which is about 90% of the changes to the files.
* IntegrationTests no longer depends on UnitTests, only Test.Common
* Any shared components from TestObjects that spins up a "live" connection has been moved to IntegrationTests Utility/LiveConnectionHelper.cs
* The dictionary-based mock file stream factory has been moved to Test.Common since it is used by UnitTests and IntegrationTests
* Added a overload that doesn't take a dictionary for when we don't care about monitoring the storage (about 90% of the time)
* The RunIf* wrapper methods have been moved to Test.Common
* OwnerUri and StandardQuery constants have been moved to Test.Common Constants file
* Updating to latest SDK version available at https://www.microsoft.com/net/core#windowscmd
* Moving unit tests to unit test folder
* Changing namespaces to UnitTests
* Moving some constants and shared functionality into common project, making the UnitTests reference it
* Unit tests are working!
* Integration tests are working
* Updating automated test runs
* Fixing one last broken unit test
* Exposing internals for other projects
* Moving edit data tests to UnitTest project
* Applying refactor fixes to unit tests
* Fixing flaky test that wasn't awaiting completion
This commit is contained in:
@@ -0,0 +1,369 @@
|
||||
//
|
||||
// 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.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.Execution
|
||||
{
|
||||
public class BatchTests
|
||||
{
|
||||
[Fact]
|
||||
public void BatchCreationTest()
|
||||
{
|
||||
// If I create a new batch...
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, MemoryFileSystem.GetFileStreamFactory());
|
||||
|
||||
// Then:
|
||||
// ... The text of the batch should be stored
|
||||
Assert.NotEmpty(batch.BatchText);
|
||||
|
||||
// ... It should not have executed and no error
|
||||
Assert.False(batch.HasExecuted, "The query should not have executed.");
|
||||
Assert.False(batch.HasError);
|
||||
|
||||
// ... The results should be empty
|
||||
Assert.Empty(batch.ResultSets);
|
||||
Assert.Empty(batch.ResultSummaries);
|
||||
|
||||
// ... The start line of the batch should be 0
|
||||
Assert.Equal(0, batch.Selection.StartLine);
|
||||
|
||||
// ... It's ordinal ID should be what I set it to
|
||||
Assert.Equal(Common.Ordinal, batch.Id);
|
||||
|
||||
// ... The summary should have the same info
|
||||
Assert.Equal(Common.Ordinal, batch.Summary.Id);
|
||||
Assert.Null(batch.Summary.ResultSetSummaries);
|
||||
Assert.Equal(0, batch.Summary.Selection.StartLine);
|
||||
Assert.NotEqual(default(DateTime).ToString("o"), batch.Summary.ExecutionStart); // Should have been set at construction
|
||||
Assert.Null(batch.Summary.ExecutionEnd);
|
||||
Assert.Null(batch.Summary.ExecutionElapsed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchExecuteNoResultSets()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of callbacks being called
|
||||
int batchStartCalls = 0;
|
||||
int batchEndCalls = 0;
|
||||
int resultSetCalls = 0;
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// If I execute a query that should get no result sets
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, fileStreamFactory);
|
||||
BatchCallbackHelper(batch,
|
||||
b => batchStartCalls++,
|
||||
b => batchEndCalls++,
|
||||
m => messages.Add(m),
|
||||
r => resultSetCalls++);
|
||||
await batch.Execute(GetConnection(Common.CreateTestConnectionInfo(null, false)), CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... Callbacks should have been called the appropriate number of times
|
||||
Assert.Equal(1, batchStartCalls);
|
||||
Assert.Equal(1, batchEndCalls);
|
||||
Assert.Equal(0, resultSetCalls);
|
||||
|
||||
// ... The batch and the summary should be correctly assigned
|
||||
ValidateBatch(batch, 0, false);
|
||||
ValidateBatchSummary(batch);
|
||||
ValidateMessages(batch, 1, messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchExecuteOneResultSet()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of callbacks being called
|
||||
int batchStartCalls = 0;
|
||||
int batchEndCalls = 0;
|
||||
int resultSetCalls = 0;
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// ... Build a data set to return
|
||||
const int resultSets = 1;
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(Common.GetTestDataSet(resultSets), false);
|
||||
|
||||
// If I execute a query that should get one result set
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, fileStreamFactory);
|
||||
BatchCallbackHelper(batch,
|
||||
b => batchStartCalls++,
|
||||
b => batchEndCalls++,
|
||||
m => messages.Add(m),
|
||||
r => resultSetCalls++);
|
||||
await batch.Execute(GetConnection(ci), CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... Callbacks should have been called the appropriate number of times
|
||||
Assert.Equal(1, batchStartCalls);
|
||||
Assert.Equal(1, batchEndCalls);
|
||||
Assert.Equal(1, resultSetCalls);
|
||||
|
||||
// ... There should be exactly one result set
|
||||
ValidateBatch(batch, resultSets, false);
|
||||
ValidateBatchSummary(batch);
|
||||
ValidateMessages(batch, 1, messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchExecuteTwoResultSets()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of callbacks being called
|
||||
int batchStartCalls = 0;
|
||||
int batchEndCalls = 0;
|
||||
int resultSetCalls = 0;
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// ... Build a data set to return
|
||||
const int resultSets = 2;
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(Common.GetTestDataSet(resultSets), false);
|
||||
|
||||
// If I execute a query that should get two result sets
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, fileStreamFactory);
|
||||
BatchCallbackHelper(batch,
|
||||
b => batchStartCalls++,
|
||||
b => batchEndCalls++,
|
||||
m => messages.Add(m),
|
||||
r => resultSetCalls++);
|
||||
await batch.Execute(GetConnection(ci), CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... Callbacks should have been called the appropriate number of times
|
||||
Assert.Equal(1, batchStartCalls);
|
||||
Assert.Equal(1, batchEndCalls);
|
||||
Assert.Equal(2, resultSetCalls);
|
||||
|
||||
// ... It should have executed without error
|
||||
ValidateBatch(batch, resultSets, false);
|
||||
ValidateBatchSummary(batch);
|
||||
ValidateMessages(batch, 1, messages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchExecuteInvalidQuery()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of callbacks being called
|
||||
int batchStartCalls = 0;
|
||||
int batchEndCalls = 0;
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// If I execute a batch that is invalid
|
||||
var ci = Common.CreateTestConnectionInfo(null, true);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, fileStreamFactory);
|
||||
BatchCallbackHelper(batch,
|
||||
b => batchStartCalls++,
|
||||
b => batchEndCalls++,
|
||||
m => messages.Add(m),
|
||||
r => { throw new Exception("ResultSet callback was called when it should not have been."); });
|
||||
await batch.Execute(GetConnection(ci), CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... Callbacks should have been called the appropriate number of times
|
||||
Assert.Equal(1, batchStartCalls);
|
||||
Assert.Equal(1, batchEndCalls);
|
||||
|
||||
// ... It should have executed with error
|
||||
ValidateBatch(batch, 0, true);
|
||||
ValidateBatchSummary(batch);
|
||||
|
||||
// ... There should be one error message returned
|
||||
Assert.Equal(1, messages.Count);
|
||||
Assert.All(messages, m =>
|
||||
{
|
||||
Assert.True(m.IsError);
|
||||
Assert.Equal(batch.Id, m.BatchId);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BatchExecuteExecuted()
|
||||
{
|
||||
// Setup: Build a data set to return
|
||||
const int resultSets = 1;
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(Common.GetTestDataSet(resultSets), false);
|
||||
|
||||
// If I execute a batch
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, fileStreamFactory);
|
||||
await batch.Execute(GetConnection(ci), CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... It should have executed without error
|
||||
Assert.True(batch.HasExecuted, "The batch should have been marked executed.");
|
||||
|
||||
// If I execute it again
|
||||
// Then:
|
||||
// ... It should throw an invalid operation exception
|
||||
BatchCallbackHelper(batch,
|
||||
b => { throw new Exception("Batch start callback should not have been called"); },
|
||||
b => { throw new Exception("Batch completion callback should not have been called"); },
|
||||
m => { throw new Exception("Message callback should not have been called"); },
|
||||
null);
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(
|
||||
() => batch.Execute(GetConnection(ci), CancellationToken.None));
|
||||
|
||||
// ... The data should still be available without error
|
||||
ValidateBatch(batch, resultSets, false);
|
||||
ValidateBatchSummary(batch);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")]
|
||||
[InlineData(null)]
|
||||
public void BatchExecuteNoSql(string query)
|
||||
{
|
||||
// If:
|
||||
// ... I create a batch that has an empty query
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentException>(() => new Batch(query, Common.SubsectionDocument, Common.Ordinal, MemoryFileSystem.GetFileStreamFactory()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchNoBufferFactory()
|
||||
{
|
||||
// If:
|
||||
// ... I create a batch that has no file stream factory
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentNullException>(() => new Batch("stuff", Common.SubsectionDocument, Common.Ordinal, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BatchInvalidOrdinal()
|
||||
{
|
||||
// If:
|
||||
// ... I create a batch has has an ordinal less than 0
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => new Batch("stuff", Common.SubsectionDocument, -1, MemoryFileSystem.GetFileStreamFactory()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StatementCompletedHandlerTest()
|
||||
{
|
||||
// If:
|
||||
// ... I call the StatementCompletedHandler
|
||||
Batch batch = new Batch(Constants.StandardQuery, Common.SubsectionDocument, Common.Ordinal, MemoryFileSystem.GetFileStreamFactory());
|
||||
int messageCalls = 0;
|
||||
batch.BatchMessageSent += args =>
|
||||
{
|
||||
messageCalls++;
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// Then:
|
||||
// ... The message handler for the batch should havve been called twice
|
||||
batch.StatementCompletedHandler(null, new StatementCompletedEventArgs(1));
|
||||
Assert.True(messageCalls == 1);
|
||||
batch.StatementCompletedHandler(null, new StatementCompletedEventArgs(2));
|
||||
Assert.True(messageCalls == 2);
|
||||
}
|
||||
|
||||
private static DbConnection GetConnection(ConnectionInfo info)
|
||||
{
|
||||
return info.Factory.CreateSqlConnection(ConnectionService.BuildConnectionString(info.ConnectionDetails));
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "UnusedParameter.Local")]
|
||||
private static void ValidateBatch(Batch batch, int expectedResultSets, bool isError)
|
||||
{
|
||||
// The batch should be executed
|
||||
Assert.True(batch.HasExecuted, "The query should have been marked executed.");
|
||||
|
||||
// Result set list should never be null
|
||||
Assert.NotNull(batch.ResultSets);
|
||||
Assert.NotNull(batch.ResultSummaries);
|
||||
|
||||
// Make sure the number of result sets matches
|
||||
Assert.Equal(expectedResultSets, batch.ResultSets.Count);
|
||||
Assert.Equal(expectedResultSets, batch.ResultSummaries.Length);
|
||||
|
||||
// Make sure that the error state is set properly
|
||||
Assert.Equal(isError, batch.HasError);
|
||||
}
|
||||
|
||||
private static void ValidateBatchSummary(Batch batch)
|
||||
{
|
||||
BatchSummary batchSummary = batch.Summary;
|
||||
|
||||
Assert.NotNull(batchSummary);
|
||||
Assert.Equal(batch.Id, batchSummary.Id);
|
||||
Assert.Equal(batch.ResultSets.Count, batchSummary.ResultSetSummaries.Length);
|
||||
Assert.Equal(batch.Selection, batchSummary.Selection);
|
||||
Assert.Equal(batch.HasError, batchSummary.HasError);
|
||||
|
||||
// Something other than default date is provided for start and end times
|
||||
Assert.True(DateTime.Parse(batchSummary.ExecutionStart) > default(DateTime));
|
||||
Assert.True(DateTime.Parse(batchSummary.ExecutionEnd) > default(DateTime));
|
||||
Assert.NotNull(batchSummary.ExecutionElapsed);
|
||||
}
|
||||
|
||||
[SuppressMessage("ReSharper", "UnusedParameter.Local")]
|
||||
private static void ValidateMessages(Batch batch, int expectedMessages, IList<ResultMessage> messages)
|
||||
{
|
||||
// There should be equal number of messages to result sets
|
||||
Assert.Equal(expectedMessages, messages.Count);
|
||||
|
||||
// No messages should be errors
|
||||
// All messages must have the batch ID
|
||||
Assert.All(messages, m =>
|
||||
{
|
||||
Assert.False(m.IsError);
|
||||
Assert.Equal(batch.Id, m.BatchId);
|
||||
});
|
||||
}
|
||||
|
||||
private static void BatchCallbackHelper(Batch batch, Action<Batch> startCallback, Action<Batch> endCallback,
|
||||
Action<ResultMessage> messageCallback, Action<ResultSet> resultCallback)
|
||||
{
|
||||
// Setup the callback for batch start
|
||||
batch.BatchStart += b =>
|
||||
{
|
||||
startCallback?.Invoke(b);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// Setup the callback for batch completion
|
||||
batch.BatchCompletion += b =>
|
||||
{
|
||||
endCallback?.Invoke(b);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// Setup the callback for batch messages
|
||||
batch.BatchMessageSent += (m) =>
|
||||
{
|
||||
messageCallback?.Invoke(m);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// Setup the result set completion callback
|
||||
batch.ResultSetCompletion += r =>
|
||||
{
|
||||
resultCallback?.Invoke(r);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System.Data.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.Execution
|
||||
{
|
||||
/// <summary>
|
||||
/// DbColumnWrapper tests
|
||||
/// </summary>
|
||||
public class DbColumnWrapperTests
|
||||
{
|
||||
/// <summary>
|
||||
/// Test DbColumn derived class
|
||||
/// </summary>
|
||||
private class TestColumn : DbColumn
|
||||
{
|
||||
public TestColumn(
|
||||
string dataTypeName = null,
|
||||
int? columnSize = null,
|
||||
string columnName = null,
|
||||
string udtAssemblyQualifiedName = null)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(dataTypeName))
|
||||
{
|
||||
this.DataTypeName = dataTypeName;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.DataTypeName = "int";
|
||||
this.DataType = typeof(int);
|
||||
}
|
||||
|
||||
if (columnSize.HasValue)
|
||||
{
|
||||
this.ColumnSize = columnSize;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(columnName))
|
||||
{
|
||||
this.ColumnName = columnName;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(udtAssemblyQualifiedName))
|
||||
{
|
||||
this.UdtAssemblyQualifiedName = udtAssemblyQualifiedName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Basic data type and properties test
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void DataTypeAndPropertiesTest()
|
||||
{
|
||||
// check default constructor doesn't throw
|
||||
Assert.NotNull(new DbColumnWrapper());
|
||||
|
||||
// check various properties are either null or not null
|
||||
var column = new TestColumn();
|
||||
var wrapper = new DbColumnWrapper(column);
|
||||
Assert.NotNull(wrapper.DataTypeName);
|
||||
Assert.NotNull(wrapper.DataType);
|
||||
Assert.Null(wrapper.AllowDBNull);
|
||||
Assert.Null(wrapper.BaseCatalogName);
|
||||
Assert.Null(wrapper.BaseColumnName);
|
||||
Assert.Null(wrapper.BaseServerName);
|
||||
Assert.Null(wrapper.BaseTableName);
|
||||
Assert.Null(wrapper.ColumnOrdinal);
|
||||
Assert.Null(wrapper.ColumnSize);
|
||||
Assert.Null(wrapper.IsAliased);
|
||||
Assert.Null(wrapper.IsAutoIncrement);
|
||||
Assert.Null(wrapper.IsExpression);
|
||||
Assert.Null(wrapper.IsHidden);
|
||||
Assert.Null(wrapper.IsIdentity);
|
||||
Assert.Null(wrapper.IsKey);
|
||||
Assert.Null(wrapper.IsReadOnly);
|
||||
Assert.Null(wrapper.IsUnique);
|
||||
Assert.Null(wrapper.NumericPrecision);
|
||||
Assert.Null(wrapper.NumericScale);
|
||||
Assert.Null(wrapper.UdtAssemblyQualifiedName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// constructor test
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void DbColumnConstructorTests()
|
||||
{
|
||||
// check that various constructor parameters initial the wrapper correctly
|
||||
var w1 = new DbColumnWrapper(new TestColumn("varchar", int.MaxValue, "Microsoft SQL Server 2005 XML Showplan"));
|
||||
Assert.True(w1.IsXml);
|
||||
|
||||
var w2 = new DbColumnWrapper(new TestColumn("binary"));
|
||||
Assert.True(w2.IsBytes);
|
||||
|
||||
var w3 = new DbColumnWrapper(new TestColumn("varbinary", int.MaxValue));
|
||||
Assert.True(w3.IsBytes);
|
||||
|
||||
var w4 = new DbColumnWrapper(new TestColumn("sql_variant"));
|
||||
Assert.True(w4.IsSqlVariant);
|
||||
|
||||
var w5 = new DbColumnWrapper(new TestColumn("my_udt"));
|
||||
Assert.True(w5.IsUdt);
|
||||
|
||||
var w6 = new DbColumnWrapper(new TestColumn("my_hieracrchy", null, null, "MICROSOFT.SQLSERVER.TYPES.SQLHIERARCHYID"));
|
||||
Assert.True(w6.IsUdt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
//
|
||||
// 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.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.Execution
|
||||
{
|
||||
public class QueryTests
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void QueryCreationCorrect()
|
||||
{
|
||||
// If:
|
||||
// ... I create a query
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(Constants.StandardQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
|
||||
// Then:
|
||||
// ... I should get back two batches to execute that haven't been executed
|
||||
Assert.NotEmpty(query.QueryText);
|
||||
Assert.False(query.HasExecuted);
|
||||
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteNoQueryText()
|
||||
{
|
||||
// If:
|
||||
// ... I create a query that has a null query text
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentException>(() =>
|
||||
new Query(null, Common.CreateTestConnectionInfo(null, false), new QueryExecutionSettings(), MemoryFileSystem.GetFileStreamFactory()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteNoConnectionInfo()
|
||||
{
|
||||
// If:
|
||||
// ... I create a query that has a null connection info
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentNullException>(() => new Query("Some Query", null, new QueryExecutionSettings(), MemoryFileSystem.GetFileStreamFactory()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteNoSettings()
|
||||
{
|
||||
// If:
|
||||
// ... I create a query that has a null settings
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentNullException>(() =>
|
||||
new Query("Some query", Common.CreateTestConnectionInfo(null, false), null, MemoryFileSystem.GetFileStreamFactory()));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteNoBufferFactory()
|
||||
{
|
||||
// If:
|
||||
// ... I create a query that has a null file stream factory
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentNullException>(() =>
|
||||
new Query("Some query", Common.CreateTestConnectionInfo(null, false), new QueryExecutionSettings(), null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteSingleBatch()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of how many times the callbacks were called
|
||||
int batchStartCallbacksReceived = 0;
|
||||
int batchCompleteCallbacksReceived = 0;
|
||||
int batchMessageCallbacksReceived = 0;
|
||||
|
||||
// If:
|
||||
// ... I create a query from a single batch (without separator)
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(Constants.StandardQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
BatchCallbackHelper(query,
|
||||
b => batchStartCallbacksReceived++,
|
||||
b => batchCompleteCallbacksReceived++,
|
||||
m => batchMessageCallbacksReceived++);
|
||||
|
||||
// ... I then execute the query
|
||||
query.Execute();
|
||||
query.ExecutionTask.Wait();
|
||||
|
||||
// Then:
|
||||
// ... There should be exactly 1 batch
|
||||
Assert.NotEmpty(query.Batches);
|
||||
Assert.Equal(1, query.Batches.Length);
|
||||
|
||||
// ... The query should have completed successfully with one batch summary returned
|
||||
Assert.True(query.HasExecuted);
|
||||
Assert.NotEmpty(query.BatchSummaries);
|
||||
Assert.Equal(1, query.BatchSummaries.Length);
|
||||
|
||||
// ... The batch callbacks should have been called precisely 1 time
|
||||
Assert.Equal(1, batchStartCallbacksReceived);
|
||||
Assert.Equal(1, batchCompleteCallbacksReceived);
|
||||
Assert.Equal(1, batchMessageCallbacksReceived);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteSingleNoOpBatch()
|
||||
{
|
||||
// Setup: Keep track of all the messages received
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// If:
|
||||
// ... I create a query from a single batch that does nothing
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(Common.NoOpQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
BatchCallbackHelper(query,
|
||||
b => { throw new Exception("Batch startup callback should not have been called."); },
|
||||
b => { throw new Exception("Batch completion callback was called"); },
|
||||
m => messages.Add(m));
|
||||
|
||||
// If:
|
||||
// ... I Then execute the query
|
||||
query.Execute();
|
||||
await query.ExecutionTask;
|
||||
|
||||
// Then:
|
||||
// ... There should be no batches
|
||||
Assert.Equal(1, query.Batches.Length);
|
||||
|
||||
// ... The query shouldn't have completed successfully
|
||||
Assert.False(query.HasExecuted);
|
||||
|
||||
// ... The message callback should have been called 0 times
|
||||
Assert.Equal(0, messages.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteMultipleResultBatches()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of how many callbacks are received
|
||||
int batchStartCallbacksReceived = 0;
|
||||
int batchCompletedCallbacksReceived = 0;
|
||||
int batchMessageCallbacksReceived = 0;
|
||||
|
||||
// If:
|
||||
// ... I create a query from two batches (with separator)
|
||||
ConnectionInfo ci = Common.CreateConnectedConnectionInfo(null, false);
|
||||
|
||||
string queryText = string.Format("{0}\r\nGO\r\n{0}", Constants.StandardQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
BatchCallbackHelper(query,
|
||||
b => batchStartCallbacksReceived++,
|
||||
b => batchCompletedCallbacksReceived++,
|
||||
m => batchMessageCallbacksReceived++);
|
||||
|
||||
// ... I then execute the query
|
||||
query.Execute();
|
||||
query.ExecutionTask.Wait();
|
||||
|
||||
// Then:
|
||||
// ... I should get back a query with one batch (no op batch is not included)
|
||||
Assert.NotEmpty(query.Batches);
|
||||
Assert.Equal(2, query.Batches.Length);
|
||||
|
||||
// ... The query should have completed successfully with two batch summaries returned
|
||||
Assert.True(query.HasExecuted);
|
||||
Assert.NotEmpty(query.BatchSummaries);
|
||||
Assert.Equal(2, query.BatchSummaries.Length);
|
||||
|
||||
// ... The batch start, complete, and message callbacks should have been called precisely 2 times
|
||||
Assert.Equal(2, batchStartCallbacksReceived);
|
||||
Assert.Equal(2, batchCompletedCallbacksReceived);
|
||||
Assert.Equal(2, batchMessageCallbacksReceived);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteMultipleBatchesWithNoOp()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of how many times callbacks are called
|
||||
int batchStartCallbacksReceived = 0;
|
||||
int batchCompletionCallbacksReceived = 0;
|
||||
int batchMessageCallbacksReceived = 0;
|
||||
|
||||
// If:
|
||||
// ... I create a query from a two batches (with separator)
|
||||
ConnectionInfo ci = Common.CreateConnectedConnectionInfo(null, false);
|
||||
string queryText = string.Format("{0}\r\nGO\r\n{1}", Constants.StandardQuery, Common.NoOpQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
BatchCallbackHelper(query,
|
||||
b => batchStartCallbacksReceived++,
|
||||
b => batchCompletionCallbacksReceived++,
|
||||
m => batchMessageCallbacksReceived++);
|
||||
|
||||
// .. I then execute the query
|
||||
query.Execute();
|
||||
await query.ExecutionTask;
|
||||
|
||||
// Then:
|
||||
// ... I should get back a query with two batches
|
||||
Assert.NotEmpty(query.Batches);
|
||||
Assert.Equal(2, query.Batches.Length);
|
||||
|
||||
// ... The query should have completed successfully
|
||||
Assert.True(query.HasExecuted);
|
||||
|
||||
// ... The batch callbacks should have been called 2 times (for each no op batch)
|
||||
Assert.Equal(2, batchStartCallbacksReceived);
|
||||
Assert.Equal(2, batchCompletionCallbacksReceived);
|
||||
Assert.Equal(2, batchMessageCallbacksReceived);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteMultipleNoOpBatches()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of how many messages were sent
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// If:
|
||||
// ... I create a query from a two batches (with separator)
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
||||
string queryText = string.Format("{0}\r\nGO\r\n{1}", Common.NoOpQuery, Common.NoOpQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
BatchCallbackHelper(query,
|
||||
b => { throw new Exception("Batch start handler was called"); },
|
||||
b => { throw new Exception("Batch completed handler was called"); },
|
||||
m => messages.Add(m));
|
||||
|
||||
// .. I then execute the query
|
||||
query.Execute();
|
||||
await query.ExecutionTask;
|
||||
|
||||
// Then:
|
||||
// ... I should get back a query with no batches
|
||||
Assert.Equal(2, query.Batches.Length);
|
||||
|
||||
// ... The query shouldn't have completed successfully
|
||||
Assert.False(query.HasExecuted);
|
||||
|
||||
// ... The message callback should have been called exactly once
|
||||
Assert.Equal(0, messages.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void QueryExecuteInvalidBatch()
|
||||
{
|
||||
// Setup:
|
||||
// ... Keep track of how many times a method is called
|
||||
int batchStartCallbacksReceived = 0;
|
||||
int batchCompletionCallbacksReceived = 0;
|
||||
List<ResultMessage> messages = new List<ResultMessage>();
|
||||
|
||||
// If:
|
||||
// ... I create a query from an invalid batch
|
||||
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, true);
|
||||
ConnectionService.Instance.OwnerToConnectionMap[ci.OwnerUri] = ci;
|
||||
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
Query query = new Query(Common.InvalidQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
||||
BatchCallbackHelper(query,
|
||||
b => batchStartCallbacksReceived++,
|
||||
b => batchCompletionCallbacksReceived++,
|
||||
m => messages.Add(m));
|
||||
|
||||
// ... I then execute the query
|
||||
query.Execute();
|
||||
query.ExecutionTask.Wait();
|
||||
|
||||
// Then:
|
||||
// ... I should get back a query with one batch
|
||||
Assert.NotEmpty(query.Batches);
|
||||
Assert.Equal(1, query.Batches.Length);
|
||||
|
||||
// ... There should be an error on the batch
|
||||
Assert.True(query.HasExecuted);
|
||||
Assert.NotEmpty(query.BatchSummaries);
|
||||
Assert.Equal(1, query.BatchSummaries.Length);
|
||||
Assert.True(messages.Any(m => m.IsError));
|
||||
|
||||
// ... The batch callbacks should have been called once
|
||||
Assert.Equal(1, batchStartCallbacksReceived);
|
||||
Assert.Equal(1, batchCompletionCallbacksReceived);
|
||||
}
|
||||
|
||||
private static void BatchCallbackHelper(Query q, Action<Batch> startCallback, Action<Batch> endCallback,
|
||||
Action<ResultMessage> messageCallback)
|
||||
{
|
||||
// Setup the callback for batch start
|
||||
q.BatchStarted += b =>
|
||||
{
|
||||
startCallback?.Invoke(b);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// Setup the callback for batch completion
|
||||
q.BatchCompleted += b =>
|
||||
{
|
||||
endCallback?.Invoke(b);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// Setup the callback for batch messages
|
||||
q.BatchMessageSent += (m) =>
|
||||
{
|
||||
messageCallback?.Invoke(m);
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
//
|
||||
// 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 System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.Execution
|
||||
{
|
||||
public class ResultSetTests
|
||||
{
|
||||
[Fact]
|
||||
public void ResultCreation()
|
||||
{
|
||||
// If:
|
||||
// ... I create a new result set with a valid db data reader
|
||||
DbDataReader mockReader = GetReader(null, false, string.Empty);
|
||||
ResultSet resultSet = new ResultSet(mockReader, Common.Ordinal, Common.Ordinal, MemoryFileSystem.GetFileStreamFactory());
|
||||
|
||||
// Then:
|
||||
// ... There should not be any data read yet
|
||||
Assert.Null(resultSet.Columns);
|
||||
Assert.Equal(0, resultSet.RowCount);
|
||||
Assert.Equal(Common.Ordinal, resultSet.Id);
|
||||
|
||||
// ... The summary should include the same info
|
||||
Assert.Null(resultSet.Summary.ColumnInfo);
|
||||
Assert.Equal(0, resultSet.Summary.RowCount);
|
||||
Assert.Equal(Common.Ordinal, resultSet.Summary.Id);
|
||||
Assert.Equal(Common.Ordinal, resultSet.Summary.BatchId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResultCreationInvalidReader()
|
||||
{
|
||||
// If:
|
||||
// ... I create a new result set without a reader
|
||||
// Then:
|
||||
// ... It should throw an exception
|
||||
Assert.Throws<ArgumentNullException>(() => new ResultSet(null, Common.Ordinal, Common.Ordinal, null));
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ReadToEndSuccess()
|
||||
{
|
||||
// Setup: Create a callback for resultset completion
|
||||
ResultSetSummary resultSummaryFromCallback = null;
|
||||
ResultSet.ResultSetAsyncEventHandler callback = r =>
|
||||
{
|
||||
resultSummaryFromCallback = r.Summary;
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// If:
|
||||
// ... I create a new resultset with a valid db data reader that has data
|
||||
// ... and I read it to the end
|
||||
DbDataReader mockReader = GetReader(Common.StandardTestDataSet, false, Constants.StandardQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
ResultSet resultSet = new ResultSet(mockReader, Common.Ordinal, Common.Ordinal, fileStreamFactory);
|
||||
resultSet.ResultCompletion += callback;
|
||||
await resultSet.ReadResultToEnd(CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... The columns should be set
|
||||
// ... There should be rows to read back
|
||||
Assert.NotNull(resultSet.Columns);
|
||||
Assert.Equal(Common.StandardColumns, resultSet.Columns.Length);
|
||||
Assert.Equal(Common.StandardRows, resultSet.RowCount);
|
||||
|
||||
// ... The summary should have the same info
|
||||
Assert.NotNull(resultSet.Summary.ColumnInfo);
|
||||
Assert.Equal(Common.StandardColumns, resultSet.Summary.ColumnInfo.Length);
|
||||
Assert.Equal(Common.StandardRows, resultSet.Summary.RowCount);
|
||||
|
||||
// ... The callback for result set completion should have been fired
|
||||
Assert.NotNull(resultSummaryFromCallback);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("JSON")]
|
||||
[InlineData("XML")]
|
||||
public async Task ReadToEndForXmlJson(string forType)
|
||||
{
|
||||
// Setup:
|
||||
// ... Build a FOR XML or FOR JSON data set
|
||||
DbColumn[] columns = {new TestDbColumn(string.Format("{0}_F52E2B61-18A1-11d1-B105-00805F49916B", forType))};
|
||||
object[][] rows = Enumerable.Repeat(new object[] {"test data"}, Common.StandardRows).ToArray();
|
||||
TestResultSet[] dataSets = {new TestResultSet(columns, rows) };
|
||||
|
||||
// ... Create a callback for resultset completion
|
||||
ResultSetSummary resultSummary = null;
|
||||
ResultSet.ResultSetAsyncEventHandler callback = r =>
|
||||
{
|
||||
resultSummary = r.Summary;
|
||||
return Task.FromResult(0);
|
||||
};
|
||||
|
||||
// If:
|
||||
// ... I create a new resultset with a valid db data reader that is FOR XML/JSON
|
||||
// ... and I read it to the end
|
||||
DbDataReader mockReader = GetReader(dataSets, false, Constants.StandardQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
ResultSet resultSet = new ResultSet(mockReader, Common.Ordinal, Common.Ordinal, fileStreamFactory);
|
||||
resultSet.ResultCompletion += callback;
|
||||
await resultSet.ReadResultToEnd(CancellationToken.None);
|
||||
|
||||
// Then:
|
||||
// ... There should only be one column
|
||||
// ... There should only be one row
|
||||
// ... The result should be marked as complete
|
||||
Assert.Equal(1, resultSet.Columns.Length);
|
||||
Assert.Equal(1, resultSet.RowCount);
|
||||
|
||||
// ... The callback should have been called
|
||||
Assert.NotNull(resultSummary);
|
||||
|
||||
// If:
|
||||
// ... I attempt to read back the results
|
||||
// Then:
|
||||
// ... I should only get one row
|
||||
var subset = await resultSet.GetSubset(0, 10);
|
||||
Assert.Equal(1, subset.RowCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetSubsetWithoutExecution()
|
||||
{
|
||||
// If:
|
||||
// ... I create a new result set with a valid db data reader without executing it
|
||||
DbDataReader mockReader = GetReader(null, false, string.Empty);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
ResultSet resultSet = new ResultSet(mockReader, Common.Ordinal, Common.Ordinal, fileStreamFactory);
|
||||
|
||||
// Then:
|
||||
// ... Attempting to read a subset should fail miserably
|
||||
await Assert.ThrowsAsync<InvalidOperationException>(() => resultSet.GetSubset(0, 0));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(-1, 0)] // Too small start row
|
||||
[InlineData(20, 0)] // Too large start row
|
||||
[InlineData(0, -1)] // Negative row count
|
||||
public async Task GetSubsetInvalidParameters(int startRow, int rowCount)
|
||||
{
|
||||
// If:
|
||||
// ... I create a new result set with a valid db data reader
|
||||
// ... And execute the result
|
||||
DbDataReader mockReader = GetReader(Common.StandardTestDataSet, false, Constants.StandardQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
ResultSet resultSet = new ResultSet(mockReader, Common.Ordinal, Common.Ordinal, fileStreamFactory);
|
||||
await resultSet.ReadResultToEnd(CancellationToken.None);
|
||||
|
||||
// ... And attempt to get a subset with invalid parameters
|
||||
// Then:
|
||||
// ... It should throw an exception for an invalid parameter
|
||||
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => resultSet.GetSubset(startRow, rowCount));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0, 3)] // Standard scenario, 3 rows should come back
|
||||
[InlineData(0, 20)] // Asking for too many rows, 5 rows should come back
|
||||
[InlineData(1, 3)] // Standard scenario from non-zero start
|
||||
[InlineData(1, 20)] // Asking for too many rows at a non-zero start
|
||||
public async Task GetSubsetSuccess(int startRow, int rowCount)
|
||||
{
|
||||
// If:
|
||||
// ... I create a new result set with a valid db data reader
|
||||
// ... And execute the result set
|
||||
DbDataReader mockReader = GetReader(Common.StandardTestDataSet, false, Constants.StandardQuery);
|
||||
var fileStreamFactory = MemoryFileSystem.GetFileStreamFactory();
|
||||
ResultSet resultSet = new ResultSet(mockReader, Common.Ordinal, Common.Ordinal, fileStreamFactory);
|
||||
await resultSet.ReadResultToEnd(CancellationToken.None);
|
||||
|
||||
// ... And attempt to get a subset with valid number of rows
|
||||
ResultSetSubset subset = await resultSet.GetSubset(startRow, rowCount);
|
||||
|
||||
// Then:
|
||||
// ... There should be rows in the subset, either the number of rows or the number of
|
||||
// rows requested or the number of rows in the result set, whichever is lower
|
||||
long availableRowsFromStart = resultSet.RowCount - startRow;
|
||||
Assert.Equal(Math.Min(availableRowsFromStart, rowCount), subset.RowCount);
|
||||
|
||||
// ... The rows should have the same number of columns as the resultset
|
||||
Assert.Equal(resultSet.Columns.Length, subset.Rows[0].Length);
|
||||
}
|
||||
|
||||
private static DbDataReader GetReader(TestResultSet[] dataSet, bool throwOnRead, string query)
|
||||
{
|
||||
var info = Common.CreateTestConnectionInfo(dataSet, throwOnRead);
|
||||
var connection = info.Factory.CreateSqlConnection(ConnectionService.BuildConnectionString(info.ConnectionDetails));
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = query;
|
||||
return command.ExecuteReader();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,509 @@
|
||||
//
|
||||
// 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.Threading.Tasks;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts.ExecuteRequests;
|
||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.UnitTests.QueryExecution.Execution
|
||||
{
|
||||
public class ServiceIntegrationTests
|
||||
{
|
||||
|
||||
#region Get SQL Tests
|
||||
|
||||
[Fact]
|
||||
public void GetSqlTextFromDocumentRequestFull()
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a workspace service with a multi-line constructed query
|
||||
// ... Create a query execution service without a connection service (we won't be
|
||||
// executing queries), and the previously created workspace service
|
||||
string query = string.Format("{0}{1}GO{1}{0}", Constants.StandardQuery, Environment.NewLine);
|
||||
var workspaceService = GetDefaultWorkspaceService(query);
|
||||
var queryService = new QueryExecutionService(null, workspaceService);
|
||||
|
||||
// If: I attempt to get query text from execute document params (entire document)
|
||||
var queryParams = new ExecuteDocumentSelectionParams {OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
var queryText = queryService.GetSqlText(queryParams);
|
||||
|
||||
// Then: The text should match the constructed query
|
||||
Assert.Equal(query, queryText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSqlTextFromDocumentRequestPartial()
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a workspace service with a multi-line constructed query
|
||||
string query = string.Format("{0}{1}GO{1}{0}", Constants.StandardQuery, Environment.NewLine);
|
||||
var workspaceService = GetDefaultWorkspaceService(query);
|
||||
var queryService = new QueryExecutionService(null, workspaceService);
|
||||
|
||||
// If: I attempt to get query text from execute document params (partial document)
|
||||
var queryParams = new ExecuteDocumentSelectionParams {OwnerUri = Constants.OwnerUri, QuerySelection = Common.SubsectionDocument};
|
||||
var queryText = queryService.GetSqlText(queryParams);
|
||||
|
||||
// Then: The text should be a subset of the constructed query
|
||||
Assert.Contains(queryText, query);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSqlTextFromStringRequest()
|
||||
{
|
||||
// Setup:
|
||||
// ... Create a query execution service without a connection service or workspace
|
||||
// service (we won't execute code that uses either
|
||||
var queryService = new QueryExecutionService(null, null);
|
||||
|
||||
// If: I attempt to get query text from execute string params
|
||||
var queryParams = new ExecuteStringParams {OwnerUri = Constants.OwnerUri, Query = Constants.StandardQuery};
|
||||
var queryText = queryService.GetSqlText(queryParams);
|
||||
|
||||
// Then: The text should match the standard query
|
||||
Assert.Equal(Constants.StandardQuery, queryText);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetSqlTextFromInvalidType()
|
||||
{
|
||||
// Setup:
|
||||
// ... Mock up an implementation of ExecuteRequestParamsBase
|
||||
// ... Create a query execution service without a connection service or workspace
|
||||
// service (we won't execute code that uses either
|
||||
var mockParams = new Mock<ExecuteRequestParamsBase>().Object;
|
||||
var queryService = new QueryExecutionService(null, null);
|
||||
|
||||
// If: I attempt to get query text from the mock params
|
||||
// Then: It should throw an exception
|
||||
Assert.Throws<InvalidCastException>(() => queryService.GetSqlText(mockParams));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Inter-Service API Tests
|
||||
|
||||
[Fact]
|
||||
public async Task InterServiceExecuteNullExecuteParams()
|
||||
{
|
||||
// Setup: Create a query service
|
||||
var qes = new QueryExecutionService(null, null);
|
||||
var eventSender = new EventFlowValidator<ExecuteRequestResult>().Complete().Object;
|
||||
|
||||
|
||||
// If: I call the inter-service API to execute with a null execute params
|
||||
// Then: It should throw
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
() => qes.InterServiceExecuteQuery(null, eventSender, null, null, null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InterServiceExecuteNullEventSender()
|
||||
{
|
||||
// Setup: Create a query service, and execute params
|
||||
var qes = new QueryExecutionService(null, null);
|
||||
var executeParams = new ExecuteStringParams();
|
||||
|
||||
// If: I call the inter-service API to execute a query with a a null event sender
|
||||
// Then: It should throw
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
() => qes.InterServiceExecuteQuery(executeParams, null, null, null, null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InterServiceDisposeNullSuccessFunc()
|
||||
{
|
||||
// Setup: Create a query service and dispose params
|
||||
var qes = new QueryExecutionService(null, null);
|
||||
Func<string, Task> failureFunc = Task.FromResult;
|
||||
|
||||
// If: I call the inter-service API to dispose a query with a null success function
|
||||
// Then: It should throw
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
() => qes.InterServiceDisposeQuery(Constants.OwnerUri, null, failureFunc));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task InterServiceDisposeNullFailureFunc()
|
||||
{
|
||||
// Setup: Create a query service and dispose params
|
||||
var qes = new QueryExecutionService(null, null);
|
||||
Func<Task> successFunc = () => Task.FromResult(0);
|
||||
|
||||
// If: I call the inter-service API to dispose a query with a null success function
|
||||
// Then: It should throw
|
||||
await Assert.ThrowsAsync<ArgumentNullException>(
|
||||
() => qes.InterServiceDisposeQuery(Constants.OwnerUri, successFunc, null));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Execution Tests
|
||||
// NOTE: In order to limit test duplication, we're running the ExecuteDocumentSelection
|
||||
// version of execute query. The code paths are almost identical.
|
||||
|
||||
[Fact]
|
||||
private async Task QueryExecuteAllBatchesNoOp()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a valid query with all batches as no op
|
||||
var workspaceService = GetDefaultWorkspaceService(string.Format("{0}\r\nGO\r\n{0}", Common.NoOpQuery));
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { QuerySelection = Common.WholeDocument, OwnerUri = Constants.OwnerUri };
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddEventValidation(QueryCompleteEvent.Type, p =>
|
||||
{
|
||||
// Validate OwnerURI matches
|
||||
Assert.Equal(Constants.OwnerUri, p.OwnerUri);
|
||||
Assert.NotNull(p.BatchSummaries);
|
||||
Assert.Equal(2, p.BatchSummaries.Length);
|
||||
Assert.All(p.BatchSummaries, bs => Assert.Equal(0, bs.ResultSetSummaries.Length));
|
||||
}).Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteSingleBatchNoResultsTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a valid query with no results
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { QuerySelection = Common.WholeDocument, OwnerUri = Constants.OwnerUri};
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardQueryCompleteValidator(1)
|
||||
.Complete();
|
||||
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteSingleBatchSingleResultTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a valid query with results
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var queryService = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false,
|
||||
workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardResultSetValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardQueryCompleteValidator(1)
|
||||
.Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteSingleBatchMultipleResultTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a valid query with one batch and multiple result sets
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var dataset = new[] {Common.StandardTestResultSet, Common.StandardTestResultSet};
|
||||
var queryService = Common.GetPrimedExecutionService(dataset, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardResultSetValidator()
|
||||
.AddStandardResultSetValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardQueryCompleteValidator(1)
|
||||
.Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteMultipleBatchSingleResultTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request a to execute a valid query with multiple batches
|
||||
var workspaceService = GetDefaultWorkspaceService(string.Format("{0}\r\nGO\r\n{0}", Constants.StandardQuery));
|
||||
var queryService = Common.GetPrimedExecutionService(Common.StandardTestDataSet, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardResultSetValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardResultSetValidator()
|
||||
.AddStandardMessageValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardQueryCompleteValidator(2)
|
||||
.Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteUnconnectedUriTest()
|
||||
{
|
||||
// Given:
|
||||
// If:
|
||||
// ... I request to execute a query using a file URI that isn't connected
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var queryService = Common.GetPrimedExecutionService(null, false, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = "notConnected", QuerySelection = Common.WholeDocument };
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should be no active queries
|
||||
Assert.Empty(queryService.ActiveQueries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteInProgressTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a query
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
|
||||
// Note, we don't care about the results of the first request
|
||||
var firstRequestContext = RequestContextMocks.Create<ExecuteRequestResult>(null);
|
||||
await Common.AwaitExecution(queryService, queryParams, firstRequestContext.Object);
|
||||
|
||||
// ... And then I request another query without waiting for the first to complete
|
||||
queryService.ActiveQueries[Constants.OwnerUri].HasExecuted = false; // Simulate query hasn't finished
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should only be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteCompletedTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a query
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
|
||||
// Note, we don't care about the results of the first request
|
||||
var firstRequestContext = RequestContextMocks.Create<ExecuteRequestResult>(null);
|
||||
await Common.AwaitExecution(queryService, queryParams, firstRequestContext.Object);
|
||||
|
||||
// ... And then I request another query after waiting for the first to complete
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardQueryCompleteValidator(1)
|
||||
.Complete();
|
||||
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... All events should have been called as per their flow validator
|
||||
efv.Validate();
|
||||
|
||||
// ... There should only be one active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteMissingSelectionTest()
|
||||
{
|
||||
// Given:
|
||||
// ... A workspace with a standard query is configured
|
||||
var workspaceService = Common.GetPrimedWorkspaceService(string.Empty);
|
||||
|
||||
// If:
|
||||
// ... I request to execute a query with a missing query string
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams { OwnerUri = Constants.OwnerUri, QuerySelection = null};
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddErrorValidation<string>(Assert.NotEmpty)
|
||||
.Complete();
|
||||
await queryService.HandleExecuteRequest(queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... Am error should have been sent
|
||||
efv.Validate();
|
||||
|
||||
// ... There should not be an active query
|
||||
Assert.Empty(queryService.ActiveQueries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task QueryExecuteInvalidQueryTest()
|
||||
{
|
||||
// If:
|
||||
// ... I request to execute a query that is invalid
|
||||
var workspaceService = GetDefaultWorkspaceService(Constants.StandardQuery);
|
||||
var queryService = Common.GetPrimedExecutionService(null, true, true, workspaceService);
|
||||
var queryParams = new ExecuteDocumentSelectionParams {OwnerUri = Constants.OwnerUri, QuerySelection = Common.WholeDocument};
|
||||
|
||||
var efv = new EventFlowValidator<ExecuteRequestResult>()
|
||||
.AddStandardQueryResultValidator()
|
||||
.AddStandardBatchStartValidator()
|
||||
.AddStandardBatchCompleteValidator()
|
||||
.AddStandardQueryCompleteValidator(1)
|
||||
.Complete();
|
||||
await Common.AwaitExecution(queryService, queryParams, efv.Object);
|
||||
|
||||
// Then:
|
||||
// ... Am error should have been sent
|
||||
efv.Validate();
|
||||
|
||||
// ... There should not be an active query
|
||||
Assert.Equal(1, queryService.ActiveQueries.Count);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private static WorkspaceService<SqlToolsSettings> GetDefaultWorkspaceService(string query)
|
||||
{
|
||||
WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings = new SqlToolsSettings();
|
||||
var workspaceService = Common.GetPrimedWorkspaceService(query);
|
||||
return workspaceService;
|
||||
}
|
||||
}
|
||||
|
||||
public static class QueryExecutionEventFlowValidatorExtensions
|
||||
{
|
||||
public static EventFlowValidator<ExecuteRequestResult> AddStandardQueryResultValidator(
|
||||
this EventFlowValidator<ExecuteRequestResult> efv)
|
||||
{
|
||||
// We just need to makes sure we get a result back, there's no params to validate
|
||||
return efv.AddResultValidation(Assert.NotNull);
|
||||
}
|
||||
|
||||
public static EventFlowValidator<TRequestContext> AddStandardBatchStartValidator<TRequestContext>(
|
||||
this EventFlowValidator<TRequestContext> efv)
|
||||
{
|
||||
return efv.AddEventValidation(BatchStartEvent.Type, p =>
|
||||
{
|
||||
// Validate OwnerURI and batch summary is returned
|
||||
Assert.Equal(Constants.OwnerUri, p.OwnerUri);
|
||||
Assert.NotNull(p.BatchSummary);
|
||||
});
|
||||
}
|
||||
|
||||
public static EventFlowValidator<TRequestContext> AddStandardBatchCompleteValidator<TRequestContext>(
|
||||
this EventFlowValidator<TRequestContext> efv)
|
||||
{
|
||||
return efv.AddEventValidation(BatchCompleteEvent.Type, p =>
|
||||
{
|
||||
// Validate OwnerURI and result summary are returned
|
||||
Assert.Equal(Constants.OwnerUri, p.OwnerUri);
|
||||
Assert.NotNull(p.BatchSummary);
|
||||
});
|
||||
}
|
||||
|
||||
public static EventFlowValidator<TRequestContext> AddStandardMessageValidator<TRequestContext>(
|
||||
this EventFlowValidator<TRequestContext> efv)
|
||||
{
|
||||
return efv.AddEventValidation(MessageEvent.Type, p =>
|
||||
{
|
||||
// Validate OwnerURI and message are returned
|
||||
Assert.Equal(Constants.OwnerUri, p.OwnerUri);
|
||||
Assert.NotNull(p.Message);
|
||||
});
|
||||
}
|
||||
|
||||
public static EventFlowValidator<TRequestContext> AddStandardResultSetValidator<TRequestContext>(
|
||||
this EventFlowValidator<TRequestContext> efv)
|
||||
{
|
||||
return efv.AddEventValidation(ResultSetCompleteEvent.Type, p =>
|
||||
{
|
||||
// Validate OwnerURI and summary are returned
|
||||
Assert.Equal(Constants.OwnerUri, p.OwnerUri);
|
||||
Assert.NotNull(p.ResultSetSummary);
|
||||
});
|
||||
}
|
||||
|
||||
public static EventFlowValidator<TRequestContext> AddStandardQueryCompleteValidator<TRequestContext>(
|
||||
this EventFlowValidator<TRequestContext> efv, int expectedBatches)
|
||||
{
|
||||
return efv.AddEventValidation(QueryCompleteEvent.Type, p =>
|
||||
{
|
||||
Assert.Equal(Constants.OwnerUri, p.OwnerUri);
|
||||
Assert.NotNull(p.BatchSummaries);
|
||||
Assert.Equal(expectedBatches, p.BatchSummaries.Length);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user