Пример #1
0
        public void GetChildren_WithTenantAsSection_ReturnsTenantChildrenWhenAvailable()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key1", "Value1")
                                .WithAppSetting("Key2", "Value2")
                                .WithAppSetting("Category1:Key1", "Value3")
                                .WithAppSetting("Category1:Key2", "Value4")
                                .WithAppSetting("Category1:Key3", "Value5")
                                .WithAppSetting("Category1:Category2:Key1", "Value6")
                                .WithAppSetting("Category1:Category2:Key2", "Value7")
                                .WithAppSetting("Category3:Key1", "Value8")
                                .WithAppSetting("Tenant:Key2", "Value9")
                                .WithAppSetting("Tenant:Key3", "Value10") // This has no global equivalent so should not be included as a child
                                .WithAppSetting("Tenant:Category1:Key1", "Value11")
                                .WithAppSetting("Tenant:Category1:Key3", "Value12")
                                .WithAppSetting("Tenant:Category1:Category2:Key2", "Value13")
                                .WithAppSetting("Tenants:Category1:Key1", "Value14") // This starts with the Tenant, but does not equal it and should be included as a child
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                var children = config.GetChildren("Tenant")
                               .ToList();

                Assert.That(children.Count, Is.EqualTo(9));

                Assert.That(children.FirstOrDefault(child => child.Path == "Tenant:Key2")?.Value, Is.EqualTo("Value9"));
            });
        }
Пример #2
0
        public void GetChildren_WithSection_ReturnsGlobalChildrenForMissingTenantChildren()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Category1:Key1", "Value1")
                                .WithAppSetting("Category1:Key2", "Value2")
                                .WithAppSetting("Category1:Key3", "Value3")
                                .WithAppSetting("Category1:Category2:Key1", "Value4")
                                .WithAppSetting("Category1:Category2:Key2", "Value5")
                                .WithAppSetting("Category3:Key1", "Value6")
                                .WithAppSetting("Tenant:Category1:Key1", "Value7")
                                .WithAppSetting("Tenant:Category1:Key3", "Value8")
                                .WithAppSetting("Tenant:Category1:Category2:Key2", "Value9")
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                var children = config.GetSection("Category1", "Tenant")
                               .GetChildren()
                               .ToList();

                // All global children of the specified section should be represented

                Assert.That(children.Count, Is.EqualTo(5));

                // If a tenant specific key is not present it should fall back to the global equivalent

                Assert.That(children.Any(child => child.Key == "Key2"), Is.True);

                Assert.That(children.FirstOrDefault(child => child.Key == "Key2")?.Value, Is.EqualTo("Value2"));
            });
        }
Пример #3
0
        public void GetSection_WithTenantSection_ReturnsGlobalValueForMissingTenantKey()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key", "Value1")
                                .WithAppSetting("Category1:Key1", "Value2")
                                .WithAppSetting("Category1:Key2", "Value3")
                                .WithAppSetting("Category1:Category2:Key1", "Value4")
                                .WithAppSetting("Category1:Category2:Key2", "Value5")
                                .WithAppSetting("Tenant:Category1:Key1", "Value6")
                                .WithAppSetting("Tenant:Category1:Category2:Key1", "Value7")
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                Assert.That(config.GetSection("Key", "Tenant").Value, Is.EqualTo("Value1"));

                var categorySection1 = config.GetSection("Category1", "Tenant");

                Assert.That(categorySection1["Key2"], Is.EqualTo("Value3"));

                Assert.That(categorySection1.GetSection("Category2")["Key2"], Is.EqualTo("Value5"));
            });
        }
Пример #4
0
        public void GetValue_WithGlobalOnlySetting_ReturnsGlobalValue()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key", "Value1")
                                .WithAppSetting("Category:Key", "Value2")
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                Assert.That(config.GetValue("Key", tenant: "Tenant"), Is.EqualTo("Value1"));

                Assert.That(config.GetValue("Category:Key", tenant: "Tenant"), Is.EqualTo("Value2"));
            });
        }
Пример #5
0
        public void Get_WithTenantSettingInCategory_ReturnsTenantValue()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Category1:Key1", "Value1")
                                .WithAppSetting("Category1:Key2", 2)
                                .WithAppSetting("Tenant:Category1:Key2", 3)
                                .Build();

            var config = new AppConfiguration(configManager);

            var settings = config.GetSection("Category1", "Tenant").Get <TestSettings>();

            Assert.Multiple(() =>
            {
                Assert.That(settings.Key1, Is.EqualTo("Value1"));

                Assert.That(settings.Key2, Is.EqualTo(3));
            });
        }
Пример #6
0
        public void GetSection_WithGlobalOnlySection_ReturnsGlobalSection()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key", "Value1")
                                .WithAppSetting("Category:Key1", "Value2")
                                .WithAppSetting("Category:Key2", "Value3")
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                Assert.That(config.GetSection("Key", "Tenant").Value, Is.EqualTo("Value1"));

                Assert.That(config.GetSection("Category", "Tenant")["Key1"], Is.EqualTo("Value2"));

                Assert.That(config.GetSection("Category", "Tenant")["Key2"], Is.EqualTo("Value3"));
            });
        }
Пример #7
0
        public void Bind_WithTenantSetting_ReturnsTenantValue()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key1", "Value1")
                                .WithAppSetting("Key2", 2)
                                .WithAppSetting("Tenant:Key2", 3)
                                .Build();

            var config = new AppConfiguration(configManager);

            var settings = new TestSettings();

            config.Bind(tenant: "Tenant", settings);

            Assert.Multiple(() =>
            {
                Assert.That(settings.Key1, Is.EqualTo("Value1"));

                Assert.That(settings.Key2, Is.EqualTo(3));
            });
        }
Пример #8
0
        public void GetValue_WithDefaultValue_ReturnsDefaultValue()
        {
            var configManager = new ConfigurationManagerFixture()
                                .WithAppSetting("Key", "Value1")
                                .WithAppSetting("Category:Key", "Value2")
                                .WithAppSetting("Tenant:Key", null)
                                .WithAppSetting("Tenant:Category:Key", null)
                                .WithAppSetting("GlobalKey", null)
                                .WithAppSetting("Category:GlobalKey", null)
                                .Build();

            var config = new AppConfiguration(configManager);

            Assert.Multiple(() =>
            {
                Assert.That(config.GetValue("Key", "Tenant", "DefaultValue"), Is.EqualTo("DefaultValue"));

                Assert.That(config.GetValue("Category:Key", "Tenant", "DefaultValue"), Is.EqualTo("DefaultValue"));

                Assert.That(config.GetValue("GlobalKey", "Tenant", "DefaultValue"), Is.EqualTo("DefaultValue"));

                Assert.That(config.GetValue("Category:GlobalKey", "Tenant", "DefaultValue"), Is.EqualTo("DefaultValue"));
            });
        }