chore: import upstream snapshot with attribution
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 13:21:23 +08:00
commit b957a53def
5423 changed files with 863745 additions and 0 deletions
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
using VectorData.ConformanceTests.Support;
namespace MongoDB.ConformanceTests.Support;
public class MongoFixture : VectorStoreFixture
{
public override TestStore TestStore => MongoTestStore.Instance;
}
@@ -0,0 +1,26 @@
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.Configuration;
namespace MongoDB.ConformanceTests.Support;
#pragma warning disable CA1810 // Initialize all static fields when those fields are declared
internal static class MongoTestEnvironment
{
public static readonly string? ConnectionUrl;
public static bool IsConnectionInfoDefined => ConnectionUrl is not null;
static MongoTestEnvironment()
{
var configuration = new ConfigurationBuilder()
.AddJsonFile(path: "testsettings.json", optional: true)
.AddJsonFile(path: "testsettings.development.json", optional: true)
.AddEnvironmentVariables()
.Build();
var mongoSection = configuration.GetSection("MongoDB");
ConnectionUrl = mongoSection["ConnectionURL"];
}
}
@@ -0,0 +1,98 @@
// Copyright (c) Microsoft. All rights reserved.
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Configurations;
using DotNet.Testcontainers.Containers;
using Microsoft.SemanticKernel.Connectors.MongoDB;
using MongoDB.Bson;
using MongoDB.Driver;
using Testcontainers.MongoDb;
using VectorData.ConformanceTests.Support;
namespace MongoDB.ConformanceTests.Support;
#pragma warning disable CA1001 // Type owns disposable fields but is not disposable
internal sealed class MongoTestStore : TestStore
{
public static MongoTestStore Instance { get; } = new();
private MongoDbContainer? _container;
public MongoClient? _client { get; private set; }
public IMongoDatabase? _database { get; private set; }
public MongoClient Client => this._client ?? throw new InvalidOperationException("Not initialized");
public IMongoDatabase Database => this._database ?? throw new InvalidOperationException("Not initialized");
public MongoVectorStore GetVectorStore(MongoVectorStoreOptions options)
=> new(this.Database, options);
private MongoTestStore()
{
}
protected override async Task StartAsync()
{
var clientSettings = MongoTestEnvironment.IsConnectionInfoDefined
? MongoClientSettings.FromConnectionString(MongoTestEnvironment.ConnectionUrl)
: await this.StartMongoDbContainerAsync();
this._client = new MongoClient(clientSettings);
this._database = this._client.GetDatabase("VectorSearchTests");
this.DefaultVectorStore = new MongoVectorStore(this._database);
}
private async Task<MongoClientSettings> StartMongoDbContainerAsync()
{
this._container = new MongoDbBuilder()
.WithImage("mongodb/mongodb-atlas-local:7.0.6")
.WithWaitStrategy(Wait.ForUnixContainer().AddCustomWaitStrategy(new MongoDbWaitUntil()))
.Build();
using CancellationTokenSource cts = new();
cts.CancelAfter(TimeSpan.FromSeconds(60));
await this._container.StartAsync(cts.Token);
return new MongoClientSettings
{
Server = new MongoServerAddress(this._container.Hostname, this._container.GetMappedPublicPort(MongoDbBuilder.MongoDbPort)),
DirectConnection = true,
// ReadConcern = ReadConcern.Linearizable,
// WriteConcern = WriteConcern.WMajority
};
}
private static readonly string? s_baseObjectId = ObjectId.GenerateNewId().ToString().Substring(0, 14);
public override TKey GenerateKey<TKey>(int value)
{
if (typeof(TKey) == typeof(ObjectId))
{
return (TKey)(object)ObjectId.Parse(s_baseObjectId + value.ToString("0000000000"));
}
return base.GenerateKey<TKey>(value);
}
protected override async Task StopAsync()
{
if (this._container != null)
{
await this._container.StopAsync();
this._container = null;
}
}
private sealed class MongoDbWaitUntil : IWaitUntil
{
/// <inheritdoc />
public async Task<bool> UntilAsync(IContainer container)
{
var (stdout, _) = await container.GetLogsAsync(timestampsEnabled: false)
.ConfigureAwait(false);
return stdout.Contains("\"msg\":\"Waiting for connections\"");
}
}
}