public static IEnumerable <object[]> GetEnsureAllLeaveUseTheSameDateRanges() { var personFaker = ServicesFixture.PersonFaker(); var rangeStart = new DateTime(2018, 5, 25); var rangeEnd = new DateTime(2018, 8, 5); for (int j = 1; j < 4; j++) { var requestDate = rangeStart; do { var requests = new LeaveRequest[j]; var person = personFaker.Generate(); person.IsThai = false; //insert 3 leave requests and test those 3 at a time for (int i = 0; i < j; i++) { var leaveRequest = new LeaveRequest { Id = Guid.NewGuid(), Approved = true, StartDate = requestDate, EndDate = requestDate, PersonId = person.Id, Type = LeaveType.Vacation }; leaveRequest.Days = leaveRequest.CalculateLength(); requests[i] = leaveRequest; requestDate += TimeSpan.FromDays(1); } yield return(new object[] { person, requests, j }); } while (requestDate < rangeEnd); } }
public void EnsureDifferentWaysOfCalculatingLeaveMatchExpected() { var person = _sf.InsertPerson(staff => staff.IsThai = false); void InsertLeaveRequest(DateTime start, DateTime?end = null, bool?approved = true) { var leaveRequest = new LeaveRequest { Id = Guid.NewGuid(), PersonId = person.Id, Type = LeaveType.Vacation, StartDate = start, EndDate = end ?? start, Approved = approved }; leaveRequest.Days = leaveRequest.CalculateLength(); _sf.DbConnection.Insert(leaveRequest); } InsertLeaveRequest(new DateTime(2018, 4, 30)); InsertLeaveRequest(new DateTime(2018, 4, 30), new DateTime(2018, 4, 30), false); InsertLeaveRequest(new DateTime(2018, 4, 30), new DateTime(2018, 4, 30), null); InsertLeaveRequest(new DateTime(2018, 6, 25), new DateTime(2018, 6, 29)); WaysToGetVacationLeaveCalculation(person.Id, 2017, _leaveService).ShouldAllBe(usage => usage.Used == 7); }
public void ThrowIfHrRequiredForUpdate(LeaveRequest oldRequest, LeaveRequest newRequest, Guid?personMakingChanges) { if (oldRequest == null) { if (newRequest.OverrideDays) { throw new UnauthorizedAccessException( "You're not allowed to override the leave calculation, talk to HR"); } if (newRequest.Days != newRequest.CalculateLength() && newRequest.Days != newRequest.CalculateLength() - 0.5m) { newRequest.Days = newRequest.CalculateLength(); } return; } if (oldRequest.PersonId != personMakingChanges || oldRequest.PersonId != newRequest.PersonId) { throw new UnauthorizedAccessException("You aren't allowed to modify this leave request"); } if (!oldRequest.OverrideDays && newRequest.OverrideDays) { throw new UnauthorizedAccessException( "You aren't allowed to override the length of this leave request, talk to HR"); } if (!newRequest.OverrideDays && newRequest.Days != newRequest.CalculateLength() && newRequest.Days != newRequest.CalculateLength() - 0.5m) { throw new UnauthorizedAccessException( "Days of leave request must match calculated when not being overriden"); } if (oldRequest.OverrideDays && oldRequest.Days != newRequest.Days) { throw new UnauthorizedAccessException( "You aren't allowed modify the length of this leave request, talk to HR"); } if (oldRequest.OverrideDays && newRequest.OverrideDays && oldRequest.CalculateLength() != newRequest.CalculateLength()) { throw new UnauthorizedAccessException( "You aren't allowed modify the start or end dates of this leave request because the calculation has been overriden, talk to HR"); } if (oldRequest.Approved != newRequest.Approved) { throw new UnauthorizedAccessException( "You aren't allowed to approve your own leave request, talk to HR"); } }
public void ShouldMatchExpectedLeave(DateTime startDate, DateTime endDate, int expectedDays) { var leaveRequest = new LeaveRequest(startDate, endDate); leaveRequest.Days = leaveRequest.CalculateLength(); var result = LeaveService.TotalLeaveUsed(new[] { leaveRequest }); Assert.Equal(expectedDays, result); }
public static int CalculateLeaveDays(LeaveRequest leaveRequest, List <Holiday> holidays) { var days = leaveRequest.CalculateLength(); foreach (var holiday in holidays) { days -= WeekdaysOverlapping(leaveRequest, holiday); } return(days); }
public void AcceptMissmatchedCalculationForHalfDays() { LeaveRequest oldRequest = GenerateRequest(); //16th is a friday oldRequest.StartDate = new DateTime(2018, 3, 16); oldRequest.EndDate = new DateTime(2018, 3, 16); oldRequest.Days = oldRequest.CalculateLength(); LeaveRequest newRequest = oldRequest.Copy(); newRequest.Days = 0.5m; //does not throw LeaveService.ThrowIfHrRequiredForUpdate(oldRequest, newRequest, new List <Holiday>()); }
public void DontMissmatchedCalculationForHalfDaysOnWeekend() { LeaveRequest oldRequest = GenerateRequest(); //17th is a saturday oldRequest.StartDate = new DateTime(2018, 3, 17); oldRequest.EndDate = new DateTime(2018, 3, 17); oldRequest.Days = oldRequest.CalculateLength(); LeaveRequest newRequest = oldRequest.Copy(); newRequest.Days = 0.5m; //does not throw Assert.Throws <UnauthorizedAccessException>(() => LeaveService.ThrowIfHrRequiredForUpdate(oldRequest, newRequest, new List <Holiday>())); }
public void ThrowsWhenChangingTheDays() { LeaveRequest oldRequest = GenerateRequest(); LeaveRequest newRequest = oldRequest.Copy(); newRequest.Days--; var ex = Assert.Throws <UnauthorizedAccessException>(() => LeaveService.ThrowIfHrRequiredForUpdate(oldRequest, newRequest, new List <Holiday>())); Assert.Contains("must match calculated", ex.Message); newRequest.Days = newRequest.CalculateLength(); newRequest.EndDate += TimeSpan.FromDays(4); ex = Assert.Throws <UnauthorizedAccessException>(() => LeaveService.ThrowIfHrRequiredForUpdate(oldRequest, newRequest, new List <Holiday>())); Assert.Contains("must match calculated", ex.Message); }