Adding unit tests to the updated message mechanism

This commit is contained in:
Benjamin Russell
2016-08-11 16:39:33 -07:00
parent 1be4daf41d
commit 9890e828bd
3 changed files with 34 additions and 14 deletions

View File

@@ -5,7 +5,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Data.Common;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Hosting; using Microsoft.SqlTools.ServiceLayer.Hosting;

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Data.SqlClient;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.SqlTools.ServiceLayer.Connection; using Microsoft.SqlTools.ServiceLayer.Connection;
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts; using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
@@ -62,7 +63,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// Setup the expected behavior // Setup the expected behavior
if (throwOnRead) if (throwOnRead)
{ {
commandMockSetup.Throws(new Mock<DbException>().Object); var mockException = new Mock<DbException>();
mockException.SetupGet(dbe => dbe.Message).Returns("Message");
commandMockSetup.Throws(mockException.Object);
} }
else else
{ {

View File

@@ -36,8 +36,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
query.Execute().Wait(); query.Execute().Wait();
// Then: // Then:
// ... It should have executed // ... It should have executed without error
Assert.True(query.HasExecuted, "The query should have been marked executed."); Assert.True(query.HasExecuted, "The query should have been marked executed.");
Assert.False(query.HasError);
// ... The results should be empty // ... The results should be empty
Assert.Empty(query.ResultSets); Assert.Empty(query.ResultSets);
@@ -46,6 +47,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// ... The results should not be null // ... The results should not be null
Assert.NotNull(query.ResultSets); Assert.NotNull(query.ResultSets);
Assert.NotNull(query.ResultSummary); Assert.NotNull(query.ResultSummary);
// ... There should be a message for how many rows were affected
Assert.Equal(1, query.ResultMessages.Count);
} }
[Fact] [Fact]
@@ -61,8 +65,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
query.Execute().Wait(); query.Execute().Wait();
// Then: // Then:
// ... It should have executed // ... It should have executed without error
Assert.True(query.HasExecuted, "The query should have been marked executed."); Assert.True(query.HasExecuted, "The query should have been marked executed.");
Assert.False(query.HasError);
// ... There should be exactly one result set // ... There should be exactly one result set
Assert.Equal(resultSets, query.ResultSets.Count); Assert.Equal(resultSets, query.ResultSets.Count);
@@ -82,6 +87,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// ... Inside the result summary, there should be 5 rows // ... Inside the result summary, there should be 5 rows
Assert.Equal(rows, query.ResultSummary[0].RowCount); Assert.Equal(rows, query.ResultSummary[0].RowCount);
// ... There should be a message for how many rows were affected
Assert.Equal(resultSets, query.ResultMessages.Count);
} }
[Fact] [Fact]
@@ -98,8 +106,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
query.Execute().Wait(); query.Execute().Wait();
// Then: // Then:
// ... It should have executed // ... It should have executed without error
Assert.True(query.HasExecuted, "The query should have been marked executed."); Assert.True(query.HasExecuted, "The query should have been marked executed.");
Assert.False(query.HasError);
// ... There should be exactly two result sets // ... There should be exactly two result sets
Assert.Equal(resultSets, query.ResultSets.Count); Assert.Equal(resultSets, query.ResultSets.Count);
@@ -125,6 +134,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// ... Inside each result summary, there should be 5 rows // ... Inside each result summary, there should be 5 rows
Assert.Equal(rows, rs.RowCount); Assert.Equal(rows, rs.RowCount);
} }
// ... There should be a message for how many rows were affected
Assert.Equal(resultSets, query.ResultMessages.Count);
} }
[Fact] [Fact]
@@ -134,10 +146,15 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// If I execute a query that is invalid // If I execute a query that is invalid
Query query = new Query("Invalid query", ci); Query query = new Query("Invalid query", ci);
query.Execute().Wait();
// Then: // Then:
// ... It should throw an exception // ... It should have executed with error
Exception e = Assert.Throws<AggregateException>(() => query.Execute().Wait()); Assert.True(query.HasExecuted);
Assert.True(query.HasError);
// ... There should be plenty of messages for the eror
Assert.NotEmpty(query.ResultMessages);
} }
[Fact] [Fact]
@@ -150,8 +167,9 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
query.Execute().Wait(); query.Execute().Wait();
// Then: // Then:
// ... It should have executed // ... It should have executed without error
Assert.True(query.HasExecuted, "The query should have been marked executed."); Assert.True(query.HasExecuted, "The query should have been marked executed.");
Assert.False(query.HasError);
// If I execute it again // If I execute it again
// Then: // Then:
@@ -160,7 +178,8 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
Assert.Equal(1, ae.InnerExceptions.Count); Assert.Equal(1, ae.InnerExceptions.Count);
Assert.IsType<InvalidOperationException>(ae.InnerExceptions[0]); Assert.IsType<InvalidOperationException>(ae.InnerExceptions[0]);
// ... The data should still be available // ... The data should still be available without error
Assert.False(query.HasError);
Assert.True(query.HasExecuted, "The query should still be marked executed."); Assert.True(query.HasExecuted, "The query should still be marked executed.");
Assert.NotEmpty(query.ResultSets); Assert.NotEmpty(query.ResultSets);
Assert.NotEmpty(query.ResultSummary); Assert.NotEmpty(query.ResultSummary);
@@ -208,12 +227,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// Then: // Then:
// ... No Errors should have been sent // ... No Errors should have been sent
// ... A successful result should have been sent with no messages // ... A successful result should have been sent with messages
// ... A completion event should have been fired with empty results // ... A completion event should have been fired with empty results
// ... There should be one active query // ... There should be one active query
VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Once(), Times.Never()); VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Once(), Times.Never());
Assert.Null(result.Messages); Assert.Null(result.Messages);
Assert.Empty(completeParams.Messages); Assert.NotEmpty(completeParams.Messages);
Assert.Empty(completeParams.ResultSetSummaries); Assert.Empty(completeParams.ResultSetSummaries);
Assert.False(completeParams.HasError); Assert.False(completeParams.HasError);
Assert.Equal(1, queryService.ActiveQueries.Count); Assert.Equal(1, queryService.ActiveQueries.Count);
@@ -234,12 +253,12 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
// Then: // Then:
// ... No errors should have been sent // ... No errors should have been sent
// ... A successful result should have been sent with no messages // ... A successful result should have been sent with messages
// ... A completion event should have been fired with one result // ... A completion event should have been fired with one result
// ... There should be one active query // ... There should be one active query
VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Once(), Times.Never()); VerifyQueryExecuteCallCount(requestContext, Times.Once(), Times.Once(), Times.Never());
Assert.Null(result.Messages); Assert.Null(result.Messages);
Assert.Empty(completeParams.Messages); Assert.NotEmpty(completeParams.Messages);
Assert.NotEmpty(completeParams.ResultSetSummaries); Assert.NotEmpty(completeParams.ResultSetSummaries);
Assert.False(completeParams.HasError); Assert.False(completeParams.HasError);
Assert.Equal(1, queryService.ActiveQueries.Count); Assert.Equal(1, queryService.ActiveQueries.Count);
@@ -327,7 +346,6 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.QueryExecution
[Theory] [Theory]
[InlineData("")] [InlineData("")]
[InlineData(" ")]
[InlineData(null)] [InlineData(null)]
public void QueryExecuteMissingQueryTest(string query) public void QueryExecuteMissingQueryTest(string query)
{ {