Esempio n. 1
0
        public void Configure_ServiceInfoOveridesConfig_ReturnsExpected()
        {
            var config = new PostgresProviderConnectorOptions()
            {
                Host     = "localhost",
                Port     = 1234,
                Username = "******",
                Password = "******",
                Database = "database"
            };

            var configurer = new PostgresProviderConfigurer();
            var si         = new PostgresServiceInfo("MyId", "postgres://*****:*****@192.168.0.90:5432/cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355");

            var opts = configurer.Configure(si, config);

            Assert.Contains("Host=192.168.0.90;", opts);
            Assert.Contains("Port=5432;", opts);
            Assert.Contains("Username=Dd6O1BPXUHdrmzbP;", opts);
            Assert.Contains("Password=7E1LxXnlH2hhlPVt;", opts);
            Assert.Contains("Database=cf_b4f8d2fa_a3ea_4e3a_a0e8_2cd040790355;", opts);
        }
Esempio n. 2
0
        /// <summary>
        /// Adds NpgsqlConnection (as IDbConnection and NpgsqlConnection) to your Autofac Container
        /// </summary>
        /// <param name="container">Your Autofac Container Builder</param>
        /// <param name="config">Application configuration</param>
        /// <param name="serviceName">Cloud Foundry service name binding</param>
        /// <returns>the RegistrationBuilder for (optional) additional configuration</returns>
        public static IRegistrationBuilder <object, SimpleActivatorData, SingleRegistrationStyle> RegisterPostgreSqlConnection(this ContainerBuilder container, IConfiguration config, string serviceName = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            PostgresServiceInfo info = serviceName == null
                ? config.GetSingletonServiceInfo <PostgresServiceInfo>()
                : config.GetRequiredServiceInfo <PostgresServiceInfo>(serviceName);

            Type postgreSqlConnection = PostgreSqlTypeLocator.NpgsqlConnection;
            var  postgreSqlConfig     = new PostgresProviderConnectorOptions(config);
            var  factory = new PostgresProviderConnectorFactory(info, postgreSqlConfig, postgreSqlConnection);

            container.RegisterType <RelationalHealthContributor>().As <IHealthContributor>();
            return(container.Register(c => factory.Create(null)).As(typeof(IDbConnection), postgreSqlConnection));
        }
        public void Constructor_BindsValues()
        {
            var appsettings = new Dictionary <string, string>()
            {
                ["postgres:client:host"]     = "localhost",
                ["postgres:client:port"]     = "1234",
                ["postgres:client:password"] = "******",
                ["postgres:client:username"] = "******"
            };

            ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(appsettings);
            var config = configurationBuilder.Build();

            var sconfig = new PostgresProviderConnectorOptions(config);

            Assert.Equal("localhost", sconfig.Host);
            Assert.Equal(1234, sconfig.Port);
            Assert.Equal("password", sconfig.Password);
            Assert.Equal("username", sconfig.Username);
            Assert.Null(sconfig.ConnectionString);
        }
Esempio n. 4
0
        public void ConnectionString_Returned_BuildFromConfig()
        {
            // arrange
            var appsettings = new Dictionary <string, string>()
            {
                ["postgres:client:Host"]       = "fake-db.host",
                ["postgres:client:Port"]       = "3000",
                ["postgres:client:Username"]   = "******",
                ["postgres:client:Password"]   = "******",
                ["postgres:client:Database"]   = "fakeDB",
                ["postgres:client:SearchPath"] = "fakeSchema",
            };
            var expected             = "Host=fake-db.host;Port=3000;Username=fakeUsername;Password=fakePassword;Database=fakeDB;Search Path=fakeSchema;";
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(appsettings);
            var config = configurationBuilder.Build();

            // act
            var sconfig = new PostgresProviderConnectorOptions(config);

            // assert
            Assert.Equal(expected, sconfig.ToString());
        }
        public void ConnectionString_Overridden_By_CloudFoundryConfig()
        {
            // simulate an appsettings file
            var appsettings = new Dictionary <string, string>()
            {
                ["postgres:client:ConnectionString"] = "Server=fake;Database=test;User Id=steeltoe;Password=password;"
            };

            // add environment variables as Cloud Foundry would
            Environment.SetEnvironmentVariable("VCAP_APPLICATION", TestHelpers.VCAP_APPLICATION);
            Environment.SetEnvironmentVariable("VCAP_SERVICES", PostgresTestHelpers.SingleServerVCAP_EDB);

            // add settings to config
            var configurationBuilder = new ConfigurationBuilder();

            configurationBuilder.AddInMemoryCollection(appsettings);
            configurationBuilder.AddEnvironmentVariables();
            configurationBuilder.AddCloudFoundry();
            var config = configurationBuilder.Build();

            var sconfig = new PostgresProviderConnectorOptions(config);

            Assert.NotEqual(appsettings["postgres:client:ConnectionString"], sconfig.ToString());
        }