Пример #1
0
        public async Task ShouldCreateWorkingHours()
        {
            //Arrange
            var userId = await RunAsDefaultUserAsync();

            var command = new CreateWorkingHoursCommand
            {
                Date        = DateTime.UtcNow,
                Duration    = TimeSpan.FromHours(3),
                UserName    = CurrentUser.UserName,
                Description = "Some description"
            };

            //Act
            var id = await SendAsync(command);

            //Assert
            var item = await FindAsync <UserWorkingHours>(id);

            item.Should().NotBeNull();
            item.Date.Should().Be(command.Date.Date);
            item.Description.Should().Be(command.Description);
            item.Duration.Should().Be(command.Duration);
            item.UserId.Should().Be(userId);
            item.CreatedBy.Should().Be(userId);
            item.Created.Should().BeCloseTo(DateTime.Now, 10000);
            item.LastModifiedBy.Should().BeNull();
            item.LastModified.Should().BeNull();
        }
Пример #2
0
        public void ShouldThrowValidationExceptionWhenSumOfWorkingHoursForDayBiggerThan24()
        {
            //Arrange
            var request = new CreateWorkingHoursCommand
            {
                Date        = _date,
                Description = "Description",
                Duration    = TimeSpan.FromHours(4),
                UserName    = _userName
            };
            var expectedCreatedId = 10;
            var user = new UserDto(_userName, _userId, "Role");

            _identityService.Setup(s => s.GetUserByNameAsync(_userName)).ReturnsAsync(user);

            var mockWorkingHoursDbSet = new List <UserWorkingHours>
            {
                new UserWorkingHours(_userId, "Description", _date, TimeSpan.FromHours(10)),
                new UserWorkingHours(_userId, "Description2", _date, TimeSpan.FromHours(12)),
            }.AsQueryable().BuildMockDbSet();

            _applicationDbContext.Setup(c => c.WorkingHoursSet).Returns(mockWorkingHoursDbSet.Object);
            _applicationDbContext.Setup(c => c.WorkingHoursSet.Add(It.IsAny <UserWorkingHours>()))
            .Callback <UserWorkingHours>(i => i.Id = expectedCreatedId);

            //Act & Assert
            FluentActions.Invoking(() => _handler.Handle(request, _cancellationToken)).Should().Throw <ValidationException>()
            .Which.Errors.First().Value.First().Should().Be("Sum of working hours per day can't be bigger than 24 hours");
        }
Пример #3
0
        public void ShouldRequireMinimumFields()
        {
            //Arrange
            var command = new CreateWorkingHoursCommand();

            //Act & Assert
            FluentActions.Invoking(() =>
                                   SendAsync(command)).Should().Throw <ValidationException>();
        }
Пример #4
0
        public async Task ShouldThrowValidationExceptionWhenUserIsNull()
        {
            //Arrange
            var request = new CreateWorkingHoursCommand
            {
                Date        = _date,
                Description = "Description",
                Duration    = TimeSpan.FromHours(4),
                UserName    = _userName
            };

            _identityService.Setup(s => s.GetUserByNameAsync(_userName)).ReturnsAsync((UserDto)null);
            _identityService.Setup(s => s.CheckUserIsNotNull(null)).Throws <ValidationException>();

            //Act & Assert
            FluentActions.Invoking(() => _handler.Handle(request, _cancellationToken)).Should().Throw <ValidationException>();
        }
Пример #5
0
        public async Task ShouldCreateWorkingHours()
        {
            //Arrange
            var request = new CreateWorkingHoursCommand
            {
                Date        = _date,
                Description = "Description",
                Duration    = TimeSpan.FromHours(4),
                UserName    = _userName
            };
            var expectedCreatedId   = 10;
            var expectedWorkingHour = new UserWorkingHours(_userId, "Description", _date, TimeSpan.FromHours(4))
            {
                Id = expectedCreatedId
            };
            var user = new UserDto(_userName, _userId, "Role");

            _identityService.Setup(s => s.GetUserByNameAsync(_userName)).ReturnsAsync(user);

            var mockWorkingHoursDbSet = new List <UserWorkingHours>
            {
                new UserWorkingHours(_userId, "Description", _date, TimeSpan.FromHours(10))
            }.AsQueryable().BuildMockDbSet();

            _applicationDbContext.Setup(c => c.WorkingHoursSet).Returns(mockWorkingHoursDbSet.Object);
            _applicationDbContext.Setup(c => c.WorkingHoursSet.Add(It.IsAny <UserWorkingHours>()))
            .Callback <UserWorkingHours>(i => i.Id = expectedCreatedId);

            //Act
            var id = await _handler.Handle(request, _cancellationToken);

            //Assert
            id.Should().Be(expectedCreatedId);
            _applicationDbContext.Verify(c =>
                                         c.WorkingHoursSet.Add(It.Is <UserWorkingHours>(i => MatchWorkingHours(i, expectedWorkingHour))));
            _applicationDbContext.Verify(c => c.SaveChangesAsync(_cancellationToken), Times.Once);
        }
Пример #6
0
 public async Task <ActionResult <int> > Create([FromBody] CreateWorkingHoursCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }
        public async Task <ActionResult <Unit> > Create(int id, CreateWorkingHoursCommand command)
        {
            command.SportObjectId = id;

            return(await Mediator.Send(command));
        }