public void Wires_up_settings_from_string() { var services = new ServiceCollection(); const string json = @" { ""AttributeValidated"": { ""IntegerA"": 76, ""BooleanB"": true } } "; var configuration = ConfigurationTestBuilder.BuildFromJsonString(json); services.AddValidatedSettings <AttributeValidatedSettings>(configuration); var serviceProvider = services.BuildServiceProvider(); var result = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value; Assert.NotNull(result); Assert.Equal(76, result.IntegerA); Assert.True(result.BooleanB); }
public void Catches_validation_error_for_Test1_file() { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder .BuildFromEmbeddedResource(JsonIndex.AttributeValidated.Test1); var sectionName = SettingsSectionNameAttribute .GetSettingsSectionName <AttributeValidatedSettings>(); // the next call will throw an exception Action act = () => { services.AddEagerlyValidatedSettings <AttributeValidatedSettings>(configuration, out _); }; var exception = Assert.Throws <OptionsValidationException>(act); Assert.StartsWith("Validation failed for members:", exception.Message); /* Full validation message looks like * * Validation failed for members: 'IntegerA' with the error: 'The field IntegerA must be between 1 * and 100.'.; Validation failed for members: 'BooleanB' with the error: 'The BooleanB field is * required.'. */ }
public void Wires_up_SimpleSettings_from_json_files(string filename, int integerA) { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename); services.AddValidatedSettings <SimpleSettings>(configuration); var serviceProvider = services.BuildServiceProvider(); var result = serviceProvider.GetRequiredService <IOptions <SimpleSettings> >().Value; Assert.NotNull(result); Assert.Equal(integerA, result.IntegerA); }
public void Wires_up_SimpleSettings_from_json_files_ReferenceEquals(string filename, int integerA) { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename); services.AddEagerlyValidatedSettings <SimpleSettings>(configuration, out var initialResult); var serviceProvider = services.BuildServiceProvider(); var result = serviceProvider.GetRequiredService <IOptions <SimpleSettings> >().Value; Assert.Equal(integerA, initialResult.IntegerA); Assert.Equal(integerA, result.IntegerA); Assert.True(object.ReferenceEquals(initialResult, result)); }
public void Wires_up_settings_from_Test2_file() { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource( JsonIndex.AttributeValidated.Test2); services.AddValidatedSettings <AttributeValidatedSettings>(configuration); var serviceProvider = services.BuildServiceProvider(); var result = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value; Assert.NotNull(result); Assert.Equal(92, result.IntegerA); Assert.False(result.BooleanB); }
public void Wires_up_settings_from_Test2_file_ReferenceEquals() { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder .BuildFromEmbeddedResource(JsonIndex.AttributeValidated.Test2); services.AddEagerlyValidatedSettings <AttributeValidatedSettings>(configuration, out var initialResult); var serviceProvider = services.BuildServiceProvider(); var result = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value; Assert.Equal(92, initialResult.IntegerA); Assert.Equal(92, result.IntegerA); Assert.True(object.ReferenceEquals(initialResult, result)); }
public void FindEngineersAvailableOn_WEEK_SCAN_PERIOD_Error() { string contextName = "FindEngineersAvailableOn_WEEK_SCAN_PERIOD_Error"; _contextNames.Add(contextName); DbContextOptions <BAUDbContext> options = DbContextUtils.GetContextOptions(contextName); using (var context = new BAUDbContext(options)) { try { new ShiftRepository(context, ConfigurationTestBuilder.GetConfiguration("WEEK_SCAN_PERIOD")); } catch (ArgumentNullException ex) { Assert.Equal("App:WEEK_SCAN_PERIOD", ex.ParamName); } } }
public void FindEngineersAvailableOn_MAX_SHIFT_SUM_HOURS_DURATION_Error() { string contextName = "FindEngineersAvailableOn_MAX_SHIFT_SUM_HOURS_DURATION_Error"; _contextNames.Add(contextName); DbContextOptions <BAUDbContext> options = DbContextUtils.GetContextOptions(contextName); using (var context = new BAUDbContext(options)) { try { new ShiftRepository(context, ConfigurationTestBuilder.GetConfiguration("MAX_SHIFT_SUM_HOURS_DURATION")); } catch (ArgumentNullException ex) { Assert.Equal("App:MAX_SHIFT_SUM_HOURS_DURATION", ex.ParamName); } } }
public void Test_reference_equals_1(string filename) { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename); var sectionName = SettingsSectionNameAttribute.GetSettingsSectionName <AttributeValidatedSettings>(); var configurationSection = configuration.GetSection(sectionName); // This is the standard way, it results in lazy validation services.AddOptions <AttributeValidatedSettings>() .Bind(configurationSection) .RecursivelyValidateDataAnnotations(); var serviceProvider = services.BuildServiceProvider(); var result1 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value; var result2 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value; Assert.True(object.ReferenceEquals(result1, result2)); }
public void Catches_validation_error_for_Test1_file() { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource( JsonIndex.AttributeValidated.Test1); services.AddValidatedSettings <AttributeValidatedSettings>(configuration); var serviceProvider = services.BuildServiceProvider(); // this call will work, because we're just getting the accessor singleton var optionsAccessor = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >(); // the next call will throw an exception because we're accessing the .Value instance Action act = () => { var _ = optionsAccessor.Value; }; var exception = Assert.Throws <OptionsValidationException>(act); Assert.StartsWith("Validation failed for members: 'IntegerA'", exception.Message); }
public void Wires_up_SimpleSettings_from_string() { var services = new ServiceCollection(); const string json = @" { ""Simple"": { ""IntegerA"": 1075 } } "; var configuration = ConfigurationTestBuilder.BuildFromJsonString(json); services.AddSettings <SimpleSettings>(configuration); var serviceProvider = services.BuildServiceProvider(); var result = serviceProvider.GetRequiredService <IOptions <SimpleSettings> >().Value; Assert.NotNull(result); Assert.Equal(1075, result.IntegerA); }
public void Test_reference_equals_2(string filename) { var services = new ServiceCollection(); var configuration = ConfigurationTestBuilder.BuildFromEmbeddedResource(filename); var sectionName = SettingsSectionNameAttribute.GetSettingsSectionName <AttributeValidatedSettings>(); var simpleSettings = configuration.GetSection(sectionName).Get <AttributeValidatedSettings>(); // This approach gives eager validation // Downside is that I think IOptionsMonitor<T> will not inform of changes var options = Options.Create <AttributeValidatedSettings>(simpleSettings); services.AddSingleton <IOptions <AttributeValidatedSettings> >(options); services.AddSingleton <IValidateOptions <AttributeValidatedSettings> >( new RecursiveDataAnnotationValidateOptions <AttributeValidatedSettings>( null )); var serviceProvider = services.BuildServiceProvider(); var validator = serviceProvider.GetRequiredService <IValidateOptions <AttributeValidatedSettings> >(); var validateOptionsResult = validator.Validate(null, options.Value); if (validateOptionsResult.Failed) { throw new Exception(); } var validatedSettings = options.Value; var result1 = validatedSettings; var result2 = serviceProvider.GetRequiredService <IOptions <AttributeValidatedSettings> >().Value; Assert.True(object.ReferenceEquals(result1, result2)); }
public void FindEngineersAvailableOn_Check_Consecutive_Days(DateTime date, int nEngineers) { string contextName = $"FindEngineersAvailableOn_Check_Consecutive_Days"; _contextNames.Add(contextName); DbContextOptions <BAUDbContext> options = DbContextUtils.GetContextOptions(contextName); IShiftRepository repository = null; const int shiftDuration = 4; // fake data using (BAUDbContext context = new BAUDbContext(options)) { var engineer1 = new Engineer { Name = "1" }; var engineer2 = new Engineer { Name = "2" }; var engineer3 = new Engineer { Name = "3" }; var engineer4 = new Engineer { Name = "4" }; var engineer5 = new Engineer { Name = "5" }; var engineer6 = new Engineer { Name = "6" }; var engineer7 = new Engineer { Name = "7" }; var engineer8 = new Engineer { Name = "8" }; var engineer9 = new Engineer { Name = "9" }; var engineer10 = new Engineer { Name = "10" }; var engineersList = new List <Engineer> { engineer1, engineer2, engineer3, engineer4, engineer5, engineer6, engineer7, engineer8, engineer9, engineer10, }; context.Engineers.AddRange(engineersList); var today = new DateTime(2017, 12, 11); var shifts = new List <EngineerShift> { //first week new EngineerShift { Date = today.Date, Duration = shiftDuration, Engineer = engineer1 }, new EngineerShift { Date = today.Date, Duration = shiftDuration, Engineer = engineer2 }, new EngineerShift { Date = today.Date.AddDays(1), Duration = shiftDuration, Engineer = engineer7 }, new EngineerShift { Date = today.Date.AddDays(2), Duration = shiftDuration, Engineer = engineer3 }, new EngineerShift { Date = today.Date.AddDays(2), Duration = shiftDuration, Engineer = engineer4 }, new EngineerShift { Date = today.Date.AddDays(4), Duration = shiftDuration, Engineer = engineer5 }, new EngineerShift { Date = today.Date.AddDays(4), Duration = shiftDuration, Engineer = engineer6 }, }; context.EngineersShifts.AddRange(shifts); context.SaveChanges(); } // test using (var context = new BAUDbContext(options)) { repository = new ShiftRepository(context, ConfigurationTestBuilder.GetConfiguration()); IList <Engineer> availableEngineers = repository.FindEngineersAvailableOn(date); Assert.Equal(nEngineers, availableEngineers.Count); } }