mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 10:58:30 -05:00
Add some tests to increase code coverage metrics
This commit is contained in:
@@ -49,7 +49,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
|
|
||||||
private ScriptParseInfo currentCompletionParseInfo;
|
private ScriptParseInfo currentCompletionParseInfo;
|
||||||
|
|
||||||
private bool ShouldEnableAutocomplete()
|
internal bool ShouldEnableAutocomplete()
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -109,12 +109,12 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
|
|
||||||
private static CancellationTokenSource ExistingRequestCancellation { get; set; }
|
private static CancellationTokenSource ExistingRequestCancellation { get; set; }
|
||||||
|
|
||||||
private SqlToolsSettings CurrentSettings
|
internal SqlToolsSettings CurrentSettings
|
||||||
{
|
{
|
||||||
get { return WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings; }
|
get { return WorkspaceService<SqlToolsSettings>.Instance.CurrentSettings; }
|
||||||
}
|
}
|
||||||
|
|
||||||
private Workspace.Workspace CurrentWorkspace
|
internal Workspace.Workspace CurrentWorkspace
|
||||||
{
|
{
|
||||||
get { return WorkspaceService<SqlToolsSettings>.Instance.Workspace; }
|
get { return WorkspaceService<SqlToolsSettings>.Instance.Workspace; }
|
||||||
}
|
}
|
||||||
@@ -123,7 +123,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
/// Gets or sets the current SQL Tools context
|
/// Gets or sets the current SQL Tools context
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private SqlToolsContext Context { get; set; }
|
internal SqlToolsContext Context { get; set; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@@ -219,7 +219,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
TextDocumentPosition textDocumentPosition,
|
TextDocumentPosition textDocumentPosition,
|
||||||
RequestContext<Location[]> requestContext)
|
RequestContext<Location[]> requestContext)
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Verbose, "HandleDefinitionRequest");
|
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +226,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
ReferencesParams referencesParams,
|
ReferencesParams referencesParams,
|
||||||
RequestContext<Location[]> requestContext)
|
RequestContext<Location[]> requestContext)
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Verbose, "HandleReferencesRequest");
|
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,7 +233,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
TextDocumentPosition textDocumentPosition,
|
TextDocumentPosition textDocumentPosition,
|
||||||
RequestContext<SignatureHelp> requestContext)
|
RequestContext<SignatureHelp> requestContext)
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Verbose, "HandleSignatureHelpRequest");
|
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,7 +240,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
TextDocumentPosition textDocumentPosition,
|
TextDocumentPosition textDocumentPosition,
|
||||||
RequestContext<DocumentHighlight[]> requestContext)
|
RequestContext<DocumentHighlight[]> requestContext)
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Verbose, "HandleDocumentHighlightRequest");
|
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,7 +247,6 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
TextDocumentPosition textDocumentPosition,
|
TextDocumentPosition textDocumentPosition,
|
||||||
RequestContext<Hover> requestContext)
|
RequestContext<Hover> requestContext)
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Verbose, "HandleHoverRequest");
|
|
||||||
await Task.FromResult(true);
|
await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,9 +21,13 @@ using Microsoft.SqlServer.Management.SqlParser.MetadataProvider;
|
|||||||
using Microsoft.SqlServer.Management.SqlParser.Parser;
|
using Microsoft.SqlServer.Management.SqlParser.Parser;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Connection.Contracts;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Credentials;
|
||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
|
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
|
using Microsoft.SqlTools.ServiceLayer.Test.Utility;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||||
using Microsoft.SqlTools.Test.Utility;
|
using Microsoft.SqlTools.Test.Utility;
|
||||||
using Moq;
|
using Moq;
|
||||||
@@ -148,6 +152,150 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region "General Language Service tests"
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Check that autocomplete is enabled by default
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void CheckAutocompleteEnabledByDefault()
|
||||||
|
{
|
||||||
|
// get test service
|
||||||
|
LanguageService service = TestObjects.GetTestLanguageService();
|
||||||
|
Assert.True(service.ShouldEnableAutocomplete());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test the service initialization code path and verify nothing throws
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void ServiceInitiailzation()
|
||||||
|
{
|
||||||
|
InitializeTestServices();
|
||||||
|
|
||||||
|
Assert.True(LanguageService.Instance.Context != null);
|
||||||
|
Assert.True(LanguageService.Instance.ConnectionServiceInstance != null);
|
||||||
|
Assert.True(LanguageService.Instance.CurrentSettings != null);
|
||||||
|
Assert.True(LanguageService.Instance.CurrentWorkspace != null);
|
||||||
|
|
||||||
|
LanguageService.Instance.ConnectionServiceInstance = null;
|
||||||
|
Assert.True(LanguageService.Instance.ConnectionServiceInstance == null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test the service initialization code path and verify nothing throws
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void UpdateLanguageServiceOnConnection()
|
||||||
|
{
|
||||||
|
string ownerUri = "file://my/sample/file.sql";
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
ConnectionInfo connInfo = null;
|
||||||
|
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||||
|
|
||||||
|
var task = LanguageService.Instance.UpdateLanguageServiceOnConnection(connInfo);
|
||||||
|
task.Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test the service initialization code path and verify nothing throws
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void PrepopulateCommonMetadata()
|
||||||
|
{
|
||||||
|
InitializeTestServices();
|
||||||
|
|
||||||
|
string sqlFilePath = GetTestSqlFile();
|
||||||
|
ScriptFile scriptFile = WorkspaceService<SqlToolsSettings>.Instance.Workspace.GetFile(sqlFilePath);
|
||||||
|
|
||||||
|
string ownerUri = scriptFile.ClientFilePath;
|
||||||
|
var connectionService = TestObjects.GetTestConnectionService();
|
||||||
|
var connectionResult =
|
||||||
|
connectionService
|
||||||
|
.Connect(new ConnectParams()
|
||||||
|
{
|
||||||
|
OwnerUri = ownerUri,
|
||||||
|
Connection = TestObjects.GetTestConnectionDetails()
|
||||||
|
});
|
||||||
|
|
||||||
|
ConnectionInfo connInfo = null;
|
||||||
|
connectionService.TryFindConnection(ownerUri, out connInfo);
|
||||||
|
|
||||||
|
ScriptParseInfo scriptInfo = new ScriptParseInfo();
|
||||||
|
scriptInfo.IsConnected = true;
|
||||||
|
|
||||||
|
AutoCompleteHelper.PrepopulateCommonMetadata(connInfo, scriptInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetTestSqlFile()
|
||||||
|
{
|
||||||
|
string filePath = Path.Combine(
|
||||||
|
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
|
||||||
|
"sqltest.sql");
|
||||||
|
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
File.Delete(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
File.WriteAllText(filePath, "SELECT * FROM sys.objects\n");
|
||||||
|
|
||||||
|
return filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InitializeTestServices()
|
||||||
|
{
|
||||||
|
const string hostName = "SQL Tools Service Host";
|
||||||
|
const string hostProfileId = "SQLToolsService";
|
||||||
|
Version hostVersion = new Version(1,0);
|
||||||
|
|
||||||
|
// set up the host details and profile paths
|
||||||
|
var hostDetails = new HostDetails(hostName, hostProfileId, hostVersion);
|
||||||
|
var profilePaths = new ProfilePaths(hostProfileId, "baseAllUsersPath", "baseCurrentUserPath");
|
||||||
|
SqlToolsContext sqlToolsContext = new SqlToolsContext(hostDetails, profilePaths);
|
||||||
|
|
||||||
|
// Grab the instance of the service host
|
||||||
|
Hosting.ServiceHost serviceHost = Hosting.ServiceHost.Instance;
|
||||||
|
|
||||||
|
// Start the service
|
||||||
|
serviceHost.Start().Wait();
|
||||||
|
|
||||||
|
// Initialize the services that will be hosted here
|
||||||
|
WorkspaceService<SqlToolsSettings>.Instance.InitializeService(serviceHost);
|
||||||
|
LanguageService.Instance.InitializeService(serviceHost, sqlToolsContext);
|
||||||
|
ConnectionService.Instance.InitializeService(serviceHost);
|
||||||
|
CredentialService.Instance.InitializeService(serviceHost);
|
||||||
|
QueryExecutionService.Instance.InitializeService(serviceHost);
|
||||||
|
|
||||||
|
serviceHost.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
private Hosting.ServiceHost GetTestServiceHost()
|
||||||
|
{
|
||||||
|
// set up the host details and profile paths
|
||||||
|
var hostDetails = new HostDetails("Test Service Host", "SQLToolsService", new Version(1,0));
|
||||||
|
var profilePaths = new ProfilePaths("SQLToolsService", "baseAllUsersPath", "baseCurrentUserPath");
|
||||||
|
SqlToolsContext context = new SqlToolsContext(hostDetails, profilePaths);
|
||||||
|
|
||||||
|
// Grab the instance of the service host
|
||||||
|
Hosting.ServiceHost host = Hosting.ServiceHost.Instance;
|
||||||
|
|
||||||
|
// Start the service
|
||||||
|
host.Start().Wait();
|
||||||
|
|
||||||
|
return host;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region "Autocomplete Tests"
|
#region "Autocomplete Tests"
|
||||||
|
|
||||||
// This test currently requires a live database connection to initialize
|
// This test currently requires a live database connection to initialize
|
||||||
|
|||||||
Reference in New Issue
Block a user