Adding star expression expansion (#1270)

This commit is contained in:
Aasim Khan
2021-10-27 16:59:05 -07:00
committed by GitHub
parent 26d4339277
commit e246bc5325
6 changed files with 309 additions and 30 deletions

View File

@@ -5,8 +5,10 @@
using System.Threading.Tasks;
using Microsoft.SqlServer.Management.SqlParser.Parser;
using Microsoft.SqlServer.Management.SqlParser.SqlCodeDom;
using Microsoft.SqlTools.Hosting.Protocol;
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion;
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
using Moq;
@@ -186,5 +188,81 @@ namespace Microsoft.SqlTools.ServiceLayer.UnitTests.LanguageServer
// verify that send result was called with a completion array
requestContext.Verify(m => m.SendResult(It.IsAny<CompletionItem[]>()), Times.Once());
}
public ScriptDocumentInfo CreateSqlStarTestFile(string sqlText, int startLine, int startColumn)
{
var uri = "file://nofile.sql";
var textDocumentPosition = new TextDocumentPosition()
{
TextDocument = new TextDocumentIdentifier()
{
Uri = uri
},
Position = new Position()
{
Line = startLine,
Character = startColumn
}
};
var scriptFile = new ScriptFile()
{
ClientUri = uri,
Contents = sqlText
};
ParseResult parseResult = langService.ParseAndBind(scriptFile, null);
ScriptParseInfo scriptParseInfo = langService.GetScriptParseInfo(scriptFile.ClientUri, true);
return new ScriptDocumentInfo(textDocumentPosition, scriptFile, scriptParseInfo);
}
[Test]
//complete select query with the cursor at * should return a sqlselectstarexpression object.
[TestCase("select * from sys.all_objects", 0, 8, "SelectStarExpression is not returned on complete select query with star")]
//incomplete select query with the cursor at * should sqlselectstarexpression
[TestCase("select * ", 0, 8, "SelectStarExpression is returned on an incomplete select query with star")]
//method should return sqlselectstarexpression on *s with object identifiers.
[TestCase("select a.* from sys.all_objects as a", 0, 10, "SelectStarExpression returned on star expression with object identifier")]
public void TryGetSqlSelectStarStatementNotNullTests(string sqlQuery, int cursorLine, int cursorColumn, string errorValidationMessage)
{
InitializeTestObjects();
var testFile = CreateSqlStarTestFile(sqlQuery, cursorLine, cursorColumn);
Assert.NotNull(AutoCompleteHelper.TryGetSelectStarStatement(testFile.ScriptParseInfo.ParseResult.Script, testFile), errorValidationMessage);
}
[Test]
//complete select query with the cursor not at * should return null.
[TestCase("select * from sys.all_objects", 0, 0, "null is not returned when the cursor is not at a star expression")]
//file with no text should return null
[TestCase("", 0, 0, "null is not returned on file with empty sql text")]
//file with out of bounds cursor position should return null
[TestCase("select * from sys.all_objects", 0, 100, "null is not returned when the cursor is out of bounds.")]
public void TryGetSqlSelectStarStatementNullTests(string sqlQuery, int cursorLine, int cursorColumn, string errorValidationMessage)
{
InitializeTestObjects();
var testFile = CreateSqlStarTestFile(sqlQuery, cursorLine, cursorColumn);
Assert.Null(AutoCompleteHelper.TryGetSelectStarStatement(testFile.ScriptParseInfo.ParseResult.Script, testFile), errorValidationMessage);
}
[Test]
public void TryGetSqlSelectStarStatementNullFileTest()
{
Assert.Null(AutoCompleteHelper.TryGetSelectStarStatement(null, null), "null is not returned on null file");
}
[Test]
[TestCase("select a.*, * from sys.all_objects as a CROSS JOIN sys.databases", 0, 10, "a.*")]
[TestCase("select a.*, * from sys.all_objects as a CROSS JOIN sys.databases", 0, 13, "*")]
public void TryGetSqlSelectStarStatmentMulitpleStarExpressionsTest(string sqlQuery, int cursorLine, int cursorColumn, string expectedStarExpressionSqlText)
{
InitializeTestObjects();
var testFile = CreateSqlStarTestFile(sqlQuery, cursorLine, cursorColumn);
var starExpressionTest = AutoCompleteHelper.TryGetSelectStarStatement(testFile.ScriptParseInfo.ParseResult.Script, testFile).Sql;
Assert.AreEqual(expectedStarExpressionSqlText, expectedStarExpressionSqlText, string.Format("correct SelectStarExpression is not returned."));
}
}
}