Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1929 - Current days of study/studybreaks count is 1 short of total #1975

Merged
merged 5 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see some tests added to this ❤️

Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import { OfferingStudyBreakCalculationContext } from "../../education-program-offering-validation.models";
import { EducationProgramOfferingService } from "../../education-program-offering.service";

describe("EducationProgramOfferingService-getCalculatedStudyBreaksAndWeeks", () => {
it("Should calculate fundedStudyPeriodDays and totalDays counting start and end dates when studyStartDate and studyEndDate are available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "2023-01-01",
studyEndDate: "2023-01-10",
studyBreaks: [],
};

// Act
const calculatedStudyBreaksAndWeeks =
EducationProgramOfferingService.getCalculatedStudyBreaksAndWeeks(
offeringStudyBreakCalculationContext,
);

// Assert
expect(calculatedStudyBreaksAndWeeks).toEqual({
allowableStudyBreaksDaysAmount: 1,
fundedStudyPeriodDays: 10,
studyBreaks: [],
sumOfTotalEligibleBreakDays: 0,
sumOfTotalIneligibleBreakDays: 0,
totalDays: 10,
totalFundedWeeks: 2,
unfundedStudyPeriodDays: 0,
});
});

it("Should not calculate fundedStudyPeriodDays and totalDays counting start and end dates when studyStartDate is not available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "",
studyEndDate: "2023-01-10",
studyBreaks: [],
};

// Act
const calculatedStudyBreaksAndWeeks =
EducationProgramOfferingService.getCalculatedStudyBreaksAndWeeks(
offeringStudyBreakCalculationContext,
);

// Assert
expect(calculatedStudyBreaksAndWeeks).toEqual({
allowableStudyBreaksDaysAmount: NaN,
fundedStudyPeriodDays: NaN,
studyBreaks: [],
sumOfTotalEligibleBreakDays: 0,
sumOfTotalIneligibleBreakDays: 0,
totalDays: NaN,
totalFundedWeeks: NaN,
unfundedStudyPeriodDays: NaN,
});
});

it("Should not calculate fundedStudyPeriodDays and totalDays counting start and end dates when studyEndDate is not available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "2023-01-01",
studyEndDate: "",
studyBreaks: [],
};

// Act
const calculatedStudyBreaksAndWeeks =
EducationProgramOfferingService.getCalculatedStudyBreaksAndWeeks(
offeringStudyBreakCalculationContext,
);

// Assert
expect(calculatedStudyBreaksAndWeeks).toEqual({
allowableStudyBreaksDaysAmount: NaN,
fundedStudyPeriodDays: NaN,
studyBreaks: [],
sumOfTotalEligibleBreakDays: 0,
sumOfTotalIneligibleBreakDays: 0,
totalDays: NaN,
totalFundedWeeks: NaN,
unfundedStudyPeriodDays: NaN,
});
});

it("Should calculate studyBreaks counting start and end dates when study break dates available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "",
studyEndDate: "",
studyBreaks: [
{
breakStartDate: "2023-05-29",
breakEndDate: "2023-06-06",
},
{
breakStartDate: "2023-06-08",
breakEndDate: "2023-06-13",
},
],
};

// Act
const calculatedStudyBreaksAndWeeks =
EducationProgramOfferingService.getCalculatedStudyBreaksAndWeeks(
offeringStudyBreakCalculationContext,
);

// Assert
expect(calculatedStudyBreaksAndWeeks).toEqual({
allowableStudyBreaksDaysAmount: NaN,
fundedStudyPeriodDays: NaN,
studyBreaks: [
{
breakDays: 9,
breakEndDate: "2023-06-06",
breakStartDate: "2023-05-29",
eligibleBreakDays: 9,
ineligibleBreakDays: 0,
},
{
breakDays: 6,
breakEndDate: "2023-06-13",
breakStartDate: "2023-06-08",
eligibleBreakDays: 6,
ineligibleBreakDays: 0,
},
],
sumOfTotalEligibleBreakDays: 15,
sumOfTotalIneligibleBreakDays: 0,
totalDays: NaN,
totalFundedWeeks: NaN,
unfundedStudyPeriodDays: NaN,
});
});

it("Should calculate funded periods and allowed study breaks when there are data available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "2023-05-29",
studyEndDate: "2023-09-29",
studyBreaks: [
{
breakStartDate: "2023-05-29",
breakEndDate: "2023-06-06",
},
{
breakStartDate: "2023-06-08",
breakEndDate: "2023-06-30",
},
],
};

// Act
const calculatedStudyBreaksAndWeeks =
EducationProgramOfferingService.getCalculatedStudyBreaksAndWeeks(
offeringStudyBreakCalculationContext,
);

// Assert
expect(calculatedStudyBreaksAndWeeks).toEqual({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit of using toStrictEqual instead toEqual for these cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case we can use .toStrictEqual because the service is creating an object literal.

Benefits of using .toEqual:

  • keys with undefined properties are checked, e.g. {a: undefined, b: 2} will not equal {b: 2};
  • undefined items are taken into account, e.g. [2] will not equal [2, undefined];
  • array sparseness is checked, e.g. [, 1] will not equal [undefined, 1];
  • object types are checked, e.g. a class instance with fields a and b will not equal a literal object with fields a and b.

I changed the comparions to .toStrictEqual. Thanks!

allowableStudyBreaksDaysAmount: 12.4,
fundedStudyPeriodDays: 104.4,
studyBreaks: [
{
breakDays: 9,
breakEndDate: "2023-06-06",
breakStartDate: "2023-05-29",
eligibleBreakDays: 9,
ineligibleBreakDays: 0,
},
{
breakDays: 23,
breakEndDate: "2023-06-30",
breakStartDate: "2023-06-08",
eligibleBreakDays: 21,
ineligibleBreakDays: 2,
},
],
sumOfTotalEligibleBreakDays: 30,
sumOfTotalIneligibleBreakDays: 2,
totalDays: 124,
totalFundedWeeks: 15,
unfundedStudyPeriodDays: 19.6,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,7 @@ export class EducationProgramOfferingService extends RecordDataModelService<Educ
newStudyBreak.breakDays = dateDifference(
eachBreak.breakEndDate,
eachBreak.breakStartDate,
true,
);
newStudyBreak.breakStartDate = eachBreak.breakStartDate;
newStudyBreak.breakEndDate = eachBreak.breakEndDate;
Expand All @@ -1279,6 +1280,7 @@ export class EducationProgramOfferingService extends RecordDataModelService<Educ
const totalDays = dateDifference(
offering.studyEndDate,
offering.studyStartDate,
true,
);

// Allowable amount of break days allowed.
Expand Down
5 changes: 4 additions & 1 deletion sources/packages/backend/libs/utilities/src/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@ export const isAfter = (
* Difference in days between endDate and startDate (endDate-startDate).
* @param endDate end date.
* @param startDate start date.
* @param inclusiveDiff whether it should consider start and end date as part of the calculation.
* @returns the date difference in days.
*/
export const dateDifference = (
endDate: string | Date,
startDate: string | Date,
inclusiveDiff?: boolean,
): number => {
return dayjs(endDate).diff(startDate, "days");
const diff = dayjs(endDate).diff(startDate, "days");
return inclusiveDiff ? diff + 1 : diff;
};

/**
Expand Down