Skip to content

Commit 482ca5d

Browse files
authored
.Net: Allow the MongoDB VectorData tests to run without an image (#12860)
### Motivation and Context We are running the SK tests in our https://github.com/mongodb-labs/ai-ml-pipeline-testing repo and would like to also be able to run them against live MongoDB Atlas clusters for further verification rather than just the Docker image. ### Description The PR adjusts the vector data tests (only as per @roji recommendation) so that if an environment variable named `MONGODB__CONNECTIONURL` exists then that is used to connect to MongoDB rather than using TestContainers to fire up a Docker image of MongoDB Atlas. Additionally to improve reliability it was found that we need to increase the number of attempts to wait for the data to be ready after creating an index. (Bumped from 20 to 200). We're going to look at adding index status helper methods in the C# Driver going forward for a better experience. Once this is done we should be able to fully enable the new tests on our repo and diagnose any further instabilities and then be able to submit a PR to fully-enable the image-based tests here. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent bb67609 commit 482ca5d

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

dotnet/test/VectorData/MongoDB.ConformanceTests/MongoDB.ConformanceTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.Configuration" />
14+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" />
15+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" />
1316
<PackageReference Include="xunit" />
1417
<PackageReference Include="xunit.runner.visualstudio">
1518
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using Microsoft.Extensions.Configuration;
4+
5+
namespace MongoDB.ConformanceTests.Support;
6+
7+
#pragma warning disable CA1810 // Initialize all static fields when those fields are declared
8+
9+
internal static class MongoTestEnvironment
10+
{
11+
public static readonly string? ConnectionUrl;
12+
13+
public static bool IsConnectionInfoDefined => ConnectionUrl is not null;
14+
15+
static MongoTestEnvironment()
16+
{
17+
var configuration = new ConfigurationBuilder()
18+
.AddJsonFile(path: "testsettings.json", optional: true)
19+
.AddJsonFile(path: "testsettings.development.json", optional: true)
20+
.AddEnvironmentVariables()
21+
.Build();
22+
23+
var mongoSection = configuration.GetSection("MongoDB");
24+
ConnectionUrl = mongoSection["ConnectionURL"];
25+
}
26+
}

dotnet/test/VectorData/MongoDB.ConformanceTests/Support/MongoTestStore.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ internal sealed class MongoTestStore : TestStore
1313
{
1414
public static MongoTestStore Instance { get; } = new();
1515

16-
private readonly MongoDbContainer _container = new MongoDbBuilder()
17-
.WithImage("mongodb/mongodb-atlas-local:7.0.6")
18-
.Build();
16+
private MongoDbContainer? _container;
1917

2018
public MongoClient? _client { get; private set; }
2119
public IMongoDatabase? _database { get; private set; }
@@ -32,21 +30,40 @@ private MongoTestStore()
3230

3331
protected override async Task StartAsync()
3432
{
33+
var clientSettings = MongoTestEnvironment.IsConnectionInfoDefined
34+
? MongoClientSettings.FromConnectionString(MongoTestEnvironment.ConnectionUrl)
35+
: await this.StartMongoDbContainerAsync();
36+
37+
this._client = new MongoClient(clientSettings);
38+
this._database = this._client.GetDatabase("VectorSearchTests");
39+
this.DefaultVectorStore = new MongoVectorStore(this._database);
40+
}
41+
42+
private async Task<MongoClientSettings> StartMongoDbContainerAsync()
43+
{
44+
this._container = new MongoDbBuilder()
45+
.WithImage("mongodb/mongodb-atlas-local:7.0.6")
46+
.Build();
47+
3548
using CancellationTokenSource cts = new();
3649
cts.CancelAfter(TimeSpan.FromSeconds(60));
3750
await this._container.StartAsync(cts.Token);
3851

39-
this._client = new MongoClient(new MongoClientSettings
52+
return new MongoClientSettings
4053
{
4154
Server = new MongoServerAddress(this._container.Hostname, this._container.GetMappedPublicPort(MongoDbBuilder.MongoDbPort)),
4255
DirectConnection = true,
4356
// ReadConcern = ReadConcern.Linearizable,
4457
// WriteConcern = WriteConcern.WMajority
45-
});
46-
this._database = this._client.GetDatabase("VectorSearchTests");
47-
this.DefaultVectorStore = new MongoVectorStore(this._database);
58+
};
4859
}
4960

50-
protected override Task StopAsync()
51-
=> this._container.StopAsync();
61+
protected override async Task StopAsync()
62+
{
63+
if (this._container != null)
64+
{
65+
await this._container.StopAsync();
66+
this._container = null;
67+
}
68+
}
5269
}

dotnet/test/VectorData/VectorData.ConformanceTests/Support/TestStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public virtual async Task WaitForDataAsync<TKey, TRecord>(
9696

9797
var vector = dummyVector ?? new ReadOnlyMemory<float>(Enumerable.Range(0, vectorSize ?? 3).Select(i => (float)i).ToArray());
9898

99-
for (var i = 0; i < 20; i++)
99+
for (var i = 0; i < 200; i++)
100100
{
101101
var results = collection.SearchAsync(
102102
vector,

0 commit comments

Comments
 (0)