// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.SemanticKernel.Connectors.CosmosNoSql;
using Moq;
using Xunit;
namespace SemanticKernel.Connectors.CosmosNoSql.UnitTests;
///
/// Unit tests for class.
///
public sealed class CosmosNoSqlVectorStoreTests
{
private readonly Mock _mockDatabase = new();
public CosmosNoSqlVectorStoreTests()
{
var mockClient = new Mock();
mockClient.Setup(l => l.ClientOptions).Returns(new CosmosClientOptions() { UseSystemTextJsonSerializerWithOptions = JsonSerializerOptions.Default });
this._mockDatabase
.Setup(l => l.Client)
.Returns(mockClient.Object);
}
[Fact]
public void GetCollectionWithNotSupportedKeyThrowsException()
{
// Arrange
using var sut = new Microsoft.SemanticKernel.Connectors.CosmosNoSql.CosmosNoSqlVectorStore(this._mockDatabase.Object);
// Act & Assert
Assert.Throws(() => sut.GetCollection("collection"));
}
[Fact]
public void GetCollectionWithSupportedKeyReturnsCollection()
{
// Arrange
using var sut = new Microsoft.SemanticKernel.Connectors.CosmosNoSql.CosmosNoSqlVectorStore(this._mockDatabase.Object);
// Act
var collection = sut.GetCollection("collection1");
// Assert
Assert.NotNull(collection);
}
[Fact]
public void GetCollectionWithStringKeyReturnsCollection()
{
// Arrange
using var sut = new Microsoft.SemanticKernel.Connectors.CosmosNoSql.CosmosNoSqlVectorStore(this._mockDatabase.Object);
// Act
var collection = sut.GetCollection("collection1");
// Assert
Assert.NotNull(collection);
}
[Fact]
public void GetCollectionWithGuidKeyReturnsCollection()
{
// Arrange
using var sut = new Microsoft.SemanticKernel.Connectors.CosmosNoSql.CosmosNoSqlVectorStore(this._mockDatabase.Object);
// Act
var collection = sut.GetCollection("collection1");
// Assert
Assert.NotNull(collection);
}
[Fact]
public void GetCollectionWithoutFactoryReturnsDefaultCollection()
{
// Arrange
using var sut = new Microsoft.SemanticKernel.Connectors.CosmosNoSql.CosmosNoSqlVectorStore(this._mockDatabase.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 mockFeedResponse = new Mock>();
mockFeedResponse
.Setup(l => l.Resource)
.Returns(expectedCollectionNames);
var mockFeedIterator = new Mock>();
mockFeedIterator
.SetupSequence(l => l.HasMoreResults)
.Returns(true)
.Returns(false);
mockFeedIterator
.Setup(l => l.ReadNextAsync(It.IsAny()))
.ReturnsAsync(mockFeedResponse.Object);
this._mockDatabase
.Setup(l => l.GetContainerQueryIterator(
It.IsAny(),
It.IsAny(),
It.IsAny()))
.Returns(mockFeedIterator.Object);
using var sut = new Microsoft.SemanticKernel.Connectors.CosmosNoSql.CosmosNoSqlVectorStore(this._mockDatabase.Object);
// Act
var actualCollectionNames = await sut.ListCollectionNamesAsync().ToListAsync();
// Assert
Assert.Equal(expectedCollectionNames, actualCollectionNames);
}
#pragma warning disable CA1812
private sealed class GuidKeyHotel
{
[Microsoft.Extensions.VectorData.VectorStoreKey]
public Guid Id { get; set; }
[Microsoft.Extensions.VectorData.VectorStoreData]
public string? Name { get; set; }
}
#pragma warning restore CA1812
}