public async Task Should_UpdateExistCompanySetting_When_PassValidParameters() { // Arrange var entity = new SettingsDb() { SettingKey = "key_1", DefaultValue = "default_value_1", SettingType = Domain.Enums.SettingType.Company }; entity.CompanySettings.Add(new CompanySettingsDb() { CompanyId = 1, SettingValue = "company1_key1_value" }); entity.CompanySettings.Add(new CompanySettingsDb() { CompanyId = 2, SettingValue = "company2_key1_value" }); _fixture.Context.Settings.Attach(entity); entity = new SettingsDb() { SettingKey = "key_2", DefaultValue = "default_value_2", SettingType = Domain.Enums.SettingType.Company }; entity.CompanySettings.Add(new CompanySettingsDb() { CompanyId = 1, SettingValue = "company1_key2_value" }); _fixture.Context.Settings.Attach(entity); _fixture.Context.SaveChanges(); IRepository <SettingsDb> settingRepository = new Repository <SettingsDb>(_fixture.Context); IRepository <CompanySettingsDb> repository = new Repository <CompanySettingsDb>(_fixture.Context); IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context); var handler = new UpdateCompanySettingHandler(unitOfWork, repository, settingRepository, _companyLogger, _mapper); var dtos = new List <SettingsDto>(); dtos.Add(new SettingsDto() { Key = "key_1", Value = "company1_key1_updated_value" }); var request = new UpdateCompanySettingCommand(companyId: 1, dtos); // Act var tcs = new CancellationToken(); var result = await handler.Handle(request, tcs); // Assert Assert.Equal("company1_key1_updated_value", result.SingleOrDefault(x => x.Key == "key_1").Value); Assert.Equal("company1_key2_value", result.SingleOrDefault(x => x.Key == "key_2").Value); }
public async Task Should_ThrowException_When_PassInvalidSettingKey() { // Arrange var entity = new SettingsDb() { SettingKey = "key_1", DefaultValue = "default_value_1", SettingType = Domain.Enums.SettingType.Company }; entity.CompanySettings.Add(new CompanySettingsDb() { CompanyId = 1, SettingValue = "company1_key1_value" }); entity.CompanySettings.Add(new CompanySettingsDb() { CompanyId = 2, SettingValue = "company2_key1_value" }); _fixture.Context.Settings.Attach(entity); entity = new SettingsDb() { SettingKey = "key_2", DefaultValue = "default_value_2", SettingType = Domain.Enums.SettingType.Company }; entity.CompanySettings.Add(new CompanySettingsDb() { CompanyId = 1, SettingValue = "company1_key2_value" }); _fixture.Context.Settings.Attach(entity); _fixture.Context.SaveChanges(); IRepository <SettingsDb> settingRepository = new Repository <SettingsDb>(_fixture.Context); IRepository <CompanySettingsDb> repository = new Repository <CompanySettingsDb>(_fixture.Context); IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context); var handler = new UpdateCompanySettingHandler(unitOfWork, repository, settingRepository, _companyLogger, _mapper); var dtos = new List <SettingsDto>(); dtos.Add(new SettingsDto() { Key = "key_5", Value = "company1_key5_updated_value" }); var request = new UpdateCompanySettingCommand(companyId: 1, dtos); // Act var tcs = new CancellationToken(); var exception = await Assert.ThrowsAsync <VacationTrackingException>(async() => { var result = await handler.Handle(request, tcs); }); // Assert Assert.Equal(ExceptionMessages.ItemNotFound, exception.Message); Assert.Equal(400, exception.Code); }