mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:35:43 -05:00
This is a reworking of the unit tests to permit us to better test events from the service host. This new Event Flow Validator class allows creating a chain of events that can then be validated after execution of the request. Each event can have its own custom validation logic for verifying that the object sent via the service host is correct. It also allows us to validate that the order of events are correct. The big drawback is that (at this time) the validator cannot support asynchronous events or non-determinant ordering of events. We don't need this for the query execution functionality despite messages being sent asynchronously because async messages aren't sent during unit tests (due to the db message event only being present on SqlDbConnection classes). If the need arises to do async or out of order event validation, then I have some ideas for how we can do that. * Applying the event flow validator to the query execution service integration tests * Undoing changes to events that were included in cherry-picked commit * Cleaning up event flow validation to query execution * Add efv to cancel tests * Adding efv to dispose tests * Adding efv to subset tests * Adding efv to SaveResults tests * Copyright
225 lines
10 KiB
C#
225 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.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution.Contracts;
|
|
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
|
|
{
|
|
public class SubsetTests
|
|
{
|
|
#region ResultSet Class Tests
|
|
|
|
[Theory]
|
|
[InlineData(0, 2)]
|
|
[InlineData(0, 20)]
|
|
[InlineData(1, 2)]
|
|
public void ResultSetValidTest(int startRow, int rowCount)
|
|
{
|
|
// Setup:
|
|
// ... I have a batch that has been executed
|
|
Batch b = Common.GetBasicExecutedBatch();
|
|
|
|
// If:
|
|
// ... I have a result set and I ask for a subset with valid arguments
|
|
ResultSet rs = b.ResultSets.First();
|
|
ResultSetSubset subset = rs.GetSubset(startRow, rowCount).Result;
|
|
|
|
// Then:
|
|
// ... I should get the requested number of rows back
|
|
Assert.Equal(Math.Min(rowCount, Common.StandardTestData.Length), subset.RowCount);
|
|
Assert.Equal(Math.Min(rowCount, Common.StandardTestData.Length), subset.Rows.Length);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-1, 2)] // Invalid start index, too low
|
|
[InlineData(10, 2)] // Invalid start index, too high
|
|
[InlineData(0, -1)] // Invalid row count, too low
|
|
[InlineData(0, 0)] // Invalid row count, zero
|
|
public void ResultSetInvalidParmsTest(int rowStartIndex, int rowCount)
|
|
{
|
|
// If:
|
|
// I have an executed batch with a resultset in it and request invalid result set from it
|
|
Batch b = Common.GetBasicExecutedBatch();
|
|
ResultSet rs = b.ResultSets.First();
|
|
|
|
// Then:
|
|
// ... It should throw an exception
|
|
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => rs.GetSubset(rowStartIndex, rowCount)).Wait();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ResultSetNotReadTest()
|
|
{
|
|
// If:
|
|
// ... I have a resultset that hasn't been executed and I request a valid result set from it
|
|
// Then:
|
|
// ... It should throw an exception for having not been read
|
|
ResultSet rs = new ResultSet(new TestDbDataReader(null), Common.Ordinal, Common.Ordinal, Common.GetFileStreamFactory(new Dictionary<string, byte[]>()));
|
|
await Assert.ThrowsAsync<InvalidOperationException>(() => rs.GetSubset(0, 1));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Batch Class Tests
|
|
|
|
[Theory]
|
|
[InlineData(2)]
|
|
[InlineData(20)]
|
|
public void BatchSubsetValidTest(int rowCount)
|
|
{
|
|
// If I have an executed batch
|
|
Batch b = Common.GetBasicExecutedBatch();
|
|
|
|
// ... And I ask for a subset with valid arguments
|
|
ResultSetSubset subset = b.GetSubset(0, 0, rowCount).Result;
|
|
|
|
// Then:
|
|
// I should get the requested number of rows
|
|
Assert.Equal(Math.Min(rowCount, Common.StandardTestData.Length), subset.RowCount);
|
|
Assert.Equal(Math.Min(rowCount, Common.StandardTestData.Length), subset.Rows.Length);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(-1)] // Invalid result set, too low
|
|
[InlineData(2)] // Invalid result set, too high
|
|
public void BatchSubsetInvalidParamsTest(int resultSetIndex)
|
|
{
|
|
// If I have an executed batch
|
|
Batch b = Common.GetBasicExecutedBatch();
|
|
|
|
// ... And I ask for a subset with an invalid result set index
|
|
// Then:
|
|
// ... It should throw an exception
|
|
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => b.GetSubset(resultSetIndex, 0, 2)).Wait();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Query Class Tests
|
|
|
|
[Theory]
|
|
[InlineData(-1)] // Invalid batch, too low
|
|
[InlineData(2)] // Invalid batch, too high
|
|
public void QuerySubsetInvalidParamsTest(int batchIndex)
|
|
{
|
|
// If I have an executed query
|
|
Query q = Common.GetBasicExecutedQuery();
|
|
|
|
// ... And I ask for a subset with an invalid result set index
|
|
// Then:
|
|
// ... It should throw an exception
|
|
Assert.ThrowsAsync<ArgumentOutOfRangeException>(() => q.GetSubset(batchIndex, 0, 0, 1)).Wait();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Service Intergration Tests
|
|
|
|
[Fact]
|
|
public async Task SubsetServiceValidTest()
|
|
{
|
|
// If:
|
|
// ... I have a query that has results (doesn't matter what)
|
|
var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
|
|
var queryService = Common.GetPrimedExecutionService(new[] {Common.StandardTestData}, true, false, workspaceService);
|
|
var executeParams = new QueryExecuteParams {QuerySelection = null, OwnerUri = Common.OwnerUri};
|
|
var executeRequest = RequestContextMocks.Create<QueryExecuteResult>(null);
|
|
await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);
|
|
await queryService.ActiveQueries[Common.OwnerUri].ExecutionTask;
|
|
|
|
// ... And I then ask for a valid set of results from it
|
|
var subsetParams = new QueryExecuteSubsetParams { OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
|
var subsetRequest = new EventFlowValidator<QueryExecuteSubsetResult>()
|
|
.AddResultValidation(r =>
|
|
{
|
|
// Then: Messages should be null and subset should not be null
|
|
Assert.Null(r.Message);
|
|
Assert.NotNull(r.ResultSubset);
|
|
}).Complete();
|
|
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
|
subsetRequest.Validate();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubsetServiceMissingQueryTest()
|
|
{
|
|
// If:
|
|
// ... I ask for a set of results for a file that hasn't executed a query
|
|
var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
|
|
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
|
var subsetParams = new QueryExecuteSubsetParams { OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
|
var subsetRequest = new EventFlowValidator<QueryExecuteSubsetResult>()
|
|
.AddResultValidation(r =>
|
|
{
|
|
// Then: Messages should not be null and the subset should be null
|
|
Assert.NotNull(r.Message);
|
|
Assert.Null(r.ResultSubset);
|
|
}).Complete();
|
|
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
|
subsetRequest.Validate();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubsetServiceUnexecutedQueryTest()
|
|
{
|
|
// If:
|
|
// ... I have a query that hasn't finished executing (doesn't matter what)
|
|
var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
|
|
var queryService = Common.GetPrimedExecutionService(new[] { Common.StandardTestData }, true, false, workspaceService);
|
|
var executeParams = new QueryExecuteParams { QuerySelection = null, OwnerUri = Common.OwnerUri };
|
|
var executeRequest = RequestContextMocks.Create<QueryExecuteResult>(null);
|
|
await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);
|
|
await queryService.ActiveQueries[Common.OwnerUri].ExecutionTask;
|
|
queryService.ActiveQueries[Common.OwnerUri].Batches[0].ResultSets[0].hasBeenRead = false;
|
|
|
|
// ... And I then ask for a valid set of results from it
|
|
var subsetParams = new QueryExecuteSubsetParams { OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
|
var subsetRequest = new EventFlowValidator<QueryExecuteSubsetResult>()
|
|
.AddResultValidation(r =>
|
|
{
|
|
// Then: There should not be a subset and message should not be null
|
|
Assert.NotNull(r.Message);
|
|
Assert.Null(r.ResultSubset);
|
|
}).Complete();
|
|
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
|
subsetRequest.Validate();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SubsetServiceOutOfRangeSubsetTest()
|
|
{
|
|
// If:
|
|
// ... I have a query that doesn't have any result sets
|
|
var workspaceService = Common.GetPrimedWorkspaceService(Common.StandardQuery);
|
|
var queryService = Common.GetPrimedExecutionService(null, true, false, workspaceService);
|
|
var executeParams = new QueryExecuteParams { QuerySelection = null, OwnerUri = Common.OwnerUri };
|
|
var executeRequest = RequestContextMocks.Create<QueryExecuteResult>(null);
|
|
await queryService.HandleExecuteRequest(executeParams, executeRequest.Object);
|
|
await queryService.ActiveQueries[Common.OwnerUri].ExecutionTask;
|
|
|
|
// ... And I then ask for a set of results from it
|
|
var subsetParams = new QueryExecuteSubsetParams { OwnerUri = Common.OwnerUri, RowsCount = 1, ResultSetIndex = 0, RowsStartIndex = 0 };
|
|
var subsetRequest = new EventFlowValidator<QueryExecuteSubsetResult>()
|
|
.AddResultValidation(r =>
|
|
{
|
|
// Then: There should be an error message and no subset
|
|
Assert.NotNull(r.Message);
|
|
Assert.Null(r.ResultSubset);
|
|
}).Complete();
|
|
await queryService.HandleResultSubsetRequest(subsetParams, subsetRequest.Object);
|
|
subsetRequest.Validate();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|