// Copyright (c) Microsoft. All rights reserved. using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel.Connectors.SqliteVec; using VectorData.ConformanceTests; using Xunit; namespace SqliteVec.ConformanceTests; public class SqliteDependencyInjectionTests : DependencyInjectionTests.Record>, string, DependencyInjectionTests.Record> { protected const string ConnectionString = "Data Source=:memory:"; protected override void PopulateConfiguration(ConfigurationManager configuration, object? serviceKey = null) => configuration.AddInMemoryCollection( [ new(CreateConfigKey("Sqlite", serviceKey, "ConnectionString"), ConnectionString), ]); private static string ConnectionStringProvider(IServiceProvider sp) => sp.GetRequiredService().GetRequiredSection("Sqlite:ConnectionString").Value!; private static string ConnectionStringProvider(IServiceProvider sp, object serviceKey) => sp.GetRequiredService().GetRequiredSection(CreateConfigKey("Sqlite", serviceKey, "ConnectionString")).Value!; public override IEnumerable> CollectionDelegates { get { yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddSqliteCollection( name, connectionString: ConnectionString, lifetime: lifetime) : services.AddKeyedSqliteCollection( serviceKey, name, connectionString: ConnectionString, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddSqliteCollection( name, ConnectionStringProvider, lifetime: lifetime) : services.AddKeyedSqliteCollection( serviceKey, name, sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime); } } public override IEnumerable> StoreDelegates { get { yield return (services, serviceKey, lifetime) => serviceKey is null ? services.AddSqliteVectorStore( ConnectionStringProvider, lifetime: lifetime) : services.AddKeyedSqliteVectorStore( serviceKey, sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime); } } [Fact] public void ConnectionStringProviderCantBeNull() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddSqliteVectorStore(connectionStringProvider: null!)); Assert.Throws(() => services.AddKeyedSqliteVectorStore(serviceKey: "notNull", connectionStringProvider: null!)); Assert.Throws(() => services.AddSqliteCollection(name: "notNull", connectionStringProvider: null!)); Assert.Throws(() => services.AddKeyedSqliteCollection(serviceKey: "notNull", name: "notNull", connectionStringProvider: null!)); } [Fact] public void ConnectionStringCantBeNullOrEmpty() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddSqliteCollection( name: "notNull", connectionString: null!)); Assert.Throws(() => services.AddSqliteCollection( name: "notNull", connectionString: "")); Assert.Throws(() => services.AddKeyedSqliteCollection( serviceKey: "notNull", name: "notNull", connectionString: null!)); Assert.Throws(() => services.AddKeyedSqliteCollection( serviceKey: "notNull", name: "notNull", connectionString: "")); } }