// Copyright (c) Microsoft. All rights reserved. using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel.Connectors.PgVector; using VectorData.ConformanceTests; using Xunit; namespace PgVector.ConformanceTests; public class PostgresDependencyInjectionTests : DependencyInjectionTests.Record>, string, DependencyInjectionTests.Record> { protected const string ConnectionString = "Host=localhost;Database=test;"; protected override void PopulateConfiguration(ConfigurationManager configuration, object? serviceKey = null) => configuration.AddInMemoryCollection( [ new(CreateConfigKey("Postgres", serviceKey, "ConnectionString"), ConnectionString), ]); private static string ConnectionStringProvider(IServiceProvider sp) => sp.GetRequiredService().GetRequiredSection("Postgres:ConnectionString").Value!; private static string ConnectionStringProvider(IServiceProvider sp, object serviceKey) => sp.GetRequiredService().GetRequiredSection(CreateConfigKey("Postgres", serviceKey, "ConnectionString")).Value!; public override IEnumerable> CollectionDelegates { get { yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddPostgresCollection( name, connectionString: ConnectionString, lifetime: lifetime) : services.AddKeyedPostgresCollection( serviceKey, name, connectionString: ConnectionString, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddPostgresCollection( name, ConnectionStringProvider, lifetime: lifetime) : services.AddKeyedPostgresCollection( serviceKey, name, sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime); } } public override IEnumerable> StoreDelegates { get { yield return (services, serviceKey, lifetime) => serviceKey is null ? services.AddPostgresVectorStore( ConnectionString, lifetime: lifetime) : services.AddKeyedPostgresVectorStore( serviceKey, ConnectionString, lifetime: lifetime); yield return (services, serviceKey, lifetime) => serviceKey is null ? services.AddPostgresVectorStore( connectionStringProvider: ConnectionStringProvider, lifetime: lifetime) : services.AddKeyedPostgresVectorStore( serviceKey, connectionStringProvider: sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime); } } [Fact] public void ConnectionStringProviderCantBeNull() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddPostgresCollection(name: "notNull", connectionStringProvider: null!)); Assert.Throws(() => services.AddKeyedPostgresCollection(serviceKey: "notNull", name: "notNull", connectionStringProvider: null!)); } [Fact] public void ConnectionStringCantBeNullOrEmpty() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddPostgresVectorStore(connectionString: null!)); Assert.Throws(() => services.AddKeyedPostgresVectorStore(serviceKey: "notNull", connectionString: null!)); Assert.Throws(() => services.AddPostgresCollection( name: "notNull", connectionString: null!)); Assert.Throws(() => services.AddPostgresCollection( name: "notNull", connectionString: "")); Assert.Throws(() => services.AddKeyedPostgresCollection( serviceKey: "notNull", name: "notNull", connectionString: null!)); Assert.Throws(() => services.AddKeyedPostgresCollection( serviceKey: "notNull", name: "notNull", connectionString: "")); } }