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 4 commits
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,247 @@
import { OfferingStudyBreakCalculationContext } from "../../education-program-offering-validation.models";
import { EducationProgramOfferingService } from "../../education-program-offering.service";

describe("EducationProgramOfferingService-getCalculatedStudyBreaksAndWeeks", () => {
it("Should calculate funded study period and total days when study start and end study dates 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 funded study period days and total days when study start date 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 funded study period days and total days when study end date 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 study breaks when start and end 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 not calculate study breaks when start study break date is not available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "",
studyEndDate: "",
studyBreaks: [
{
breakStartDate: "",
breakEndDate: "2023-06-06",
},
],
};

// 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 study breaks when end study break date is not available.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "",
studyEndDate: "",
studyBreaks: [
{
breakStartDate: "2023-06-06",
breakEndDate: "",
},
],
};

// 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 eligible break days as a maximum of 21 days when a study break exceeds it.", () => {
//Arrange
const offeringStudyBreakCalculationContext: OfferingStudyBreakCalculationContext =
{
studyStartDate: "2023-05-29",
studyEndDate: "2023-09-29",
studyBreaks: [
{
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: 113.4,
studyBreaks: [
{
breakDays: 23,
breakEndDate: "2023-06-30",
breakStartDate: "2023-06-08",
eligibleBreakDays: 21,
ineligibleBreakDays: 2,
},
],
sumOfTotalEligibleBreakDays: 21,
sumOfTotalIneligibleBreakDays: 2,
totalDays: 124,
totalFundedWeeks: 17,
unfundedStudyPeriodDays: 10.6,
});
});
});
4 changes: 2 additions & 2 deletions sources/packages/backend/libs/utilities/src/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const isAfter = (
};

/**
* Difference in days between endDate and startDate (endDate-startDate).
* Difference in days between endDate and startDate (endDate-startDate) with start and end date inclusive.
* @param endDate end date.
* @param startDate start date.
* @returns the date difference in days.
Expand All @@ -59,7 +59,7 @@ export const dateDifference = (
endDate: string | Date,
startDate: string | Date,
): number => {
return dayjs(endDate).diff(startDate, "days");
return dayjs(endDate).diff(startDate, "days") + 1;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2771,7 +2771,7 @@
"requireDecimal": false,
"inputFormat": "plain",
"truncateMultipleSpaces": false,
"calculateValue": "value = 0;\r\nif (row.breakStartDate && row.breakEndDate) {\r\n const diffDays = moment(row.breakEndDate, \"YYYY-MM-DD\").diff(\r\n moment(row.breakStartDate, \"YYYY-MM-DD\"),\r\n \"days\"\r\n );\r\n value = Math.max(diffDays, 0);\r\n}\r\n",
"calculateValue": "value = 0;\r\nif (row.breakStartDate && row.breakEndDate) {\r\n const diffDays = moment(row.breakEndDate, \"YYYY-MM-DD\").diff(\r\n moment(row.breakStartDate, \"YYYY-MM-DD\"),\r\n \"days\"\r\n ) + 1 ;\r\n value = Math.max(diffDays, 0);\r\n}\r\n",
"calculateServer": true,
"validate": {
"required": true,
Expand Down