|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | +using SqlStreamStore; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Config.SqlStreamStore.Tests |
| 9 | +{ |
| 10 | + |
| 11 | + public class StreamStoreConfigurationProviderTests |
| 12 | + { |
| 13 | + [Fact] |
| 14 | + public async Task Can_build_config_source_with_instance_from_lambda() |
| 15 | + { |
| 16 | + var instance = await BuildSteamStoreWithSettings(("setting1", "value1")); |
| 17 | + |
| 18 | + var config = new ConfigurationBuilder() |
| 19 | + .Add(new StreamStoreConfigurationSource(() => instance)) |
| 20 | + .Build(); |
| 21 | + |
| 22 | + Assert.Equal("value1", config.GetValue<string>("setting1")); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task Can_build_config_source_from_inner_config() |
| 28 | + { |
| 29 | + const string connectionStringKey = "Config.SqlStreamStore.ConnectionString"; |
| 30 | + const string expectedConnectionString = "not really a conection string, but as good as it gets"; |
| 31 | + |
| 32 | + var instance = await BuildSteamStoreWithSettings(("setting1", "value1")); |
| 33 | + |
| 34 | + var config = new ConfigurationBuilder() |
| 35 | + .AddInMemoryCollection(new Dictionary<string, string> |
| 36 | + { |
| 37 | + { connectionStringKey , expectedConnectionString} |
| 38 | + }) |
| 39 | + .Add(new StreamStoreConfigurationSource( |
| 40 | + (innerConfig) => |
| 41 | + { |
| 42 | + Assert.Equal(expectedConnectionString, innerConfig[connectionStringKey]); |
| 43 | + return instance; |
| 44 | + })) |
| 45 | + .Build(); |
| 46 | + |
| 47 | + |
| 48 | + // Ensure setting 1was read from stream store |
| 49 | + Assert.Equal("value1", config.GetValue<string>("setting1")); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task Can_build_config_source_with_connection_string() |
| 54 | + { |
| 55 | + const string connectionStringKey = "Config.SqlStreamStore.ConnectionString"; |
| 56 | + const string expectedConnectionString = "not really a conection string, but as good as it gets"; |
| 57 | + |
| 58 | + // Create a SSS instance with some settings |
| 59 | + var instance = await BuildSteamStoreWithSettings(("setting1", "value1")); |
| 60 | + |
| 61 | + |
| 62 | + // Set up the factory to capture the used connection string when used |
| 63 | + string capturedConnectionString = null; |
| 64 | + IStreamStore FakeStreamStoreFactory(string providedConnectionString) |
| 65 | + { |
| 66 | + capturedConnectionString = providedConnectionString; |
| 67 | + return instance; |
| 68 | + } |
| 69 | + |
| 70 | + // Build the config with deafult values |
| 71 | + var config = new ConfigurationBuilder() |
| 72 | + // Setup default values |
| 73 | + .AddInMemoryCollection(new Dictionary<string, string> |
| 74 | + { |
| 75 | + { connectionStringKey , expectedConnectionString} |
| 76 | + }) |
| 77 | + |
| 78 | + // Setup Stream store implementation |
| 79 | + .Add(new StreamStoreConfigurationSource( |
| 80 | + connectionStringKey: connectionStringKey, |
| 81 | + streamStoreFactory: FakeStreamStoreFactory)) |
| 82 | + .Build(); |
| 83 | + |
| 84 | + // Ensure the factory actually used the configured connection string key |
| 85 | + Assert.Equal(expectedConnectionString, capturedConnectionString); |
| 86 | + |
| 87 | + // Ensure setting 1was read from stream store |
| 88 | + Assert.Equal("value1", config.GetValue<string>("setting1")); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task Can_reload_when_data_chagnes() |
| 93 | + { |
| 94 | + var instance = await BuildSteamStoreWithSettings(("setting1", "value1")); |
| 95 | + |
| 96 | + var config = new ConfigurationBuilder() |
| 97 | + .Add(new StreamStoreConfigurationSource(() => instance)) |
| 98 | + .Build(); |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + private static async Task<InMemoryStreamStore> BuildSteamStoreWithSettings(params (string key, string value)[] settings) |
| 105 | + { |
| 106 | + var instance = new InMemoryStreamStore(); |
| 107 | + |
| 108 | + var repo = new ConfigRepository(instance); |
| 109 | + await repo.WriteChanges(new ModifiedConfigurationSettings( |
| 110 | + settings), CancellationToken.None); |
| 111 | + |
| 112 | + return instance; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments