Skip to content

Commit

Permalink
WIP: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timheuer committed May 15, 2024
1 parent 459224c commit 0a3cbec
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.Milvus/MilvusServerResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public MilvusServerResource(string name, ParameterResource apiKey) : base(name)
private EndpointReference? _primaryEndpoint;

/// <summary>
/// Gets the parameter that contains the Qdrant API key.
/// Gets the parameter that contains the Milvus API key.
/// </summary>
public ParameterResource ApiKeyParameter { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Extensions.Hosting;
/// </summary>
public static class AspireMilvusExtensions
{
private const string DefaultConfigSectionName = "Aspire:MilvusMilvus:Client";
private const string DefaultConfigSectionName = "Aspire:Milvus:Client";

/// <summary>
/// Registers <see cref="MilvusClient" /> as a singleton in the services provided by the <paramref name="builder"/>.
Expand Down
110 changes: 110 additions & 0 deletions tests/Aspire.Milvus.Client.Tests/AspireMilvusExtensionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Components.Common.Tests;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Milvus.Client;
using Xunit;

namespace Aspire.Milvus.Client.Tests;
public class AspireMilvusExtensionTests : IClassFixture<MilvusContainerFixture>
{
private readonly MilvusContainerFixture _containerFixture;

private string ConnectionString => RequiresDockerTheoryAttribute.IsSupported
? _containerFixture.GetConnectionString()
: "localhost:19530";

private string NormalizedConnectionString => ConnectionString;

public AspireMilvusExtensionTests(MilvusContainerFixture containerFixture)
=> _containerFixture = containerFixture;

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ReadsFromConnectionStringsCorrectly(bool useKeyed)
{
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Configuration.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string?>("ConnectionStrings:milvus", ConnectionString)
});

if (useKeyed)
{
builder.AddKeyedMilvusClient("milvus");
}
else
{
builder.AddMilvusClient("milvus");
}

using var host = builder.Build();
var dataSource = useKeyed ?
host.Services.GetRequiredKeyedService<MilvusClient>("milvus") :
host.Services.GetRequiredService<MilvusClient>();

Assert.Equal(NormalizedConnectionString, $"http://{dataSource.Address}/");
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ConnectionStringCanBeSetInCode(bool useKeyed)
{
var builder = Host.CreateEmptyApplicationBuilder(null);

void SetConnectionString(MilvusClientSettings settings) => settings.Endpoint = new Uri(ConnectionString);

if (useKeyed)
{
builder.AddKeyedMilvusClient("milvus", SetConnectionString);
}
else
{
builder.AddMilvusClient("milvus", SetConnectionString);
}

using var host = builder.Build();
var dataSource = useKeyed ?
host.Services.GetRequiredKeyedService<MilvusClient>("milvus") :
host.Services.GetRequiredService<MilvusClient>();

Assert.Equal(NormalizedConnectionString, $"http://{dataSource.Address}/");
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ConnectionNameWinsOverConfigSection(bool useKeyed)
{
var builder = Host.CreateEmptyApplicationBuilder(null);

var key = useKeyed ? "milvus" : null;
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>(ConformanceTests.CreateConfigKey("Aspire:Milvus:Client", key, "Endpoint"), "unused"),
new KeyValuePair<string, string?>("ConnectionStrings:milvus", ConnectionString)
]);

if (useKeyed)
{
builder.AddKeyedMilvusClient("milvus");
}
else
{
builder.AddMilvusClient("milvus");
}

using var host = builder.Build();
var connection = useKeyed ?
host.Services.GetRequiredKeyedService<MilvusClient>("milvus") :
host.Services.GetRequiredService<MilvusClient>();

Assert.Equal(ConnectionString, $"http://{connection.Address}/");
// the connection string from config should not be used since it was found in ConnectionStrings
Assert.DoesNotContain("unused", connection.Address);
}
}
4 changes: 3 additions & 1 deletion tests/Aspire.Milvus.Client.Tests/ConformanceTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Components.Common.Tests;
using Aspire.Components.ConformanceTests;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -16,7 +17,7 @@ public class ConformanceTests : ConformanceTests<MilvusClient, MilvusClientSetti

protected override bool CanCreateClientWithoutConnectingToServer => false;

protected override bool CanConnectToServer => AspireMilvusHelpers.CanConnectToServer;
protected override bool CanConnectToServer => RequiresDockerTheoryAttribute.IsSupported;

protected override ServiceLifetime ServiceLifetime => ServiceLifetime.Singleton;

Expand Down Expand Up @@ -45,6 +46,7 @@ protected override void PopulateConfiguration(ConfigurationManager configuration

protected override void TriggerActivity(MilvusClient service)
{
service.GetVersionAsync().Wait();
}

protected override void SetHealthCheck(MilvusClientSettings options, bool enabled) => throw new NotImplementedException();
Expand Down
1 change: 1 addition & 0 deletions tests/Aspire.Milvus.Client.Tests/MilvusContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public async Task InitializeAsync()
{
Container = new MilvusBuilder()
.WithImage($"{MilvusContainerImageTags.Image}:{MilvusContainerImageTags.Tag}")
.WithPortBinding(19530)
.Build();
await Container.StartAsync();
}
Expand Down

0 comments on commit 0a3cbec

Please sign in to comment.