using Microsoft.Extensions.Configuration; namespace ZB.MOM.WW.ScadaBridge.Host.Tests; public class InboundApiKeyStoreOptionsTests { private static IConfiguration BuildConfig(Dictionary values) { return new ConfigurationBuilder() .AddInMemoryCollection(values) .Build(); } [Fact] public void Provider_DefaultsToSqlite() { Assert.Equal(InboundApiKeyStoreProvider.Sqlite, new InboundApiKeyStoreOptions().Provider); var configuration = new ConfigurationBuilder().Build(); Assert.Equal(InboundApiKeyStoreProvider.Sqlite, InboundApiKeyStoreOptions.ReadProvider(configuration)); } [Theory] [InlineData("sqlserver", InboundApiKeyStoreProvider.SqlServer)] [InlineData("SQLSERVER", InboundApiKeyStoreProvider.SqlServer)] [InlineData("SqlServer", InboundApiKeyStoreProvider.SqlServer)] [InlineData("sqlite", InboundApiKeyStoreProvider.Sqlite)] public void Provider_ParsesCaseInsensitively(string value, InboundApiKeyStoreProvider expected) { var configuration = BuildConfig(new Dictionary { [$"{InboundApiKeyStoreOptions.SectionPath}:Provider"] = value, }); Assert.Equal(expected, InboundApiKeyStoreOptions.ReadProvider(configuration)); } [Theory] [InlineData("Postgres")] [InlineData("7")] public void Provider_RejectsUnknownValue(string value) { var configuration = BuildConfig(new Dictionary { [$"{InboundApiKeyStoreOptions.SectionPath}:Provider"] = value, }); var ex = Assert.Throws(() => InboundApiKeyStoreOptions.ReadProvider(configuration)); Assert.Contains("Sqlite", ex.Message); Assert.Contains("SqlServer", ex.Message); } [Fact] public void Binds_ImportFromSqlitePath() { var configuration = BuildConfig(new Dictionary { [$"{InboundApiKeyStoreOptions.SectionPath}:Provider"] = "SqlServer", [$"{InboundApiKeyStoreOptions.SectionPath}:ImportFromSqlitePath"] = "/app/data/inbound-api-keys.db", }); var options = configuration.GetSection(InboundApiKeyStoreOptions.SectionPath).Get(); Assert.NotNull(options); Assert.Equal(InboundApiKeyStoreProvider.SqlServer, options!.Provider); Assert.Equal("/app/data/inbound-api-keys.db", options.ImportFromSqlitePath); } }