Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve kql and sql connection experience #2745

Merged
merged 5 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Microsoft.DotNet.Interactive.Kql.Tests/KqlConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,65 @@ public async Task It_can_connect_and_query_data()
e.FormattedValues.Any(f => f.MimeType == HtmlFormatter.MimeType));
}

[KqlFact]
public async Task It_does_not_add_a_kernel_on_connection_failure()
{
var cluster = KqlFactAttribute.GetClusterForTests();
using var kernel = await CreateKernelAsync();
var result = await kernel.SubmitCodeAsync(
"#!connect kql --kernel-name KustoHelp --cluster \"invalid_cluster\" --database \"Samples\"");

result.Events
.Should()
.ContainSingle<CommandFailed>();

var kqlKernel = kernel.FindKernelByName("kql-KustoHelp");

kqlKernel.Should().BeNull();
}

[KqlFact]
public async Task It_allows_to_retry_connecting()
{
var cluster = KqlFactAttribute.GetClusterForTests();
using var kernel = await CreateKernelAsync();
var result = await kernel.SubmitCodeAsync(
"#!connect kql --kernel-name KustoHelp --cluster \"invalid_cluster\" --database \"Samples\"");

result.Events
.Should()
.ContainSingle<CommandFailed>();

result = await kernel.SubmitCodeAsync(
$"#!connect kql --kernel-name KustoHelp --cluster \"{cluster}\" --database \"Samples\"");

result.Events
.Should()
.NotContainErrors();
}

[KqlFact]
public async Task It_gives_error_if_kernel_name_is_already_used()
{
var cluster = KqlFactAttribute.GetClusterForTests();
using var kernel = await CreateKernelAsync();
var result = await kernel.SubmitCodeAsync($"#!connect kql --kernel-name KustoHelp --cluster \"{cluster}\" --database \"Samples\"");

result.Events
.Should()
.NotContainErrors();

result = await kernel.SubmitCodeAsync($"#!connect kql --kernel-name KustoHelp --cluster \"{cluster}\" --database \"Samples\"");

result.Events
.Should()
.ContainSingle<CommandFailed>()
.Which
.Message
.Should()
.Contain("A kernel with name KustoHelp is already present. Use a different value for the kernel-name option.");
}

[KqlFact]
public async Task It_can_store_result_set_with_a_name()
{
Expand Down
9 changes: 9 additions & 0 deletions src/Microsoft.DotNet.Interactive.Kql/ConnectKqlCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.Threading.Tasks;
Expand Down Expand Up @@ -40,6 +41,14 @@ public ConnectKqlCommand(string resolvedToolsServicePath)

var localName = commandLineContext.ParseResult.GetValueForOption(KernelNameOption);

var found = context?.HandlingKernel?.RootKernel.FindKernelByName($"kql-{localName}") is not null;

if (found)
{
throw new InvalidOperationException(
$"A kernel with name {localName} is already present. Use a different value for the {KernelNameOption.Name} option.");
colombod marked this conversation as resolved.
Show resolved Hide resolved
}

var kernel = await connector.CreateKernelAsync(localName);

return kernel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,64 @@ public async Task It_can_connect_and_query_data()
e.FormattedValues.Any(f => f.MimeType == HtmlFormatter.MimeType));
}

[MsSqlFact]
public async Task It_does_not_add_a_kernel_on_connection_failure()
{
var connectionString = MsSqlFactAttribute.GetConnectionStringForTests();
using var kernel = await CreateKernelAsync();
var result = await kernel.SubmitCodeAsync("#!connect mssql --kernel-name adventureworks \"invalid_connection_string\"");

result.Events
.Should()
.ContainSingle<CommandFailed>();

var kqlKernel = kernel.FindKernelByName("sql-adventureworks");

kqlKernel.Should().BeNull();
}

[MsSqlFact]
public async Task It_allows_to_retry_connecting()
{
var connectionString = MsSqlFactAttribute.GetConnectionStringForTests();
using var kernel = await CreateKernelAsync();
var result = await kernel.SubmitCodeAsync("#!connect mssql --kernel-name adventureworks \"invalid_connection_string\"");

result.Events
.Should()
.ContainSingle<CommandFailed>();

result = await kernel.SubmitCodeAsync(
$"#!connect mssql --kernel-name adventureworks \"{connectionString}\"");

result.Events
.Should()
.NotContainErrors();
}

[MsSqlFact]
public async Task It_gives_error_if_kernel_name_is_already_used()
{
var connectionString = MsSqlFactAttribute.GetConnectionStringForTests();
using var kernel = await CreateKernelAsync();
var result = await kernel.SubmitCodeAsync($"#!connect mssql --kernel-name adventureworks \"{connectionString}\"");

result.Events
.Should()
.NotContainErrors();

result = await kernel.SubmitCodeAsync(
$"#!connect mssql --kernel-name adventureworks \"{connectionString}\"");

result.Events
.Should()
.ContainSingle<CommandFailed>()
.Which
.Message
.Should()
.Contain("\"A kernel with name adventureworks is already present. Use a different value for the kernel-name option.\"");
colombod marked this conversation as resolved.
Show resolved Hide resolved
}

[MsSqlFact]
public async Task null_values_are_preserved_as_null_references()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
Expand Down Expand Up @@ -43,6 +44,14 @@ public ConnectMsSqlCommand(string resolvedToolsServicePath)

var localName = commandLineContext.ParseResult.GetValueForOption(KernelNameOption);

var found = context?.HandlingKernel?.RootKernel.FindKernelByName($"sql-{localName}") is not null;

if (found)
{
throw new InvalidOperationException(
$"A kernel with name {localName} is already present. Use a different value for the {KernelNameOption.Name} option.");
}

var kernel = await connector.CreateKernelAsync(localName);

if (connector.CreateDbContext)
Expand Down