Example #1
0
        private void TestDeletions()
        {
            // team deletions
            Team[] teams = schedule.Teams.ToArray();

            foreach (Team team in teams)
            {
                schedule.DeleteTeam(team);
            }
            Assert.IsTrue(schedule.Teams.Count == 0);

            // shift deletions
            Shift[] shifts = schedule.Shifts.ToArray();

            foreach (Shift shift in shifts)
            {
                schedule.DeleteShift(shift);
            }
            Assert.IsTrue(schedule.Shifts.Count == 0);

            // non-working period deletions
            NonWorkingPeriod[] periods = schedule.NonWorkingPeriods.ToArray();

            foreach (NonWorkingPeriod period in periods)
            {
                schedule.DeleteNonWorkingPeriod(period);
            }
            Assert.IsTrue(schedule.NonWorkingPeriods.Count == 0);
        }
Example #2
0
        public void TestNonWorkingTime()
        {
            schedule = new WorkSchedule("Non Working Time", "Test non working time");
            LocalDate date = new LocalDate(2017, 1, 1);
            LocalTime time = new LocalTime(7, 0, 0);

            NonWorkingPeriod period1 = schedule.CreateNonWorkingPeriod("Day1", "First test day",
                                                                       date.At(LocalTime.Midnight), Duration.FromHours(24));
            NonWorkingPeriod period2 = schedule.CreateNonWorkingPeriod("Day2", "First test day",
                                                                       date.PlusDays(7).At(time), Duration.FromHours(24));

            LocalDateTime from = date.At(time);
            LocalDateTime to   = date.At(time.PlusHours(1));

            // case #1
            Duration duration = schedule.CalculateNonWorkingTime(from, to);

            Assert.IsTrue(duration.Equals(Duration.FromHours(1)));

            // case #2
            from     = date.Minus(Period.FromDays(1)).At(time);
            to       = date.PlusDays(1).At(time);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(24)));

            // case #3
            from     = date.Minus(Period.FromDays(1)).At(time);
            to       = date.Minus(Period.FromDays(1)).At(time.PlusHours(1));
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            // case #4
            from     = date.PlusDays(1).At(time);
            to       = date.PlusDays(1).At(time.PlusHours(1));
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            // case #5
            from     = date.Minus(Period.FromDays(1)).At(time);
            to       = date.At(time);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(7)));

            // case #6
            from     = date.At(time);
            to       = date.PlusDays(1).At(time);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(17)));

            // case #7
            from     = date.At(LocalTime.Noon);
            to       = date.PlusDays(7).At(LocalTime.Noon);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(17)));

            // case #8
            from     = date.Minus(Period.FromDays(1)).At(LocalTime.Noon);
            to       = date.PlusDays(8).At(LocalTime.Noon);
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(48)));

            // case #9
            schedule.DeleteNonWorkingPeriod(period1);
            schedule.DeleteNonWorkingPeriod(period2);
            from = date.At(time);
            to   = date.At(time.PlusHours(1));

            // case #10
            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            Duration  shiftDuration = Duration.FromHours(8);
            LocalTime shiftStart    = new LocalTime(7, 0, 0);

            Shift shift = schedule.CreateShift("Work Shift1", "Working time shift", shiftStart, shiftDuration);

            Rotation rotation = new Rotation("Case 10", "Case10");

            rotation.AddSegment(shift, 1, 1);

            LocalDate startRotation = new LocalDate(2017, 1, 1);
            Team      team          = schedule.CreateTeam("Team", "Team", rotation, startRotation);

            team.RotationStart = startRotation;

            period1 = schedule.CreateNonWorkingPeriod("Day1", "First test day", date.At(LocalTime.Midnight),
                                                      Duration.FromHours(24));

            LocalDate mark = date.PlusDays(rotation.GetDayCount());

            from = mark.At(time.Minus(Period.FromHours(2)));
            to   = mark.At(time.Minus(Period.FromHours(1)));

            // case #11
            duration = schedule.CalculateWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(0)));

            // case #12
            from = date.At(shiftStart);
            to   = date.At(time.PlusHours(8));

            duration = schedule.CalculateNonWorkingTime(from, to);
            Assert.IsTrue(duration.Equals(Duration.FromHours(8)));
        }
Example #3
0
        public void TestExceptions()
        {
            schedule = new WorkSchedule("Exceptions", "Test exceptions");
            Duration  shiftDuration = Duration.FromHours(24);
            LocalTime shiftStart    = new LocalTime(7, 0, 0);

            NonWorkingPeriod period = schedule.CreateNonWorkingPeriod("Non-working", "Non-working period",
                                                                      new LocalDateTime(2017, 1, 1, 0, 0, 0), Duration.FromHours(24));

            try
            {
                period.Duration = Duration.FromSeconds(0);
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                // same period
                schedule.CreateNonWorkingPeriod("Non-working", "Non-working period", new LocalDateTime(2017, 1, 1, 0, 0, 0),
                                                Duration.FromHours(24));
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            // shift
            Shift shift = schedule.CreateShift("Test", "Test shift", shiftStart, shiftDuration);

            try
            {
                // crosses midnight
                shift.CalculateWorkingTime(shiftStart.Minus(Period.FromHours(1)), shift.GetEnd().PlusHours(1));
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                shift.Duration = Duration.FromSeconds(0);
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                shift.Duration = Duration.FromSeconds(48 * 3600);
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                // same shift
                shift = schedule.CreateShift("Test", "Test shift", shiftStart, shiftDuration);
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            Rotation rotation = new Rotation("Rotation", "Rotation");

            rotation.AddSegment(shift, 5, 2);

            LocalDate startRotation = new LocalDate(2016, 12, 31);
            Team      team          = schedule.CreateTeam("Team", "Team", rotation, startRotation);

            // ok
            schedule.CalculateWorkingTime(new LocalDateTime(2017, 1, 1, 7, 0, 0), new LocalDateTime(2017, 2, 1, 0, 0, 0));

            try
            {
                // end before start
                schedule.CalculateWorkingTime(new LocalDateTime(2017, 1, 2, 0, 0, 0), new LocalDateTime(2017, 1, 1, 0, 0, 0));
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                // same team
                team = schedule.CreateTeam("Team", "Team", rotation, startRotation);
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                // date before start
                team.GetDayInRotation(new LocalDate(2016, 1, 1));
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                // end before start
                schedule.PrintShiftInstances(new LocalDate(2017, 1, 2), new LocalDate(2017, 1, 1));
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            try
            {
                // delete in-use shift
                schedule.DeleteShift(shift);
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            // breaks
            Break lunch = shift.CreateBreak("Lunch", "Lunch", new LocalTime(12, 0, 0), Duration.FromMinutes(60));

            lunch.Duration  = Duration.FromMinutes(30);
            lunch.StartTime = new LocalTime(11, 30, 0);
            shift.RemoveBreak(lunch);
            shift.RemoveBreak(lunch);

            Shift shift2 = schedule.CreateShift("Test2", "Test shift2", shiftStart, shiftDuration);

            Assert.IsFalse(shift.Equals(shift2));

            Break lunch2 = shift2.CreateBreak("Lunch2", "Lunch", new LocalTime(12, 0, 0), Duration.FromMinutes(60));

            shift.RemoveBreak(lunch2);

            // ok to delete
            WorkSchedule schedule2 = new WorkSchedule("Exceptions2", "Test exceptions2");

            schedule2.Name        = "Schedule 2";
            schedule2.Description = "a description";

            schedule2.DeleteShift(shift);
            schedule2.DeleteTeam(team);
            schedule2.DeleteNonWorkingPeriod(period);

            // nulls
            try
            {
                schedule.CreateShift(null, "1", shiftStart, Duration.FromMinutes(60));
                Assert.Fail();
            }
            catch (Exception)
            {
            }

            Assert.IsFalse(shift.Equals(rotation));

            // hashcode()
            team.GetHashCode();
            String name = team.Name;
            Dictionary <String, Team> teams = new Dictionary <String, Team>();

            teams[name] = team;
            Team t = teams[name];
        }