Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/Aspire.MySqlConnector.Tests/Aspire.MySqlConnector.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Components\Aspire.MySqlConnector\Aspire.MySqlConnector.csproj" /> | ||
<ProjectReference Include="..\Aspire.Components.Common.Tests\Aspire.Components.Common.Tests.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
104 changes: 104 additions & 0 deletions
104
tests/Aspire.MySqlConnector.Tests/AspireMySqlConnectorExtensionsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using MySqlConnector; | ||
using Xunit; | ||
|
||
namespace Aspire.MySqlConnector.Tests; | ||
|
||
public class AspireMySqlConnectorExtensionsTests | ||
{ | ||
private const string ConnectionString = "Server=localhost;Database=test_aspire_mysql"; | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ReadsFromConnectionStringsCorrectly(bool useKeyed) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>("ConnectionStrings:mysql", ConnectionString) | ||
]); | ||
|
||
if (useKeyed) | ||
{ | ||
builder.AddKeyedMySqlDataSource("mysql"); | ||
} | ||
else | ||
{ | ||
builder.AddMySqlDataSource("mysql"); | ||
} | ||
|
||
var host = builder.Build(); | ||
var dataSource = useKeyed ? | ||
host.Services.GetRequiredKeyedService<MySqlDataSource>("mysql") : | ||
host.Services.GetRequiredService<MySqlDataSource>(); | ||
|
||
Assert.Equal(ConnectionString, dataSource.ConnectionString); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ConnectionStringCanBeSetInCode(bool useKeyed) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>("ConnectionStrings:mysql", "unused") | ||
]); | ||
|
||
static void SetConnectionString(MySqlConnectorSettings settings) => settings.ConnectionString = ConnectionString; | ||
if (useKeyed) | ||
{ | ||
builder.AddKeyedMySqlDataSource("mysql", SetConnectionString); | ||
} | ||
else | ||
{ | ||
builder.AddMySqlDataSource("mysql", SetConnectionString); | ||
} | ||
|
||
var host = builder.Build(); | ||
var dataSource = useKeyed ? | ||
host.Services.GetRequiredKeyedService<MySqlDataSource>("mysql") : | ||
host.Services.GetRequiredService<MySqlDataSource>(); | ||
|
||
Assert.Equal(ConnectionString, dataSource.ConnectionString); | ||
// the connection string from config should not be used since code set it explicitly | ||
Assert.DoesNotContain("unused", dataSource.ConnectionString); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true)] | ||
[InlineData(false)] | ||
public void ConnectionNameWinsOverConfigSection(bool useKeyed) | ||
{ | ||
var builder = Host.CreateEmptyApplicationBuilder(null); | ||
|
||
var key = useKeyed ? "mysql" : null; | ||
builder.Configuration.AddInMemoryCollection([ | ||
new KeyValuePair<string, string?>(ConformanceTests.CreateConfigKey("Aspire:MySqlConnector", key, "ConnectionString"), "unused"), | ||
new KeyValuePair<string, string?>("ConnectionStrings:mysql", ConnectionString) | ||
]); | ||
|
||
if (useKeyed) | ||
{ | ||
builder.AddKeyedMySqlDataSource("mysql"); | ||
} | ||
else | ||
{ | ||
builder.AddMySqlDataSource("mysql"); | ||
} | ||
|
||
var host = builder.Build(); | ||
var dataSource = useKeyed ? | ||
host.Services.GetRequiredKeyedService<MySqlDataSource>("mysql") : | ||
host.Services.GetRequiredService<MySqlDataSource>(); | ||
|
||
Assert.Equal(ConnectionString, dataSource.ConnectionString); | ||
// the connection string from config should not be used since it was found in ConnectionStrings | ||
Assert.DoesNotContain("unused", dataSource.ConnectionString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Xunit; | ||
|
||
namespace Aspire.MySqlConnector.Tests; | ||
|
||
public class ConfigurationTests | ||
{ | ||
[Fact] | ||
public void ConnectionStringIsNullByDefault() | ||
=> Assert.Null(new MySqlConnectorSettings().ConnectionString); | ||
|
||
[Fact] | ||
public void HealthCheckIsEnabledByDefault() | ||
=> Assert.True(new MySqlConnectorSettings().HealthChecks); | ||
|
||
[Fact] | ||
public void TracingIsEnabledByDefault() | ||
=> Assert.True(new MySqlConnectorSettings().Tracing); | ||
|
||
[Fact] | ||
public void MetricsAreEnabledByDefault() | ||
=> Assert.True(new MySqlConnectorSettings().Metrics); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Data.Common; | ||
using Aspire.Components.ConformanceTests; | ||
using Microsoft.DotNet.RemoteExecutor; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using MySqlConnector; | ||
using Xunit; | ||
|
||
namespace Aspire.MySqlConnector.Tests; | ||
|
||
public class ConformanceTests : ConformanceTests<MySqlDataSource, MySqlConnectorSettings> | ||
{ | ||
private const string ConnectionSting = "Host=localhost;Database=test_aspire_mysql;Username=root;Password=password"; | ||
|
||
private static readonly Lazy<bool> s_canConnectToServer = new(GetCanConnect); | ||
|
||
protected override ServiceLifetime ServiceLifetime => ServiceLifetime.Singleton; | ||
|
||
// https://github.com/mysql-net/MySqlConnector/blob/d895afc013a5849d33a123a7061442e2cbb9ce76/src/MySqlConnector/Utilities/ActivitySourceHelper.cs#L61 | ||
protected override string ActivitySourceName => "MySqlConnector"; | ||
|
||
protected override string[] RequiredLogCategories => [ | ||
"MySqlConnector.ConnectionPool", | ||
"MySqlConnector.MySqlBulkCopy", | ||
"MySqlConnector.MySqlCommand", | ||
"MySqlConnector.MySqlConnection", | ||
"MySqlConnector.MySqlDataSource", | ||
]; | ||
|
||
protected override bool SupportsKeyedRegistrations => true; | ||
|
||
protected override bool CanConnectToServer => s_canConnectToServer.Value; | ||
|
||
protected override string JsonSchemaPath => "src/Components/Aspire.MySqlConnector/ConfigurationSchema.json"; | ||
|
||
protected override string ValidJsonConfig => """ | ||
{ | ||
"Aspire": { | ||
"MySqlConnector": { | ||
"Npgsql": { | ||
"ConnectionString": "YOUR_CONNECTION_STRING", | ||
"HealthChecks": false, | ||
"Tracing": true, | ||
"Metrics": true | ||
} | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
protected override (string json, string error)[] InvalidJsonToErrorMessage => new[] | ||
{ | ||
("""{"Aspire": { "MySqlConnector":{ "Metrics": 0}}}""", "Value is \"integer\" but should be \"boolean\""), | ||
("""{"Aspire": { "MySqlConnector":{ "ConnectionString": "Con", "HealthChecks": "false"}}}""", "Value is \"string\" but should be \"boolean\"") | ||
}; | ||
|
||
protected override void PopulateConfiguration(ConfigurationManager configuration, string? key = null) | ||
=> configuration.AddInMemoryCollection(new KeyValuePair<string, string?>[1] | ||
{ | ||
new KeyValuePair<string, string?>(CreateConfigKey("Aspire:MySqlConnector", key, "ConnectionString"), ConnectionSting) | ||
}); | ||
|
||
protected override void RegisterComponent(HostApplicationBuilder builder, Action<MySqlConnectorSettings>? configure = null, string? key = null) | ||
{ | ||
if (key is null) | ||
{ | ||
builder.AddMySqlDataSource("mysql", configure); | ||
} | ||
else | ||
{ | ||
builder.AddKeyedMySqlDataSource(key, configure); | ||
} | ||
} | ||
|
||
protected override void SetHealthCheck(MySqlConnectorSettings options, bool enabled) | ||
=> options.HealthChecks = enabled; | ||
|
||
protected override void SetTracing(MySqlConnectorSettings options, bool enabled) | ||
=> options.Tracing = enabled; | ||
|
||
protected override void SetMetrics(MySqlConnectorSettings options, bool enabled) | ||
=> options.Metrics = enabled; | ||
|
||
protected override void TriggerActivity(MySqlDataSource service) | ||
{ | ||
using MySqlConnection connection = service.CreateConnection(); | ||
connection.Open(); | ||
using MySqlCommand command = connection.CreateCommand(); | ||
command.CommandText = "Select 1;"; | ||
command.ExecuteScalar(); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("key")] | ||
public void BothDataSourceAndConnectionCanBeResolved(string? key) | ||
{ | ||
using IHost host = CreateHostWithComponent(key: key); | ||
|
||
MySqlDataSource? mySqlDataSource = Resolve<MySqlDataSource>(); | ||
DbDataSource? dbDataSource = Resolve<DbDataSource>(); | ||
MySqlConnection? mySqlConnection = Resolve<MySqlConnection>(); | ||
DbConnection? dbConnection = Resolve<DbConnection>(); | ||
|
||
Assert.NotNull(mySqlDataSource); | ||
Assert.Same(mySqlDataSource, dbDataSource); | ||
|
||
Assert.NotNull(mySqlConnection); | ||
Assert.NotNull(dbConnection); | ||
|
||
Assert.Equal(dbConnection.ConnectionString, mySqlConnection.ConnectionString); | ||
Assert.Equal(mySqlDataSource.ConnectionString, mySqlConnection.ConnectionString); | ||
|
||
T? Resolve<T>() => key is null ? host.Services.GetService<T>() : host.Services.GetKeyedService<T>(key); | ||
} | ||
|
||
[ConditionalFact] | ||
public void TracingEnablesTheRightActivitySource() | ||
{ | ||
SkipIfCanNotConnectToServer(); | ||
|
||
RemoteExecutor.Invoke(() => ActivitySourceTest(key: null)).Dispose(); | ||
} | ||
|
||
[ConditionalFact] | ||
public void TracingEnablesTheRightActivitySource_Keyed() | ||
{ | ||
SkipIfCanNotConnectToServer(); | ||
|
||
RemoteExecutor.Invoke(() => ActivitySourceTest(key: "key")).Dispose(); | ||
} | ||
|
||
private static bool GetCanConnect() | ||
{ | ||
using MySqlConnection connection = new(ConnectionSting); | ||
|
||
try | ||
{ | ||
// clear the database from the connection string so we can create it | ||
var builder = new MySqlConnectionStringBuilder(connection.ConnectionString); | ||
string dbName = connection.Database; | ||
builder.Database = null; | ||
|
||
using var noDatabaseConnection = new MySqlConnection(builder.ConnectionString); | ||
|
||
noDatabaseConnection.Open(); | ||
|
||
using var cmd = new MySqlCommand($"CREATE DATABASE IF NOT EXISTS `{dbName}`", noDatabaseConnection); | ||
cmd.ExecuteNonQuery(); | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |