84 lines
4.2 KiB
C#
84 lines
4.2 KiB
C#
// 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<SqliteVectorStore, SqliteCollection<string, DependencyInjectionTests<string>.Record>, string, DependencyInjectionTests<string>.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<IConfiguration>().GetRequiredSection("Sqlite:ConnectionString").Value!;
|
|
|
|
private static string ConnectionStringProvider(IServiceProvider sp, object serviceKey)
|
|
=> sp.GetRequiredService<IConfiguration>().GetRequiredSection(CreateConfigKey("Sqlite", serviceKey, "ConnectionString")).Value!;
|
|
|
|
public override IEnumerable<Func<IServiceCollection, object?, string, ServiceLifetime, IServiceCollection>> CollectionDelegates
|
|
{
|
|
get
|
|
{
|
|
yield return (services, serviceKey, name, lifetime) => serviceKey is null
|
|
? services.AddSqliteCollection<string, Record>(
|
|
name, connectionString: ConnectionString, lifetime: lifetime)
|
|
: services.AddKeyedSqliteCollection<string, Record>(
|
|
serviceKey, name, connectionString: ConnectionString, lifetime: lifetime);
|
|
|
|
yield return (services, serviceKey, name, lifetime) => serviceKey is null
|
|
? services.AddSqliteCollection<string, Record>(
|
|
name, ConnectionStringProvider, lifetime: lifetime)
|
|
: services.AddKeyedSqliteCollection<string, Record>(
|
|
serviceKey, name, sp => ConnectionStringProvider(sp, serviceKey), lifetime: lifetime);
|
|
}
|
|
}
|
|
|
|
public override IEnumerable<Func<IServiceCollection, object?, ServiceLifetime, IServiceCollection>> 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<ArgumentNullException>(() => services.AddSqliteVectorStore(connectionStringProvider: null!));
|
|
Assert.Throws<ArgumentNullException>(() => services.AddKeyedSqliteVectorStore(serviceKey: "notNull", connectionStringProvider: null!));
|
|
Assert.Throws<ArgumentNullException>(() => services.AddSqliteCollection<string, Record>(name: "notNull", connectionStringProvider: null!));
|
|
Assert.Throws<ArgumentNullException>(() => services.AddKeyedSqliteCollection<string, Record>(serviceKey: "notNull", name: "notNull", connectionStringProvider: null!));
|
|
}
|
|
|
|
[Fact]
|
|
public void ConnectionStringCantBeNullOrEmpty()
|
|
{
|
|
IServiceCollection services = new ServiceCollection();
|
|
|
|
Assert.Throws<ArgumentNullException>(() => services.AddSqliteCollection<string, Record>(
|
|
name: "notNull", connectionString: null!));
|
|
Assert.Throws<ArgumentException>(() => services.AddSqliteCollection<string, Record>(
|
|
name: "notNull", connectionString: ""));
|
|
Assert.Throws<ArgumentNullException>(() => services.AddKeyedSqliteCollection<string, Record>(
|
|
serviceKey: "notNull", name: "notNull", connectionString: null!));
|
|
Assert.Throws<ArgumentException>(() => services.AddKeyedSqliteCollection<string, Record>(
|
|
serviceKey: "notNull", name: "notNull", connectionString: ""));
|
|
}
|
|
}
|