mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-07 17:25:04 -05:00
Task/batch parser wrapper tests (#314)
* added more batch parser tests * fixed merge conflicts * change debug type to portable * fixed imports according to review
This commit is contained in:
@@ -1,65 +1,120 @@
|
||||
//
|
||||
// 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 Microsoft.SqlTools.ServiceLayer.BatchParser;
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.BatchParser
|
||||
{
|
||||
public class BatchParserSqlCmdTests : IDisposable
|
||||
{
|
||||
private BatchParserSqlCmd bpcmd;
|
||||
private PositionStruct testPOS;
|
||||
public BatchParserSqlCmdTests()
|
||||
{
|
||||
bpcmd = new BatchParserSqlCmd();
|
||||
testPOS = new PositionStruct();
|
||||
bpcmd.SetVariable(testPOS, "variable1", "test1");
|
||||
bpcmd.SetVariable(testPOS, "variable2", "test2");
|
||||
bpcmd.SetVariable(testPOS, "variable3", "test3");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (bpcmd != null)
|
||||
{
|
||||
bpcmd = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckSetVariable()
|
||||
{
|
||||
Assert.Equal(bpcmd.InternalVariables.Count, 3);
|
||||
bpcmd.SetVariable(testPOS, "variable4", "test4");
|
||||
bpcmd.SetVariable(testPOS, "variable5", "test5");
|
||||
bpcmd.SetVariable(testPOS, "variable6", "test6");
|
||||
Assert.Equal(bpcmd.InternalVariables.Count, 6);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckGetVariable()
|
||||
{
|
||||
string value = bpcmd.GetVariable(testPOS, "variable1");
|
||||
Assert.Equal("test1", value);
|
||||
value = bpcmd.GetVariable(testPOS, "variable2");
|
||||
Assert.Equal("test2", value);
|
||||
value = bpcmd.GetVariable(testPOS, "variable3");
|
||||
Assert.Equal("test3", value);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
//
|
||||
// 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.IO;
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser;
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.BatchParser
|
||||
{
|
||||
public class BatchParserSqlCmdTests : IDisposable
|
||||
{
|
||||
private BatchParserSqlCmd bpcmd;
|
||||
private PositionStruct testPOS;
|
||||
public BatchParserSqlCmdTests()
|
||||
{
|
||||
bpcmd = new BatchParserSqlCmd();
|
||||
testPOS = new PositionStruct();
|
||||
bpcmd.SetVariable(testPOS, "variable1", "test1");
|
||||
bpcmd.SetVariable(testPOS, "variable2", "test2");
|
||||
bpcmd.SetVariable(testPOS, "variable3", "test3");
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
if (bpcmd != null)
|
||||
{
|
||||
bpcmd = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckSetVariable()
|
||||
{
|
||||
Assert.Equal(bpcmd.InternalVariables.Count, 3);
|
||||
bpcmd.SetVariable(testPOS, "variable4", "test4");
|
||||
bpcmd.SetVariable(testPOS, "variable5", "test5");
|
||||
bpcmd.SetVariable(testPOS, "variable6", "test6");
|
||||
Assert.Equal(bpcmd.InternalVariables.Count, 6);
|
||||
|
||||
bpcmd.SetVariable(testPOS, "variable1", null);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckSetNullValueVariable()
|
||||
{
|
||||
bpcmd.SetVariable(testPOS, "variable6", null);
|
||||
Assert.Equal(bpcmd.InternalVariables.Count, 5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckGetVariable()
|
||||
{
|
||||
string value = bpcmd.GetVariable(testPOS, "variable1");
|
||||
Assert.Equal("test1", value);
|
||||
value = bpcmd.GetVariable(testPOS, "variable2");
|
||||
Assert.Equal("test2", value);
|
||||
value = bpcmd.GetVariable(testPOS, "variable3");
|
||||
Assert.Equal("test3", value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckGetNullVariable()
|
||||
{
|
||||
Assert.Null(bpcmd.GetVariable(testPOS, "variable6"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckInclude()
|
||||
{
|
||||
TextReader textReader = null;
|
||||
string outString = "out";
|
||||
var result = bpcmd.Include(null, out textReader, out outString);
|
||||
Assert.Equal(result, BatchParserAction.Abort);
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckOnError()
|
||||
{
|
||||
var errorActionChanged = bpcmd.ErrorActionChanged;
|
||||
var action = new OnErrorAction();
|
||||
var result = bpcmd.OnError(null, action);
|
||||
Assert.Equal(result, BatchParserAction.Continue);
|
||||
bpcmd.ErrorActionChanged = null;
|
||||
result = bpcmd.OnError(null, action);
|
||||
Assert.NotEqual(result, BatchParserAction.Continue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckConnectionChangedDelegate()
|
||||
{
|
||||
var initial = bpcmd.ConnectionChanged;
|
||||
bpcmd.ConnectionChanged = null;
|
||||
Assert.Null(bpcmd.ConnectionChanged);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckVariableSubstitutionDisabled()
|
||||
{
|
||||
bpcmd.DisableVariableSubstitution();
|
||||
bpcmd.SetVariable(testPOS, "variable1", "test");
|
||||
var result = bpcmd.GetVariable(testPOS, "variable1");
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,51 +1,54 @@
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser;
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.BatchParser
|
||||
{
|
||||
public class BatchParserWrapperTests
|
||||
{
|
||||
private BatchParserWrapper parserWrapper;
|
||||
public BatchParserWrapperTests()
|
||||
{
|
||||
parserWrapper = new BatchParserWrapper();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckSimpleSingleSQLBatchStatement()
|
||||
{
|
||||
string sqlScript = "select * from sys.objects";
|
||||
var batches = parserWrapper.GetBatches(sqlScript);
|
||||
Assert.Equal(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, batch.EndColumn);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckComment()
|
||||
{
|
||||
string sqlScript = "-- this is a comment --";
|
||||
var batches = parserWrapper.GetBatches(sqlScript);
|
||||
Assert.Equal(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, batch.EndColumn);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckNoOps()
|
||||
{
|
||||
string sqlScript = "GO";
|
||||
var batches = parserWrapper.GetBatches(sqlScript);
|
||||
Assert.Equal(0, batches.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser;
|
||||
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.BatchParser
|
||||
{
|
||||
public class BatchParserWrapperTests
|
||||
{
|
||||
[Fact]
|
||||
public void CheckSimpleSingleSQLBatchStatement()
|
||||
{
|
||||
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
|
||||
{
|
||||
string sqlScript = "select * from sys.objects";
|
||||
var batches = parserWrapper.GetBatches(sqlScript);
|
||||
Assert.Equal(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, batch.EndColumn);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckComment()
|
||||
{
|
||||
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
|
||||
{
|
||||
string sqlScript = "-- this is a comment --";
|
||||
var batches = parserWrapper.GetBatches(sqlScript);
|
||||
Assert.Equal(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, batch.EndColumn);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckNoOps()
|
||||
{
|
||||
using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
|
||||
{
|
||||
string sqlScript = "GO";
|
||||
var batches = parserWrapper.GetBatches(sqlScript);
|
||||
Assert.Equal(0, batches.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,10 @@ using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
|
||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||
using Microsoft.SqlTools.ServiceLayer.IntegrationTests.Utility;
|
||||
using Microsoft.SqlTools.ServiceLayer.Test.Common;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
{
|
||||
/// <summary>
|
||||
@@ -297,6 +299,14 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
Assert.True(CompareTwoStringLists(executor.ErrorMessageQueue, expErrorMessage));
|
||||
Assert.True(CompareTwoIntLists(executor.ResultCountQueue, expResultCounts));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExecutionEngineTest_DiscardConnection()
|
||||
{
|
||||
ExecutionEngine engine = new ExecutionEngine();
|
||||
Assert.True(ConnectionDiscardWrapper(engine));
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Different execution conditions
|
||||
@@ -456,6 +466,29 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
// However since that gets mapped to Failure anyhow, consider "Failure" as acceptable here
|
||||
Assert.True(executor.ExecutionResult.HasFlag(ScriptExecutionResult.Failure), "Expected failure when invalid connection is present" );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Test with multiple conditions true
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TestExecutionEngineConditions()
|
||||
{
|
||||
string sqlStatement = "select * from sys.databases\ngo\nselect name from sys.databases\ngo\nprint 'test'\ngo";
|
||||
ExecutionEngineConditions conditions = new ExecutionEngineConditions();
|
||||
conditions.IsNoExec = true;
|
||||
conditions.IsStatisticsIO = true;
|
||||
conditions.IsStatisticsTime = true;
|
||||
conditions.IsEstimatedShowPlan = true;
|
||||
TestExecutor executor = new TestExecutor(sqlStatement, connection, conditions, false);
|
||||
executor.Run();
|
||||
|
||||
//Get the expected values
|
||||
List<string> batchScripts = executor.BatchScripts;
|
||||
ExecuteSqlBatch(batchScripts, connection);
|
||||
|
||||
Assert.Equal(ScriptExecutionResult.Success, executor.ExecutionResult);
|
||||
Assert.True(CompareTwoIntLists(executor.ResultCountQueue, expResultCounts));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region SQL Commands
|
||||
@@ -491,9 +524,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
{
|
||||
TestExecutor executor = new TestExecutor(stmt, connection, conditions);
|
||||
executor.Run();
|
||||
|
||||
//Assert.AreEqual(ScriptExecutionResult.Failure, executor.ExecutionResult);
|
||||
//Assert.IsTrue(executor.ResultCountQueue.Count == 0);
|
||||
Assert.True(executor.ResultCountQueue.Count == 0);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -501,7 +532,7 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
|
||||
#region Threading
|
||||
/// <summary>
|
||||
/// Test synchous cancel
|
||||
/// Test synchronous cancel
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void ExecutionEngineTest_SyncCancel()
|
||||
@@ -657,6 +688,69 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
|
||||
#endregion
|
||||
|
||||
#region Get/Set Methods
|
||||
|
||||
[Fact]
|
||||
public void TestShowStatements()
|
||||
{
|
||||
Assert.NotNull(ExecutionEngineConditions.ShowPlanXmlStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.ShowPlanAllStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.ShowPlanTextStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.StatisticsXmlStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.StatisticsProfileStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.ParseOnlyStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.NoExecStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.StatisticsIOStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.StatisticsTimeStatement(true));
|
||||
Assert.NotNull(ExecutionEngineConditions.ResetStatement);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestExecutionEngineConditionsSetMethods()
|
||||
{
|
||||
ExecutionEngineConditions conditions = new ExecutionEngineConditions();
|
||||
bool getValue = conditions.IsScriptExecutionTracked;
|
||||
conditions.IsScriptExecutionTracked = !getValue;
|
||||
Assert.Equal(conditions.IsScriptExecutionTracked, !getValue);
|
||||
|
||||
getValue = conditions.IsEstimatedShowPlan;
|
||||
conditions.IsEstimatedShowPlan = !getValue;
|
||||
Assert.Equal(conditions.IsEstimatedShowPlan, !getValue);
|
||||
|
||||
getValue = conditions.IsActualShowPlan;
|
||||
conditions.IsActualShowPlan = !getValue;
|
||||
Assert.Equal(conditions.IsActualShowPlan, !getValue);
|
||||
|
||||
getValue = conditions.IsSuppressProviderMessageHeaders;
|
||||
conditions.IsSuppressProviderMessageHeaders = !getValue;
|
||||
Assert.Equal(conditions.IsSuppressProviderMessageHeaders, !getValue);
|
||||
|
||||
getValue = conditions.IsNoExec;
|
||||
conditions.IsNoExec = !getValue;
|
||||
Assert.Equal(conditions.IsNoExec, !getValue);
|
||||
|
||||
getValue = conditions.IsStatisticsIO;
|
||||
conditions.IsStatisticsIO = !getValue;
|
||||
Assert.Equal(conditions.IsStatisticsIO, !getValue);
|
||||
|
||||
getValue = conditions.IsShowPlanText;
|
||||
conditions.IsShowPlanText = !getValue;
|
||||
Assert.Equal(conditions.IsShowPlanText, !getValue);
|
||||
|
||||
getValue = conditions.IsStatisticsTime;
|
||||
conditions.IsStatisticsTime = !getValue;
|
||||
Assert.Equal(conditions.IsStatisticsTime, !getValue);
|
||||
|
||||
getValue = conditions.IsSqlCmd;
|
||||
conditions.IsSqlCmd = !getValue;
|
||||
Assert.Equal(conditions.IsSqlCmd, !getValue);
|
||||
|
||||
conditions.BatchSeparator = "GO";
|
||||
Assert.Equal(conditions.BatchSeparator, "GO");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private methods
|
||||
/// <summary>
|
||||
/// Connection to a database
|
||||
@@ -782,6 +876,21 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
|
||||
return isSame;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper to test the Close method in ExecutionEngine
|
||||
/// </summary>
|
||||
/// <param name="engine"></param>
|
||||
/// <returns></returns>
|
||||
private bool ConnectionDiscardWrapper(ExecutionEngine engine)
|
||||
{
|
||||
if (engine == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
engine.Close(false, true, true);
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,7 +219,10 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.TSQLExecutionEngine
|
||||
sqlStatement = batch;
|
||||
conditions.IsHaltOnError = exeCondition.IsHaltOnError;
|
||||
conditions.IsParseOnly = exeCondition.IsParseOnly;
|
||||
conditions.IsTransactionWrapped = exeCondition.IsTransactionWrapped;
|
||||
conditions.IsTransactionWrapped = exeCondition.IsTransactionWrapped;
|
||||
conditions.IsNoExec = exeCondition.IsNoExec;
|
||||
conditions.IsStatisticsIO = exeCondition.IsStatisticsIO;
|
||||
conditions.IsStatisticsTime = exeCondition.IsStatisticsTime;
|
||||
|
||||
_cancel = cancelExecution;
|
||||
connection = conn;
|
||||
|
||||
Reference in New Issue
Block a user