// 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; /// /// Unit tests for class. /// public sealed class MongoVectorStoreTests { private readonly Mock _mockMongoDatabase = new(); [Fact] public void GetCollectionWithNotSupportedKeyThrowsException() { // Arrange using var sut = new MongoVectorStore(this._mockMongoDatabase.Object); // Act & Assert Assert.Throws(() => sut.GetCollection("collection")); } [Fact] public void GetCollectionWithoutFactoryReturnsDefaultCollection() { // Arrange using var sut = new MongoVectorStore(this._mockMongoDatabase.Object); // Act var collection = sut.GetCollection("collection"); // Assert Assert.NotNull(collection); } [Fact] public async Task ListCollectionNamesReturnsCollectionNamesAsync() { // Arrange var expectedCollectionNames = new List { "collection-1", "collection-2", "collection-3" }; var mockCursor = new Mock>(); mockCursor .SetupSequence(l => l.MoveNextAsync(It.IsAny())) .ReturnsAsync(true) .ReturnsAsync(false); mockCursor .Setup(l => l.Current) .Returns(expectedCollectionNames); this._mockMongoDatabase .Setup(l => l.ListCollectionNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(mockCursor.Object); using var sut = new MongoVectorStore(this._mockMongoDatabase.Object); // Act var actualCollectionNames = await sut.ListCollectionNamesAsync().ToListAsync(); // Assert Assert.Equal(expectedCollectionNames, actualCollectionNames); } }