// Copyright (c) Microsoft. All rights reserved. using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel.Connectors.Qdrant; using Qdrant.Client; using VectorData.ConformanceTests; using Xunit; namespace Qdrant.ConformanceTests; public class QdrantDependencyInjectionTests : DependencyInjectionTests.Record>, ulong, DependencyInjectionTests.Record> { private const string Host = "localhost"; private const int Port = 8080; private const string ApiKey = "fakeKey"; protected override void PopulateConfiguration(ConfigurationManager configuration, object? serviceKey = null) => configuration.AddInMemoryCollection( [ new(CreateConfigKey("Qdrant", serviceKey, "Host"), Host), new(CreateConfigKey("Qdrant", serviceKey, "Port"), Port.ToString()), new(CreateConfigKey("Qdrant", serviceKey, "ApiKey"), ApiKey), ]); private static string HostProvider(IServiceProvider sp, object? serviceKey = null) => sp.GetRequiredService().GetRequiredSection(CreateConfigKey("Qdrant", serviceKey, "Host")).Value!; private static int PortProvider(IServiceProvider sp, object? serviceKey = null) => int.Parse(sp.GetRequiredService().GetRequiredSection(CreateConfigKey("Qdrant", serviceKey, "Port")).Value!); private static string ApiKeyProvider(IServiceProvider sp, object? serviceKey = null) => sp.GetRequiredService().GetRequiredSection(CreateConfigKey("Qdrant", serviceKey, "ApiKey")).Value!; public override IEnumerable> CollectionDelegates { get { yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services .AddSingleton(sp => new QdrantClient(Host, Port, apiKey: ApiKey)) .AddQdrantCollection(name, lifetime: lifetime) : services .AddSingleton(sp => new QdrantClient(Host, Port, apiKey: ApiKey)) .AddKeyedQdrantCollection(serviceKey, name, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddQdrantCollection( name, Host, Port, apiKey: ApiKey, lifetime: lifetime) : services.AddKeyedQdrantCollection( serviceKey, name, Host, Port, apiKey: ApiKey, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddQdrantCollection( name, sp => new QdrantClient(HostProvider(sp), PortProvider(sp), apiKey: ApiKeyProvider(sp)), lifetime: lifetime) : services.AddKeyedQdrantCollection( serviceKey, name, sp => new QdrantClient(HostProvider(sp, serviceKey), PortProvider(sp, serviceKey), apiKey: ApiKeyProvider(sp, serviceKey)), lifetime: lifetime); } } public override IEnumerable> StoreDelegates { get { yield return (services, serviceKey, lifetime) => serviceKey is null ? services.AddQdrantVectorStore( Host, Port, apiKey: ApiKey, lifetime: lifetime) : services.AddKeyedQdrantVectorStore( serviceKey, Host, Port, apiKey: ApiKey, lifetime: lifetime); yield return (services, serviceKey, lifetime) => serviceKey is null ? services .AddSingleton(sp => new QdrantClient(Host, Port, apiKey: ApiKey)) .AddQdrantVectorStore(lifetime: lifetime) : services .AddSingleton(sp => new QdrantClient(Host, Port, apiKey: ApiKey)) .AddKeyedQdrantVectorStore(serviceKey, lifetime: lifetime); } } [Fact] public void HostCantBeNullOrEmpty() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddQdrantVectorStore(host: null!)); Assert.Throws(() => services.AddKeyedQdrantVectorStore(serviceKey: "notNull", host: null!)); Assert.Throws(() => services.AddQdrantCollection( name: "notNull", host: null!)); Assert.Throws(() => services.AddQdrantCollection( name: "notNull", host: "")); Assert.Throws(() => services.AddKeyedQdrantCollection( serviceKey: "notNull", name: "notNull", host: null!)); Assert.Throws(() => services.AddKeyedQdrantCollection( serviceKey: "notNull", name: "notNull", host: "")); } }