Files
microsoft--semantic-kernel/dotnet/test/VectorData/MongoDB.UnitTests/MongoVectorStoreTests.cs
T
wehub-resource-sync b957a53def
CodeQL / Analyze (csharp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:21:23 +08:00

74 lines
2.2 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Connectors.MongoDB;
using MongoDB.Driver;
using Moq;
using Xunit;
namespace SemanticKernel.Connectors.MongoDB.UnitTests;
/// <summary>
/// Unit tests for <see cref="MongoVectorStore"/> class.
/// </summary>
public sealed class MongoVectorStoreTests
{
private readonly Mock<IMongoDatabase> _mockMongoDatabase = new();
[Fact]
public void GetCollectionWithNotSupportedKeyThrowsException()
{
// Arrange
using var sut = new MongoVectorStore(this._mockMongoDatabase.Object);
// Act & Assert
Assert.Throws<NotSupportedException>(() => sut.GetCollection<byte[], MongoHotelModel>("collection"));
}
[Fact]
public void GetCollectionWithoutFactoryReturnsDefaultCollection()
{
// Arrange
using var sut = new MongoVectorStore(this._mockMongoDatabase.Object);
// Act
var collection = sut.GetCollection<string, MongoHotelModel>("collection");
// Assert
Assert.NotNull(collection);
}
[Fact]
public async Task ListCollectionNamesReturnsCollectionNamesAsync()
{
// Arrange
var expectedCollectionNames = new List<string> { "collection-1", "collection-2", "collection-3" };
var mockCursor = new Mock<IAsyncCursor<string>>();
mockCursor
.SetupSequence(l => l.MoveNextAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(true)
.ReturnsAsync(false);
mockCursor
.Setup(l => l.Current)
.Returns(expectedCollectionNames);
this._mockMongoDatabase
.Setup(l => l.ListCollectionNamesAsync(It.IsAny<ListCollectionNamesOptions>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(mockCursor.Object);
using var sut = new MongoVectorStore(this._mockMongoDatabase.Object);
// Act
var actualCollectionNames = await sut.ListCollectionNamesAsync().ToListAsync();
// Assert
Assert.Equal(expectedCollectionNames, actualCollectionNames);
}
}