예제 #1
0
        public void Room_TwoSchedulesWithTimesThatDoOverlap_HasErrorMessage()
        {
            var rsv = new RoomScheduleValidator();
            var rv  = new RoomValidator(rsv);

            var schedule = new List <RoomSchedule> {
                new RoomSchedule {
                    Start = DateTime.Parse("2020-03-01 07:30"), End = DateTime.Parse("2020-03-01 14:00")
                },
                new RoomSchedule {
                    Start = DateTime.Parse("2020-03-01 13:30"), End = DateTime.Parse("2020-03-01 18:30")
                }
            };

            var room = new Room {
                RoomBooked = DateTime.Parse("2020-03-01"), Schedule = schedule
            };

            var validationResult = rv.TestValidate(room);

            //Assert.That(validationResult.Errors.Count, Is.EqualTo(1));
            //validationResult.ShouldHaveValidationErrorFor(x => x.Schedule)
            //    .WithErrorMessage("Schedule can not have overlapping times.");

            Assert.That(validationResult.Errors.Count, Is.EqualTo(2));
            validationResult.ShouldHaveValidationErrorFor(x => x.Schedule)
            .WithErrorMessage("Schedule 7:30 AM-2:00 PM overlaps another schedule.")
            .WithErrorMessage("Schedule 1:30 PM-6:30 PM overlaps another schedule.");
        }
예제 #2
0
        public void Schedule_HasChildValidator()
        {
            var rsv = new RoomScheduleValidator();
            var rv  = new RoomValidator(rsv);

            rv.ShouldHaveChildValidator(x => x.Schedule, typeof(RoomScheduleValidator));
        }
예제 #3
0
        public void Start_LessThanEnd_HasNoErrorMessage()
        {
            var rsv      = new RoomScheduleValidator();
            var schedule = new RoomSchedule {
                Start = DateTime.Parse("2020-03-01 13:30"), End = DateTime.Parse("2020-03-01 18:30")
            };

            rsv.ShouldNotHaveValidationErrorFor(x => x.Start, schedule);
        }
예제 #4
0
        public void Start_NotSet_HasErrorMessage()
        {
            var rsv      = new RoomScheduleValidator();
            var schedule = new RoomSchedule {
                End = DateTime.Parse("2020-03-01 18:30")
            };

            rsv.ShouldHaveValidationErrorFor(x => x.Start, schedule).WithErrorMessage("Start time required.");
        }
예제 #5
0
        public void RoomBooked_NotSet_HasErrorMessage()
        {
            var rsv = new RoomScheduleValidator();
            var rv  = new RoomValidator(rsv);

            var room = new Room();

            rv.ShouldHaveValidationErrorFor(x => x.RoomBooked, room).WithErrorMessage("Booking can not be empty.");
        }
예제 #6
0
        public void End_LessThanStart_HasErrorMessage()
        {
            var rsv      = new RoomScheduleValidator();
            var schedule = new RoomSchedule {
                Start = DateTime.Parse("2020-03-01 18:30"), End = DateTime.Parse("2020-03-01 13:30")
            };

            rsv.ShouldHaveValidationErrorFor(x => x.End, schedule).WithErrorMessage("End time can not be before start time.");
        }
예제 #7
0
        public void Start_EqualToEnd_HasErrorMessage()
        {
            var rsv      = new RoomScheduleValidator();
            var schedule = new RoomSchedule {
                Start = DateTime.Parse("2020-03-01 13:30"), End = DateTime.Parse("2020-03-01 13:30")
            };

            rsv.ShouldHaveValidationErrorFor(x => x.Start, schedule).WithErrorMessage("Start time can not be the same as the end time.");
        }
예제 #8
0
        public void Room_TwoSchedulesWithTimesThatAreContiguous_HasNoErrorMessage()
        {
            var rsv = new RoomScheduleValidator();
            var rv  = new RoomValidator(rsv);

            var schedule = new List <RoomSchedule> {
                new RoomSchedule {
                    Start = DateTime.Parse("2020-03-01 07:30"), End = DateTime.Parse("2020-03-01 13:30")
                },
                new RoomSchedule {
                    Start = DateTime.Parse("2020-03-01 13:30"), End = DateTime.Parse("2020-03-01 18:30")
                }
            };

            var room = new Room {
                RoomBooked = DateTime.Parse("2020-03-01"), Schedule = schedule
            };

            var validationResult = rv.TestValidate(room);

            Assert.That(validationResult.Errors.Count, Is.EqualTo(0));
        }
예제 #9
0
        public RoomValidator(RoomScheduleValidator roomScheduleValidator)
        {
            RuleFor(o => o.RoomBooked)
            .NotEmpty().WithMessage("Booking can not be empty.");

            //RuleFor(o => o.Schedule).Must(schedule =>
            //    {
            //        if (schedule == null || !schedule.Any())
            //        {
            //            return true;
            //        }

            //        return schedule.All(item => !schedule.Where(x => !ReferenceEquals(item, x)).Any(x => x.Start < item.End && x.End > item.Start));
            //    })
            //    .WithMessage("Schedule can not have overlapping times.");

            RuleFor(o => o.Schedule).Custom((schedule, context) =>
            {
                if (schedule == null || !schedule.Any())
                {
                    return;
                }

                foreach (var item in schedule)
                {
                    var scheduleOverlapsAnotherSchedule = schedule.Where(x => !ReferenceEquals(item, x)).Any(x => x.Start <item.End && x.End> item.Start);
                    if (scheduleOverlapsAnotherSchedule)
                    {
                        context.AddFailure($"Schedule {item.Start.ToShortTimeString()}-{item.End.ToShortTimeString()} overlaps another schedule.");
                    }
                }
            });

            RuleForEach(x => x.Schedule)
            .SetValidator(roomScheduleValidator);
        }