Skip to content
Merged
193 changes: 193 additions & 0 deletions course-matrix/backend/__tests__/getMinHourDay.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { describe, expect, it, test } from "@jest/globals";

import { Offering } from "../src/types/generatorTypes";
import {
createOffering,
getMinHour,
getMinHourDay,
} from "../src/utils/generatorHelpers";

describe("getMinHourDay function", () => {
it("Back to back to back courses", async () => {
const offering1: Offering = createOffering({
id: 1,
course_id: 101,
day: "MO",
start: "09:00:00",
end: "10:00:00",
});
const offering2: Offering = createOffering({
id: 2,
course_id: 102,
day: "MO",
start: "10:00:00",
end: "11:00:00",
});
const offering3: Offering = createOffering({
id: 3,
course_id: 103,
day: "MO",
start: "11:00:00",
end: "12:00:00",
});
const schedule: Offering[] = [offering1, offering2, offering3];

const result = getMinHourDay(schedule, 0);

expect(result).toBe(true);
});

it("courses that has a max gap of 4 hours", async () => {
const offering1: Offering = createOffering({
id: 1,
course_id: 101,
day: "MO",
start: "09:00:00",
end: "10:00:00",
});
const offering2: Offering = createOffering({
id: 2,
course_id: 102,
day: "MO",
start: "10:00:00",
end: "11:00:00",
});
const offering3: Offering = createOffering({
id: 3,
course_id: 103,
day: "MO",
start: "15:00:00",
end: "16:00:00",
});
const schedule: Offering[] = [offering3, offering2, offering1];

const result = getMinHourDay(schedule, 3);

expect(result).toBe(false);
});

it("only 1 offering in list, return 0", async () => {
const offering1: Offering = createOffering({
id: 1,
course_id: 101,
day: "MO",
start: "09:00:00",
end: "10:00:00",
});
const schedule: Offering[] = [offering1];

const result = getMinHourDay(schedule, 23);

expect(result).toBe(true);
});

it("getMinHour test", async () => {
const arr_day = [
"MO",
"MO",
"TU",
"TH",
"FR",
"MO",
"TU",
"TH",
"MO",
"MO",
];
const arr_start = [
"09:00:00",
"10:00:00",
"09:00:00",
"12:00:00",
"13:00:00",
"12:00:00",
"14:00:00",
"16:00:00",
"13:00:00",
"15:00:00",
];
const arr_end = [
"10:00:00",
"11:00:00",
"10:00:00",
"15:00:00",
"16:00:00",
"13:00:00",
"19:00:00",
"18:00:00",
"14:00:00",
"18:00:00",
];
const schedule: Offering[] = [];
for (let i = 0; i < 10; i++) {
schedule.push(
createOffering({
id: i,
course_id: 100 + i,
day: arr_day[i],
start: arr_start[i],
end: arr_end[i],
}),
);
}

const result = getMinHour(schedule, 4);

expect(result).toEqual(true);
});

it("getMinHour test 2", async () => {
const arr_day = [
"MO",
"MO",
"TU",
"TH",
"FR",
"MO",
"TU",
"TH",
"MO",
"MO",
];
const arr_start = [
"09:00:00",
"10:00:00",
"09:00:00",
"12:00:00",
"13:00:00",
"12:00:00",
"14:00:00",
"16:00:00",
"13:00:00",
"15:00:00",
];
const arr_end = [
"10:00:00",
"11:00:00",
"10:00:00",
"15:00:00",
"16:00:00",
"13:00:00",
"19:00:00",
"18:00:00",
"14:00:00",
"18:00:00",
];
const schedule: Offering[] = [];
for (let i = 0; i < 10; i++) {
schedule.push(
createOffering({
id: i,
course_id: 100 + i,
day: arr_day[i],
start: arr_start[i],
end: arr_end[i],
}),
);
}

const result = getMinHour(schedule, 3);

expect(result).toEqual(false);
});
});
8 changes: 7 additions & 1 deletion course-matrix/backend/__tests__/isValidOffering.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { describe, expect, it, test } from "@jest/globals";

import { createOffering, isValidOffering } from "../src/utils/generatorHelpers";
import {
Offering,
Restriction,
RestrictionType,
} from "../src/types/generatorTypes";
import { createOffering, isValidOffering } from "../src/utils/generatorHelpers";

describe("isValidOffering", () => {
const sampleOffering: Offering = createOffering({
Expand All @@ -29,6 +29,7 @@ describe("isValidOffering", () => {
endTime: "09:00:00",
disabled: true,
numDays: 0,
maxGap: 24,
},
];
expect(isValidOffering(sampleOffering, restrictions)).toBe(true);
Expand All @@ -43,6 +44,7 @@ describe("isValidOffering", () => {
endTime: "11:00:00",
disabled: false,
numDays: 0,
maxGap: 24,
},
];
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
Expand All @@ -57,6 +59,7 @@ describe("isValidOffering", () => {
endTime: "",
disabled: false,
numDays: 0,
maxGap: 24,
},
];
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
Expand All @@ -71,6 +74,7 @@ describe("isValidOffering", () => {
endTime: "12:00:00",
disabled: false,
numDays: 0,
maxGap: 24,
},
];
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
Expand All @@ -85,6 +89,7 @@ describe("isValidOffering", () => {
endTime: "",
disabled: false,
numDays: 0,
maxGap: 24,
},
];
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
Expand All @@ -99,6 +104,7 @@ describe("isValidOffering", () => {
endTime: "",
disabled: false,
numDays: 0,
maxGap: 24,
},
];
expect(isValidOffering(sampleOffering, restrictions)).toBe(true);
Expand Down
13 changes: 10 additions & 3 deletions course-matrix/backend/src/constants/availableFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import {
categorizeValidOfferings,
getFreq,
getMaxDays,
getMaxHour,
getValidOfferings,
groupOfferings,
trim,
shuffle,
} from "../utils/generatorHelpers";

// Add all possible function names here
Expand Down Expand Up @@ -199,6 +201,7 @@ export const availableFunctions: AvailableFunctions = {
const courseOfferingsList: OfferingList[] = [];
const validCourseOfferingsList: GroupedOfferingList[] = [];
const maxdays = getMaxDays(restrictions);
const maxhours = getMaxHour(restrictions);
const validSchedules: Offering[][] = [];
// Fetch offerings for each course
for (const course of courses) {
Expand Down Expand Up @@ -244,19 +247,22 @@ export const availableFunctions: AvailableFunctions = {
validCourseOfferingsList.push(groupedOfferings);
}

const categorizedOfferings = categorizeValidOfferings(
let categorizedOfferings = categorizeValidOfferings(
validCourseOfferingsList,
);
// console.log(JSON.stringify(categorizedOfferings));
// Generate valid schedules for the given courses and restrictions
await getValidSchedules(
categorizedOfferings = shuffle(categorizedOfferings);
getValidSchedules(
validSchedules,
categorizedOfferings,
[],
0,
categorizedOfferings.length,
maxdays,
maxhours,
false,
);

// Return error if no valid schedules are found
if (validSchedules.length === 0) {
return { status: 404, error: "No valid schedules found." };
Expand Down Expand Up @@ -445,6 +451,7 @@ ${offeringData.meeting_section} `;
disabled: restriction?.disabled,
num_days: restriction?.numDays,
calendar_id: timetableData?.id,
max_gap: restriction?.maxGap,
},
])
.select();
Expand Down
Loading