Esempio n. 1
0
        public async Task Should_UpdateExistUserSetting_When_PassValidParameters()
        {
            // Arrange
            var entity = new SettingsDb()
            {
                SettingKey   = "key_1",
                DefaultValue = "default_value_1",
                SettingType  = Domain.Enums.SettingType.User
            };

            entity.UserSettings.Add(new UserSettingsDb()
            {
                UserId       = 1,
                SettingValue = "user1_key1_value"
            });
            entity.UserSettings.Add(new UserSettingsDb()
            {
                UserId       = 2,
                SettingValue = "user2_key1_value"
            });
            _fixture.Context.Settings.Attach(entity);

            entity = new SettingsDb()
            {
                SettingKey   = "key_2",
                DefaultValue = "default_value_2",
                SettingType  = Domain.Enums.SettingType.User
            };
            entity.UserSettings.Add(new UserSettingsDb()
            {
                UserId       = 1,
                SettingValue = "user1_key2_value"
            });
            _fixture.Context.Settings.Attach(entity);
            _fixture.Context.SaveChanges();

            IRepository <SettingsDb>     settingRepository = new Repository <SettingsDb>(_fixture.Context);
            IRepository <UserSettingsDb> repository        = new Repository <UserSettingsDb>(_fixture.Context);
            IUnitOfWork unitOfWork = new UnitOfWork(_fixture.Context);

            var handler = new UpdateUserSettingHandler(unitOfWork, repository, settingRepository, _userLogger, _mapper);

            var dtos = new List <SettingsDto>();

            dtos.Add(new SettingsDto()
            {
                Key   = "key_1",
                Value = "user1_key1_updated_value"
            });

            var request = new UpdateUserSettingCommand(userId: 1, dtos);

            // Act
            var tcs    = new CancellationToken();
            var result = await handler.Handle(request, tcs);

            // Assert
            Assert.Equal("user1_key1_updated_value", result.SingleOrDefault(x => x.Key == "key_1").Value);
            Assert.Equal("user1_key2_value", result.SingleOrDefault(x => x.Key == "key_2").Value);
        }
Esempio n. 2
0
        public async Task Should_ReturnCompanySettings_When_CompanyHasCustomSettingsValue()
        {
            // Arrange
            var entity = new SettingsDb()
            {
                SettingKey   = "key_1",
                DefaultValue = "default_value_1",
                SettingType  = Domain.Enums.SettingType.Company
            };

            entity.CompanySettings.Add(new Domain.Models.CompanySetting()
            {
                CompanyId    = 1,
                SettingValue = "company_1_value"
            });
            entity.CompanySettings.Add(new Domain.Models.CompanySetting()
            {
                CompanyId    = 2,
                SettingValue = "company_2_value"
            });
            _fixture.Context.Settings.Attach(entity);

            entity = new SettingsDb()
            {
                SettingKey   = "key_2",
                DefaultValue = "default_value_2",
                SettingType  = Domain.Enums.SettingType.Company
            };
            _fixture.Context.Settings.Attach(entity);

            _fixture.Context.SaveChanges();

            IRepository <SettingsDb> repository = new Repository <SettingsDb>(_fixture.Context);

            var handler = new GetCompanySettingListHandler(repository, _mapper, _logger);

            var request = new GetCompanySettingListQuery(companyId: 1);

            // Act
            var tcs = new CancellationToken();

            var result = await handler.Handle(request, tcs);

            // Assert
            Assert.Equal("company_1_value", result.SingleOrDefault(x => x.Key == "key_1").Value);
            Assert.Equal("default_value_2", result.SingleOrDefault(x => x.Key == "key_2").Value);
        }
Esempio n. 3
0
        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);
        }