// Copyright (c) Microsoft. All rights reserved. using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel.Connectors.Redis; using Redis.ConformanceTests.Support; using StackExchange.Redis; using VectorData.ConformanceTests; using Xunit; namespace Redis.ConformanceTests; public class RedisHashSetDependencyInjectionTests : DependencyInjectionTests.Record>, string, DependencyInjectionTests.Record> { private const string ConnectionConfiguration = "localhost:6379"; protected override void PopulateConfiguration(ConfigurationManager configuration, object? serviceKey = null) => configuration.AddInMemoryCollection( [ new(CreateConfigKey("RedisHashSet", serviceKey, "Configuration"), ConnectionConfiguration), ]); private static string Provider(IServiceProvider sp, object? serviceKey = null) => sp.GetRequiredService().GetRequiredSection(CreateConfigKey("RedisHashSet", serviceKey, "Configuration")).Value!; public override IEnumerable> CollectionDelegates { get { yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services .AddRedisHashSetCollection(name, sp => new FakeDatabase(Provider(sp)), lifetime: lifetime) : services .AddKeyedRedisHashSetCollection(serviceKey, name, sp => new FakeDatabase(Provider(sp, serviceKey)), lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services .AddSingleton(new FakeDatabase(ConnectionConfiguration)) .AddRedisHashSetCollection(name, lifetime: lifetime) : services .AddSingleton(new FakeDatabase(ConnectionConfiguration)) .AddKeyedRedisHashSetCollection(serviceKey, name, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => services .AddKeyedSingleton(serviceKey, new FakeDatabase(ConnectionConfiguration)) .AddKeyedRedisHashSetCollection(serviceKey, name, sp => sp.GetRequiredKeyedService(serviceKey), lifetime: lifetime); } } public override IEnumerable> StoreDelegates { get { yield return (services, serviceKey, lifetime) => serviceKey is null ? services .AddSingleton(new FakeDatabase(ConnectionConfiguration)) .AddRedisVectorStore(lifetime: lifetime) : services .AddSingleton(new FakeDatabase(ConnectionConfiguration)) .AddKeyedRedisVectorStore(serviceKey, lifetime: lifetime); } } [Fact] public void ConnectionConfigurationCantBeNullOrEmpty() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddRedisVectorStore(connectionConfiguration: null!)); Assert.Throws(() => services.AddRedisVectorStore(connectionConfiguration: "")); Assert.Throws(() => services.AddKeyedRedisVectorStore("serviceKey", connectionConfiguration: null!)); Assert.Throws(() => services.AddKeyedRedisVectorStore("serviceKey", connectionConfiguration: "")); Assert.Throws(() => services.AddRedisHashSetCollection( name: "notNull", connectionConfiguration: null!)); Assert.Throws(() => services.AddRedisHashSetCollection( name: "notNull", connectionConfiguration: "")); } }