|
| 1 | +namespace Testcontainers.Tests; |
| 2 | + |
| 3 | +public sealed class ContainerConnectionStringProviderTests |
| 4 | +{ |
| 5 | + [Fact] |
| 6 | + public void ConfigureStoresContainerAndConfiguration() |
| 7 | + { |
| 8 | + var container = new Mock<IContainer>(); |
| 9 | + var configuration = new Mock<IContainerConfiguration>(); |
| 10 | + var provider = new InspectableProvider(); |
| 11 | + |
| 12 | + provider.Configure(container.Object, configuration.Object); |
| 13 | + |
| 14 | + Assert.Same(container.Object, provider.ConfiguredContainer); |
| 15 | + Assert.Same(configuration.Object, provider.ConfiguredConfiguration); |
| 16 | + } |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void GetConnectionStringReturnsHostConnectionString() |
| 20 | + { |
| 21 | + var provider = new HostOnlyProvider("host-connection"); |
| 22 | + |
| 23 | + var connectionString = provider.GetConnectionString(); |
| 24 | + |
| 25 | + Assert.Equal("host-connection", connectionString); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void GetConnectionStringWithEmptyNameReturnsHostConnectionString() |
| 30 | + { |
| 31 | + var provider = new HostOnlyProvider("host-connection"); |
| 32 | + |
| 33 | + var connectionString = provider.GetConnectionString(string.Empty); |
| 34 | + |
| 35 | + Assert.Equal("host-connection", connectionString); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public void GetConnectionStringThrowsWhenHostConnectionStringIsMissing() |
| 40 | + { |
| 41 | + var provider = new HostOnlyProvider(string.Empty); |
| 42 | + |
| 43 | + Assert.Throws<ConnectionStringNotAvailableException>(() => provider.GetConnectionString()); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void GetConnectionStringThrowsWhenContainerModeIsNotSupported() |
| 48 | + { |
| 49 | + var provider = new HostOnlyProvider("host-connection"); |
| 50 | + |
| 51 | + Assert.Throws<ConnectionStringModeNotSupportedException>(() => provider.GetConnectionString(ConnectionMode.Container)); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void GetConnectionStringReturnsContainerConnectionStringWhenSupported() |
| 56 | + { |
| 57 | + var provider = new HostAndContainerProvider("host-connection", "container-connection"); |
| 58 | + |
| 59 | + var connectionString = provider.GetConnectionString(ConnectionMode.Container); |
| 60 | + |
| 61 | + Assert.Equal("container-connection", connectionString); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void GetConnectionStringThrowsWhenNamedConnectionStringIsRequested() |
| 66 | + { |
| 67 | + var provider = new HostOnlyProvider("host-connection"); |
| 68 | + |
| 69 | + Assert.Throws<ConnectionStringNameNotSupportedException>(() => provider.GetConnectionString("primary")); |
| 70 | + } |
| 71 | + |
| 72 | + private sealed class InspectableProvider : ContainerConnectionStringProvider<IContainer, IContainerConfiguration> |
| 73 | + { |
| 74 | + public IContainer ConfiguredContainer => Container; |
| 75 | + |
| 76 | + public IContainerConfiguration ConfiguredConfiguration => Configuration; |
| 77 | + |
| 78 | + protected override string GetHostConnectionString() |
| 79 | + { |
| 80 | + return "host"; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private sealed class HostOnlyProvider : ContainerConnectionStringProvider<IContainer, IContainerConfiguration> |
| 85 | + { |
| 86 | + private readonly string _hostConnectionString; |
| 87 | + |
| 88 | + public HostOnlyProvider(string hostConnectionString) |
| 89 | + { |
| 90 | + _hostConnectionString = hostConnectionString; |
| 91 | + } |
| 92 | + |
| 93 | + protected override string GetHostConnectionString() |
| 94 | + { |
| 95 | + return _hostConnectionString; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private sealed class HostAndContainerProvider : ContainerConnectionStringProvider<IContainer, IContainerConfiguration> |
| 100 | + { |
| 101 | + private readonly string _hostConnectionString; |
| 102 | + |
| 103 | + private readonly string _containerConnectionString; |
| 104 | + |
| 105 | + public HostAndContainerProvider(string hostConnectionString, string containerConnectionString) |
| 106 | + { |
| 107 | + _hostConnectionString = hostConnectionString; |
| 108 | + _containerConnectionString = containerConnectionString; |
| 109 | + } |
| 110 | + |
| 111 | + protected override string GetHostConnectionString() |
| 112 | + { |
| 113 | + return _hostConnectionString; |
| 114 | + } |
| 115 | + |
| 116 | + protected override string GetContainerConnectionString() |
| 117 | + { |
| 118 | + return _containerConnectionString; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments