Convert most tools service tests to nunit (#1037)

* Remove xunit dependency from testdriver

* swap expected/actual as needed

* Convert Test.Common to nunit

* port hosting unit tests to nunit

* port batchparser integration tests to nunit

* port testdriver.tests to nunit

* fix target to copy dependency

* port servicelayer unittests to nunit

* more unit test fixes

* port integration tests to nunit

* fix test method type

* try using latest windows build for PRs

* reduce test memory use
This commit is contained in:
David Shiflet
2020-08-05 13:43:14 -04:00
committed by GitHub
parent bf4911795f
commit 839acf67cd
205 changed files with 4146 additions and 4329 deletions

View File

@@ -7,10 +7,11 @@ using System;
using System.IO;
using Microsoft.SqlTools.ServiceLayer.BatchParser;
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
using Xunit;
using NUnit.Framework;
namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
{
[TestFixture]
public class BatchParserSqlCmdTests : IDisposable
{
private BatchParserSqlCmd bpcmd;
@@ -41,62 +42,62 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
}
[Fact]
[Test]
public void CheckSetVariable()
{
Assert.Equal(bpcmd.InternalVariables.Count, 3);
Assert.AreEqual(3, bpcmd.InternalVariables.Count);
bpcmd.SetVariable(testPOS, "variable4", "test4");
bpcmd.SetVariable(testPOS, "variable5", "test5");
bpcmd.SetVariable(testPOS, "variable6", "test6");
Assert.Equal(bpcmd.InternalVariables.Count, 6);
Assert.AreEqual(6, bpcmd.InternalVariables.Count);
}
[Fact]
[Test]
public void CheckSetNullValueVariable()
{
Assert.Equal(bpcmd.InternalVariables.Count, 3);
Assert.AreEqual(3, bpcmd.InternalVariables.Count);
bpcmd.SetVariable(testPOS, "variable4", "test4");
Assert.Equal(bpcmd.InternalVariables.Count, 4);
Assert.AreEqual(4, bpcmd.InternalVariables.Count);
bpcmd.SetVariable(testPOS, "variable4", null);
Assert.Equal(bpcmd.InternalVariables.Count, 3);
Assert.AreEqual(3, bpcmd.InternalVariables.Count);
}
[Fact]
[Test]
public void CheckGetVariable()
{
string value = bpcmd.GetVariable(testPOS, "variable1");
Assert.Equal("test1", value);
Assert.AreEqual("test1", value);
value = bpcmd.GetVariable(testPOS, "variable2");
Assert.Equal("test2", value);
Assert.AreEqual("test2", value);
value = bpcmd.GetVariable(testPOS, "variable3");
Assert.Equal("test3", value);
Assert.AreEqual("test3", value);
}
[Fact]
[Test]
public void CheckGetNullVariable()
{
Assert.Null(bpcmd.GetVariable(testPOS, "variable6"));
}
[Fact]
[Test]
public void CheckInclude()
{
TextReader textReader = null;
string outString = "out";
var result = bpcmd.Include(null, out textReader, out outString);
Assert.Equal(result, BatchParserAction.Abort);
Assert.AreEqual(BatchParserAction.Abort, result);
}
[Fact]
[Test]
public void CheckOnError()
{
var errorActionChanged = bpcmd.ErrorActionChanged;
var action = new OnErrorAction();
var result = bpcmd.OnError(null, action);
Assert.Equal(result, BatchParserAction.Continue);
Assert.AreEqual(BatchParserAction.Continue, result);
}
[Fact]
[Test]
public void CheckConnectionChangedDelegate()
{
var initial = bpcmd.ConnectionChanged;
@@ -104,7 +105,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
Assert.Null(bpcmd.ConnectionChanged);
}
[Fact]
[Test]
public void CheckVariableSubstitutionDisabled()
{
bpcmd.DisableVariableSubstitution();

View File

@@ -14,12 +14,13 @@ using Microsoft.Data.SqlClient;
using System.Globalization;
using System.IO;
using System.Text;
using Xunit;
using NUnit.Framework;
using Microsoft.SqlTools.ServiceLayer.Connection;
using System.Threading.Tasks;
namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
{
[TestFixture]
public class BatchParserTests : BaselinedTest
{
public BatchParserTests()
@@ -34,7 +35,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
TestInitialize();
}
[Fact]
[Test]
public void VerifyThrowOnUnresolvedVariable()
{
string script = "print '$(NotDefined)'";
@@ -59,7 +60,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
/// Variable parameter in powershell: Specifies, as a string array, a sqlcmd scripting variable
/// for use in the sqlcmd script, and sets a value for the variable.
/// </summary>
[Fact]
[Test]
public void VerifyVariableResolverUsingVaribleParameter()
{
string query = @" Invoke-Sqlcmd -Query ""SELECT `$(calcOne)"" -Variable ""calcOne = 10 + 20"" ";
@@ -79,7 +80,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
// Verify the starting identifier of Both parameter and variable are same.
[Fact]
[Test]
public void VerifyVariableResolverIsStartIdentifierChar()
{
// instead of using variable calcOne, I purposely used In-variable 0alcOne
@@ -100,7 +101,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
// Verify all the characters inside variable are valid Identifier.
[Fact]
[Test]
public void VerifyVariableResolverIsIdentifierChar()
{
// instead of using variable calcOne, I purposely used In-variable 0alcOne
@@ -121,7 +122,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
// Verify the execution by passing long value , Except a exception.
[Fact]
[Test]
public void VerifyInvalidNumber()
{
string query = @" SELECT 1+1
@@ -144,7 +145,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
// Verify the Batch execution is executed successfully.
[Fact]
[Test]
public void VerifyExecute()
{
Batch batch = new Batch(sqlText: "SELECT 1+1", isResultExpected: true, execTimeout: 15);
@@ -152,13 +153,13 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo))
{
var executionResult = batch.Execute(sqlConn, ShowPlanType.AllShowPlan);
Assert.Equal<ScriptExecutionResult>(ScriptExecutionResult.Success, executionResult);
Assert.AreEqual(ScriptExecutionResult.Success, executionResult);
}
}
// Verify the exeception is handled by passing invalid keyword.
[Fact]
[Test]
public void VerifyHandleExceptionMessage()
{
Batch batch = new Batch(sqlText: "SEL@ECT 1+1", isResultExpected: true, execTimeout: 15);
@@ -169,11 +170,11 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
ScriptExecutionResult finalResult = (batch.RowsAffected > 0) ? ScriptExecutionResult.Success : ScriptExecutionResult.Failure;
Assert.Equal<ScriptExecutionResult>(finalResult, ScriptExecutionResult.Failure);
Assert.AreEqual(ScriptExecutionResult.Failure, finalResult);
}
// Verify the passing query has valid text.
[Fact]
[Test]
public void VerifyHasValidText()
{
Batch batch = new Batch(sqlText: null, isResultExpected: true, execTimeout: 15);
@@ -185,11 +186,11 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
}
finalResult = (batch.RowsAffected > 0) ? ScriptExecutionResult.Success : ScriptExecutionResult.Failure;
Assert.Equal<ScriptExecutionResult>(finalResult, ScriptExecutionResult.Failure);
Assert.AreEqual(ScriptExecutionResult.Failure, finalResult);
}
// Verify the cancel functionality is working fine.
[Fact]
[Test]
public void VerifyCancel()
{
ScriptExecutionResult result = ScriptExecutionResult.All;
@@ -200,14 +201,14 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
batch.Cancel();
result = batch.Execute(sqlConn, ShowPlanType.AllShowPlan);
}
Assert.Equal<ScriptExecutionResult>(result, ScriptExecutionResult.Cancel);
Assert.AreEqual(ScriptExecutionResult.Cancel, result);
}
//
/// <summary>
/// Verify whether lexer can consume token for SqlCmd variable
/// </summary>
[Fact]
[Test]
public void VerifyLexerSetState()
{
try
@@ -229,7 +230,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
// This test case is to verify that, Powershell's Invoke-SqlCmd handles ":!!if" in an inconsistent way
// Inconsistent way means, instead of throwing an exception as "Command Execute is not supported." it was throwing "Incorrect syntax near ':'."
[Fact]
[Test]
public void VerifySqlCmdExecute()
{
string query = ":!!if exist foo.txt del foo.txt";
@@ -247,12 +248,12 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
var exception = Assert.Throws<BatchParserException>(() => p.Parse());
// Verify the message should be "Command Execute is not supported."
Assert.Equal("Command Execute is not supported.", exception.Message);
Assert.AreEqual("Command Execute is not supported.", exception.Message);
}
}
// This test case is to verify that, Lexer type for :!!If was set to "Text" instead of "Execute"
[Fact]
[Test]
public void VerifyLexerTypeOfSqlCmdIFisExecute()
{
string query = ":!!if exist foo.txt del foo.txt";
@@ -264,33 +265,26 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
type = lexer.CurrentTokenType;
}
// we are expecting the lexer type should to be Execute.
Assert.Equal("Execute", type.ToString());
Assert.AreEqual("Execute", type.ToString());
}
// Verify the custom exception functionality by raising user defined error.
[Fact]
[Test]
public void VerifyCustomBatchParserException()
{
string message = "This is userDefined Error";
Token token = new Token(LexerTokenType.Text, new PositionStruct(), new PositionStruct(), message, "test");
BatchParserException batchParserException = new BatchParserException(ErrorCode.VariableNotDefined, token, message);
try
{
throw new BatchParserException(ErrorCode.UnrecognizedToken, token, "test");
}
catch (Exception ex)
{
Assert.Equal(batchParserException.ErrorCode.ToString(), ErrorCode.VariableNotDefined.ToString());
Assert.Equal(message, batchParserException.Text);
Assert.Equal(LexerTokenType.Text.ToString(), batchParserException.TokenType.ToString());
Assert.IsType<BatchParserException>(ex);
}
BatchParserException batchParserException = new BatchParserException(ErrorCode.VariableNotDefined, token, message);
Assert.AreEqual(batchParserException.ErrorCode.ToString(), ErrorCode.VariableNotDefined.ToString());
Assert.AreEqual(message, batchParserException.Text);
Assert.AreEqual(LexerTokenType.Text.ToString(), batchParserException.TokenType.ToString());
}
// Verify whether the executionEngine execute script
[Fact]
[Test]
public void VerifyExecuteScript()
{
using (ExecutionEngine executionEngine = new ExecutionEngine())
@@ -305,13 +299,13 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
ScriptExecutionResult result = (testExecutor.ExecutionResult == ScriptExecutionResult.Success) ? ScriptExecutionResult.Success : ScriptExecutionResult.Failure;
Assert.Equal<ScriptExecutionResult>(ScriptExecutionResult.Success, result);
Assert.AreEqual(ScriptExecutionResult.Success, result);
}
}
}
// Verify whether the batchParser execute SqlCmd.
//[Fact] // This Testcase should execute and pass, But it is failing now.
//[Test] // This Testcase should execute and pass, But it is failing now.
public void VerifyIsSqlCmd()
{
using (ExecutionEngine executionEngine = new ExecutionEngine())
@@ -330,7 +324,7 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
/// <summary>
/// Verify whether the batchParser execute SqlCmd successfully
/// </summary>
[Fact]
[Test]
public void VerifyRunSqlCmd()
{
using (ExecutionEngine executionEngine = new ExecutionEngine())
@@ -365,7 +359,7 @@ GO";
/// <summary>
/// Verify whether the batchParser parsed :connect command successfully
/// </summary>
[Fact]
[Test]
public void VerifyConnectSqlCmd()
{
using (ExecutionEngine executionEngine = new ExecutionEngine())
@@ -374,14 +368,15 @@ GO";
string serverName = liveConnection.ConnectionInfo.ConnectionDetails.ServerName;
string userName = liveConnection.ConnectionInfo.ConnectionDetails.UserName;
string password = liveConnection.ConnectionInfo.ConnectionDetails.Password;
var credentials = string.IsNullOrEmpty(userName) ? string.Empty : $"-U {userName} -P {password}";
string sqlCmdQuery = $@"
:Connect {serverName} -U {userName} -P {password}
:Connect {serverName}{credentials}
GO
select * from sys.databases where name = 'master'
GO";
string sqlCmdQueryIncorrect = $@"
:Connect {serverName} -u {userName} -p {password}
:Connect {serverName} -u uShouldbeUpperCase -p pShouldbeUpperCase
GO
select * from sys.databases where name = 'master'
GO";
@@ -390,17 +385,23 @@ GO";
using (TestExecutor testExecutor = new TestExecutor(sqlCmdQuery, sqlConn, condition))
{
testExecutor.Run();
Assert.True(testExecutor.ParserExecutionError == false, "Parse Execution error should be false");
Assert.True(testExecutor.ResultCountQueue.Count == 1, $"Unexpected number of ResultCount items - expected 1 but got {testExecutor.ResultCountQueue.Count}");
Assert.True(testExecutor.ErrorMessageQueue.Count == 0, $"Unexpected error messages from test executor : {string.Join(Environment.NewLine, testExecutor.ErrorMessageQueue)}");
Assert.Multiple(() =>
{
Assert.That(testExecutor.ParserExecutionError, Is.False, "Parse Execution error should be false");
Assert.That(testExecutor.ResultCountQueue.Count, Is.EqualTo(1), "Unexpected number of ResultCount items");
Assert.That(testExecutor.ErrorMessageQueue, Is.Empty, "Unexpected error messages from test executor");
});
}
using (SqlConnection sqlConn = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo))
using (TestExecutor testExecutor = new TestExecutor(sqlCmdQueryIncorrect, sqlConn, condition))
{
testExecutor.Run();
Assert.True(testExecutor.ParserExecutionError == true, "Parse Execution error should be true");
Assert.Multiple(() =>
{
Assert.True(testExecutor.ParserExecutionError, "Parse Execution error should be true");
Assert.That(testExecutor.ErrorMessageQueue, Has.Member("Incorrect syntax was encountered while -u was being parsed."), "error message expected");
});
}
}
}
@@ -408,7 +409,7 @@ GO";
/// <summary>
/// Verify whether the batchParser parsed :on error successfully
/// </summary>
[Fact]
[Test]
public void VerifyOnErrorSqlCmd()
{
using (ExecutionEngine executionEngine = new ExecutionEngine())
@@ -443,7 +444,7 @@ GO";
/// <summary>
/// Verify whether the batchParser parses Include command i.e. :r successfully
/// </summary>
[Fact]
[Test]
public void VerifyIncludeSqlCmd()
{
string file = "VerifyIncludeSqlCmd_test.sql";
@@ -486,7 +487,7 @@ GO";
}
// Verify whether the executionEngine execute Batch
[Fact]
[Test]
public void VerifyExecuteBatch()
{
using (ExecutionEngine executionEngine = new ExecutionEngine())
@@ -498,7 +499,7 @@ GO";
var executionPromise = new TaskCompletionSource<bool>();
executionEngine.BatchParserExecutionFinished += (object sender, BatchParserExecutionFinishedEventArgs e) =>
{
Assert.Equal(ScriptExecutionResult.Success, e.ExecutionResult);
Assert.AreEqual(ScriptExecutionResult.Success, e.ExecutionResult);
executionPromise.SetResult(true);
};
executionEngine.ExecuteBatch(new ScriptExecutionArgs(query, sqlConn, 15, new ExecutionEngineConditions(), new BatchParserMockEventHandler()));
@@ -508,7 +509,7 @@ GO";
}
}
[Fact]
[Test]
public void CanceltheBatch()
{
Batch batch = new Batch();
@@ -567,7 +568,7 @@ GO";
if (lexerError == false)
{
// Verify that all text from tokens can be recombined into original string
Assert.Equal<string>(inputText, roundtripTextBuilder.ToString());
Assert.AreEqual(inputText, roundtripTextBuilder.ToString());
}
}
}

View File

@@ -6,31 +6,32 @@
using System;
using Microsoft.SqlTools.ServiceLayer.BatchParser;
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
using Xunit;
using NUnit.Framework;
namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
{
[TestFixture]
public class BatchParserWrapperTests
{
[Fact]
[Test]
public void CheckSimpleSingleSQLBatchStatement()
{
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
{
string sqlScript = "select * from sys.objects";
var batches = parserWrapper.GetBatches(sqlScript);
Assert.Equal(1, batches.Count);
Assert.AreEqual(1, batches.Count);
BatchDefinition batch = batches[0];
Assert.Equal(sqlScript, batch.BatchText);
Assert.Equal(1, batch.StartLine);
Assert.Equal(1, batch.StartColumn);
Assert.Equal(2, batch.EndLine);
Assert.Equal(sqlScript.Length + 1, batch.EndColumn);
Assert.Equal(1, batch.BatchExecutionCount);
Assert.AreEqual(sqlScript, batch.BatchText);
Assert.AreEqual(1, batch.StartLine);
Assert.AreEqual(1, batch.StartColumn);
Assert.AreEqual(2, batch.EndLine);
Assert.AreEqual(sqlScript.Length + 1, batch.EndColumn);
Assert.AreEqual(1, batch.BatchExecutionCount);
}
}
[Fact]
[Test]
public void CheckSimpleMultipleQLBatchStatement()
{
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
@@ -44,48 +45,48 @@ namespace Microsoft.SqlTools.ManagedBatchParser.UnitTests.BatchParser
SELECT 'LastLine'";
var batches = parserWrapper.GetBatches(sqlScript);
// Each select statement is one batch , so we are expecting 4 batches.
Assert.Equal(4, batches.Count);
Assert.AreEqual(4, batches.Count);
}
}
[Fact]
[Test]
public void CheckSQLBatchStatementWithRepeatExecution()
{
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
{
string sqlScript = "select * from sys.object" + Environment.NewLine + "GO 2";
var batches = parserWrapper.GetBatches(sqlScript);
Assert.Equal(1, batches.Count);
Assert.AreEqual(1, batches.Count);
BatchDefinition batch = batches[0];
Assert.Equal(2, batch.BatchExecutionCount);
Assert.AreEqual(2, batch.BatchExecutionCount);
}
}
[Fact]
[Test]
public void CheckComment()
{
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
{
string sqlScript = "-- this is a comment --";
var batches = parserWrapper.GetBatches(sqlScript);
Assert.Equal(1, batches.Count);
Assert.AreEqual(1, batches.Count);
BatchDefinition batch = batches[0];
Assert.Equal(sqlScript, batch.BatchText);
Assert.Equal(1, batch.StartLine);
Assert.Equal(1, batch.StartColumn);
Assert.Equal(2, batch.EndLine);
Assert.Equal(sqlScript.Length + 1, batch.EndColumn);
Assert.AreEqual(sqlScript, batch.BatchText);
Assert.AreEqual(1, batch.StartLine);
Assert.AreEqual(1, batch.StartColumn);
Assert.AreEqual(2, batch.EndLine);
Assert.AreEqual(sqlScript.Length + 1, batch.EndColumn);
}
}
[Fact]
[Test]
public void CheckNoOps()
{
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
{
string sqlScript = "GO";
var batches = parserWrapper.GetBatches(sqlScript);
Assert.Equal(0, batches.Count);
Assert.AreEqual(0, batches.Count);
}
}
}