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

@@ -401,5 +401,64 @@ namespace Microsoft.SqlTools.ServiceLayer.IntegrationTests.LanguageServer
Assert.True(notif.Diagnostics.Length == errors, $"Notification errors {notif.Diagnostics.Length} are not as expected {errors}");
return true;
}
[Test]
//simple select star with single column in the table
[TestCase("select * from wildcard_test_table", 0, 8, "CREATE TABLE wildcard_test_table(col1 int)", "[col1]")]
//simple select star with multiple columns in the table
[TestCase("select * from wildcard_test_table", 0, 8, "CREATE TABLE wildcard_test_table(col1 int, col2 int, \"col3\" int)", "[col1],\r\n[col2],\r\n[col3]")]
//select star query with special characters in the table
[TestCase("select * from wildcard_test_table", 0, 8, "CREATE TABLE wildcard_test_table(\"col[$$$#]\" int)", "[col[$$$#]]]")]
//select star query for multiple tables
[TestCase("select * from wildcard_test_table1 CROSS JOIN wildcard_test_table2", 0, 8, "CREATE TABLE wildcard_test_table1(table1col1 int); CREATE TABLE wildcard_test_table2(table2col1 int)", "[wildcard_test_table1].[table1col1],\r\n[wildcard_test_table2].[table2col1]")]
//select star query with object identifier in associated with * eg: a.*
[TestCase("select *, a.* from wildcard_test_table1 as a CROSS JOIN wildcard_test_table2", 0, 13, "CREATE TABLE wildcard_test_table1(table1col1 int); CREATE TABLE wildcard_test_table2(table2col1 int)", "[a].[table1col1]")]
//select star query with nested from statement
[TestCase("select * from (select col2 from wildcard_test_table1) as alias", 0, 8, "CREATE TABLE wildcard_test_table1(col1 int, col2 int)", "[col2]")]
public async Task ExpandSqlStarExpressionsTest(string sqlStarQuery, int cursorLine, int cursorColumn, string createTableQueries, string expectedStarExpansionInsertText)
{
var testDb = SqlTestDb.CreateNew(TestServerType.OnPrem, false, null, null, "WildCardExpansionTest");
try
{
var connectionInfoResult = LiveConnectionHelper.InitLiveConnectionInfo(testDb.DatabaseName);
var langService = LanguageService.Instance;
await langService.UpdateLanguageServiceOnConnection(connectionInfoResult.ConnectionInfo);
connectionInfoResult.ScriptFile.SetFileContents(sqlStarQuery);
var textDocumentPosition =
connectionInfoResult.TextDocumentPosition ??
new TextDocumentPosition()
{
TextDocument = new TextDocumentIdentifier
{
Uri = connectionInfoResult.ScriptFile.ClientUri
},
Position = new Position
{
Line = cursorLine,
Character = cursorColumn //Position of the star expression
}
};
// Now create tables that should show up in the completion list
testDb.RunQuery(createTableQueries);
// And refresh the cache
await langService.HandleRebuildIntelliSenseNotification(
new RebuildIntelliSenseParams() { OwnerUri = connectionInfoResult.ScriptFile.ClientUri },
new TestEventContext());
// Now we should expect to see the star expansion show up in the completion list
var starExpansionCompletionItem = await langService.GetCompletionItems(
textDocumentPosition, connectionInfoResult.ScriptFile, connectionInfoResult.ConnectionInfo);
Assert.AreEqual(expectedStarExpansionInsertText, starExpansionCompletionItem[0].InsertText, "Star expansion not found");
}
finally
{
testDb.Cleanup();
}
}
}
}

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."));
}
}
}