mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 09:59:48 -05:00
This is a fairly large set of changes to the unit tests that help isolate the effectiveness of the unit tests. * Unit tests for query execution have been split into separate files for different classes. * Unit tests have been added for the ResultSet class which previously did not have tests * The InMemoryStreamWrapper has been improved to share memory, creating a simulated filesystem * Creating a mock ConnectionService to decrease noisy exceptions and prevent "row stealing". Unfortunately this lowers code coverage. However, since the tests that touched the connection service were not really testing it, this helps keep us honest. But it will require adding more unit tests for connection service. * Standardizing the await mechanism for query execution * Cleaning up the mechanism for getting WorkspaceService mocks and mock FileStreamFactories * Refactor the query execution tests into their own files * Removing tests from ExecuteTests.cs that were moved to separate files * Adding tests for ResultSet class * Adding test for the FOR XML/JSON component of the resultset class * Setting up shared storage between file stream readers/writers * Standardizing on Workspace mocking, awaiting execution completion * Adding comment for ResultSet class
209 lines
8.2 KiB
C#
209 lines
8.2 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 Microsoft.SqlTools.ServiceLayer.Connection;
|
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
|
using Xunit;
|
|
|
|
namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution.Execution
|
|
{
|
|
public class QueryTests
|
|
{
|
|
|
|
[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(), Common.GetFileStreamFactory(null)));
|
|
}
|
|
|
|
[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(), Common.GetFileStreamFactory(null)));
|
|
}
|
|
|
|
[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, Common.GetFileStreamFactory(null)));
|
|
}
|
|
|
|
[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()
|
|
{
|
|
// If:
|
|
// ... I create a query from a single batch (without separator)
|
|
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
|
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
|
|
Query query = new Query(Common.StandardQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
|
|
|
// Then:
|
|
// ... I should get a single batch to execute that hasn't been executed
|
|
Assert.NotEmpty(query.QueryText);
|
|
Assert.NotEmpty(query.Batches);
|
|
Assert.Equal(1, query.Batches.Length);
|
|
Assert.False(query.HasExecuted);
|
|
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
|
|
|
|
// If:
|
|
// ... I then execute the query
|
|
query.Execute();
|
|
query.ExecutionTask.Wait();
|
|
|
|
// Then:
|
|
// ... 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);
|
|
}
|
|
|
|
[Fact]
|
|
public void QueryExecuteNoOpBatch()
|
|
{
|
|
// If:
|
|
// ... I create a query from a single batch that does nothing
|
|
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
|
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
|
|
Query query = new Query(Common.NoOpQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
|
|
|
// Then:
|
|
// ... I should get no batches back
|
|
Assert.NotEmpty(query.QueryText);
|
|
Assert.Empty(query.Batches);
|
|
Assert.False(query.HasExecuted);
|
|
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
|
|
|
|
// If:
|
|
// ... I Then execute the query
|
|
query.Execute();
|
|
query.ExecutionTask.Wait();
|
|
|
|
// Then:
|
|
// ... The query should have completed successfully with no batch summaries returned
|
|
Assert.True(query.HasExecuted);
|
|
Assert.Empty(query.BatchSummaries);
|
|
}
|
|
|
|
[Fact]
|
|
public void QueryExecuteMultipleBatches()
|
|
{
|
|
// If:
|
|
// ... I create a query from two batches (with separator)
|
|
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, false);
|
|
string queryText = string.Format("{0}\r\nGO\r\n{0}", Common.StandardQuery);
|
|
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
|
|
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
|
|
|
|
// Then:
|
|
// ... I should get back two batches to execute that haven't been executed
|
|
Assert.NotEmpty(query.QueryText);
|
|
Assert.NotEmpty(query.Batches);
|
|
Assert.Equal(2, query.Batches.Length);
|
|
Assert.False(query.HasExecuted);
|
|
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
|
|
|
|
// If:
|
|
// ... I then execute the query
|
|
query.Execute();
|
|
query.ExecutionTask.Wait();
|
|
|
|
// Then:
|
|
// ... 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);
|
|
}
|
|
|
|
[Fact]
|
|
public void QueryExecuteMultipleBatchesWithNoOp()
|
|
{
|
|
// 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.StandardQuery, Common.NoOpQuery);
|
|
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
|
|
Query query = new Query(queryText, ci, new QueryExecutionSettings(), fileStreamFactory);
|
|
|
|
// Then:
|
|
// ... I should get back one batch to execute that hasn't been executed
|
|
Assert.NotEmpty(query.QueryText);
|
|
Assert.NotEmpty(query.Batches);
|
|
Assert.Equal(1, query.Batches.Length);
|
|
Assert.False(query.HasExecuted);
|
|
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
|
|
|
|
// If:
|
|
// .. I then execute the query
|
|
query.Execute();
|
|
query.ExecutionTask.Wait();
|
|
|
|
// ... 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);
|
|
}
|
|
|
|
[Fact]
|
|
public void QueryExecuteInvalidBatch()
|
|
{
|
|
// If:
|
|
// ... I create a query from an invalid batch
|
|
ConnectionInfo ci = Common.CreateTestConnectionInfo(null, true);
|
|
var fileStreamFactory = Common.GetFileStreamFactory(new Dictionary<string, byte[]>());
|
|
Query query = new Query(Common.InvalidQuery, ci, new QueryExecutionSettings(), fileStreamFactory);
|
|
|
|
// Then:
|
|
// ... I should get back a query with one batch not executed
|
|
Assert.NotEmpty(query.QueryText);
|
|
Assert.NotEmpty(query.Batches);
|
|
Assert.Equal(1, query.Batches.Length);
|
|
Assert.False(query.HasExecuted);
|
|
Assert.Throws<InvalidOperationException>(() => query.BatchSummaries);
|
|
|
|
// If:
|
|
// ... I then execute the query
|
|
query.Execute();
|
|
query.ExecutionTask.Wait();
|
|
|
|
// Then:
|
|
// ... There should be an error on the batch
|
|
Assert.True(query.HasExecuted);
|
|
Assert.NotEmpty(query.BatchSummaries);
|
|
Assert.Equal(1, query.BatchSummaries.Length);
|
|
Assert.True(query.BatchSummaries[0].HasError);
|
|
Assert.NotEmpty(query.BatchSummaries[0].Messages);
|
|
}
|
|
|
|
}
|
|
}
|