Initial implementation for autocomplete.

This commit is contained in:
Karl Burtram
2016-07-24 02:47:45 -07:00
parent 3a55333598
commit 4484c8d8cf
12 changed files with 664 additions and 19 deletions

View File

@@ -0,0 +1,63 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
#define USE_LIVE_CONNECTION
using System.Threading.Tasks;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
namespace Microsoft.SqlTools.Test.Connection
{
/// <summary>
/// Tests for the ServiceHost Connection Service tests
/// </summary>
public class ConnectionServiceTests
{
#region "Connection tests"
/// <summary>
/// Verify that the SQL parser correctly detects errors in text
/// </summary>
[Fact]
public void ConnectToDatabaseTest()
{
// connect to a database instance
var connectionResult =
TestObjects.GetTestConnectionService()
.Connect(TestObjects.GetTestConnectionDetails());
// verify that a valid connection id was returned
Assert.True(connectionResult.ConnectionId > 0);
}
/// <summary>
/// Verify that the SQL parser correctly detects errors in text
/// </summary>
[Fact]
public void OnConnectionCallbackHandlerTest()
{
bool callbackInvoked = false;
// setup connection service with callback
var connectionService = TestObjects.GetTestConnectionService();
connectionService.RegisterOnConnectionTask(
(sqlConnection) => {
callbackInvoked = true;
return Task.FromResult(true);
}
);
// connect to a database instance
var connectionResult = TestObjects.GetTestConnectionService()
.Connect(TestObjects.GetTestConnectionDetails());
// verify that a valid connection id was returned
Assert.True(callbackInvoked);
}
#endregion
}
}

View File

@@ -6,6 +6,8 @@
using Microsoft.SqlTools.EditorServices;
using Microsoft.SqlTools.EditorServices.Session;
using Microsoft.SqlTools.LanguageSupport;
using Microsoft.SqlTools.Test.Connection;
using Microsoft.SqlTools.Test.Utility;
using Xunit;
namespace Microsoft.SqlTools.Test.LanguageServer
@@ -15,15 +17,6 @@ namespace Microsoft.SqlTools.Test.LanguageServer
/// </summary>
public class LanguageServiceTests
{
/// <summary>
/// Create a test language service instance
/// </summary>
/// <returns></returns>
private LanguageService CreateTestService()
{
return new LanguageService(new SqlToolsContext(null, null));
}
#region "Diagnostics tests"
/// <summary>
@@ -36,7 +29,7 @@ namespace Microsoft.SqlTools.Test.LanguageServer
const string sqlWithErrors = "SELECT * FROM sys.objects";
// get the test service
LanguageService service = CreateTestService();
LanguageService service = TestObjects.GetTestLanguageService();
// parse the sql statement
var scriptFile = new ScriptFile();
@@ -57,7 +50,7 @@ namespace Microsoft.SqlTools.Test.LanguageServer
const string sqlWithErrors = "SELECT *** FROM sys.objects";
// get test service
LanguageService service = CreateTestService();
LanguageService service = TestObjects.GetTestLanguageService();
// parse sql statement
var scriptFile = new ScriptFile();
@@ -87,7 +80,7 @@ namespace Microsoft.SqlTools.Test.LanguageServer
"SELECT *** FROM sys.objects;\n";
// get test service
LanguageService service = CreateTestService();
LanguageService service = TestObjects.GetTestLanguageService();
// parse sql
var scriptFile = new ScriptFile();
@@ -111,6 +104,23 @@ namespace Microsoft.SqlTools.Test.LanguageServer
}
#endregion
#region "Autocomplete Tests"
/// <summary>
/// Verify that the SQL parser correctly detects errors in text
/// </summary>
[Fact]
public void AutocompleteTest()
{
var autocompleteService = TestObjects.GetAutoCompleteService();
var connectionService = TestObjects.GetTestConnectionService();
var connectionResult = connectionService.Connect(TestObjects.GetTestConnectionDetails());
var sqlConnection = connectionService.ActiveConnections[connectionResult.ConnectionId];
autocompleteService.UpdateAutoCompleteCache(sqlConnection);
}
#endregion
}
}

View File

@@ -4,7 +4,6 @@
//
using Microsoft.SqlTools.EditorServices.Protocol.MessageProtocol;
using System;
using System.Threading.Tasks;
namespace Microsoft.SqlTools.EditorServices.Test.Protocol.MessageProtocol

View File

@@ -0,0 +1,108 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//#define USE_LIVE_CONNECTION
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.SqlTools.EditorServices.Connection;
using Microsoft.SqlTools.EditorServices.Session;
using Microsoft.SqlTools.LanguageSupport;
using Xunit;
namespace Microsoft.SqlTools.Test.Utility
{
/// <summary>
/// Tests for the ServiceHost Connection Service tests
/// </summary>
public class TestObjects
{
/// <summary>
/// Creates a test connection service
/// </summary>
public static ConnectionService GetTestConnectionService()
{
#if !USE_LIVE_CONNECTION
// use mock database connection
return new ConnectionService(new TestSqlConnectionFactory());
#else
// connect to a real server instance
return ConnectionService.Instance;
#endif
}
/// <summary>
/// Creates a test connection details object
/// </summary>
public static ConnectionDetails GetTestConnectionDetails()
{
return new ConnectionDetails()
{
UserName = "sa",
Password = "Yukon900",
DatabaseName = "AdventureWorks2016CTP3_2",
ServerName = "sqltools11"
};
}
/// <summary>
/// Create a test language service instance
/// </summary>
/// <returns></returns>
public static LanguageService GetTestLanguageService()
{
return new LanguageService(new SqlToolsContext(null, null));
}
/// <summary>
/// Creates a test autocomplete service instance
/// </summary>
public static AutoCompleteService GetAutoCompleteService()
{
return AutoCompleteService.Instance;
}
/// <summary>
/// Creates a test sql connection factory instance
/// </summary>
public static ISqlConnectionFactory GetTestSqlConnectionFactory()
{
#if !USE_LIVE_CONNECTION
// use mock database connection
return new TestSqlConnectionFactory();
#else
// connect to a real server instance
return ConnectionService.Instance.ConnectionFactory;
#endif
}
}
/// <summary>
/// Test mock class for SqlConnection wrapper
/// </summary>
public class TestSqlConnection : ISqlConnection
{
public void OpenDatabaseConnection(string connectionString)
{
}
public IEnumerable<string> GetServerObjects()
{
return null;
}
}
/// <summary>
/// Test mock class for SqlConnection factory
/// </summary>
public class TestSqlConnectionFactory : ISqlConnectionFactory
{
public ISqlConnection CreateSqlConnection()
{
return new TestSqlConnection();
}
}
}

View File

@@ -6,11 +6,13 @@
"dependencies": {
"Newtonsoft.Json": "9.0.1",
"System.Runtime.Serialization.Primitives": "4.1.1",
"System.Data.Common": "4.1.0",
"System.Data.SqlClient": "4.1.0",
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-192208-24",
"ServiceHost": {
"target": "project"
}
"ServiceHost": {
"target": "project"
}
},
"testRunner": "xunit",
"frameworks": {