Skip to content

Commit d6a653f

Browse files
committed
Main implementation for timetable generation is completed
Also added unit tests for 2 of the crucial helper functions.
1 parent dc10d32 commit d6a653f

File tree

3 files changed

+223
-29
lines changed

3 files changed

+223
-29
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { describe, expect, it, test } from "@jest/globals";
2+
3+
import {
4+
createOffering,
5+
Offering,
6+
getFrequencyTable,
7+
} from "../src/controllers/generatorController";
8+
9+
describe("getFrequencyTable", () => {
10+
test("should return a frequency map of days", () => {
11+
const offering1: Offering = createOffering({
12+
id: 1,
13+
course_id: 101,
14+
day: "MO",
15+
start: "09:00:00",
16+
end: "10:00:00",
17+
});
18+
const offering2: Offering = createOffering({
19+
id: 2,
20+
course_id: 102,
21+
day: "TU",
22+
start: "10:00:00",
23+
end: "11:00:00",
24+
});
25+
const offering3: Offering = createOffering({
26+
id: 3,
27+
course_id: 103,
28+
day: "TU",
29+
start: "11:00:00",
30+
end: "12:00:00",
31+
});
32+
const offering4: Offering = createOffering({
33+
id: 4,
34+
course_id: 104,
35+
day: "MO",
36+
start: "11:00:00",
37+
end: "12:00:00",
38+
});
39+
const offering5: Offering = createOffering({
40+
id: 5,
41+
course_id: 105,
42+
day: "WE",
43+
start: "11:00:00",
44+
end: "12:00:00",
45+
});
46+
const offering6: Offering = createOffering({
47+
id: 6,
48+
course_id: 106,
49+
day: "WE",
50+
start: "11:00:00",
51+
end: "12:00:00",
52+
});
53+
const offering7: Offering = createOffering({
54+
id: 7,
55+
course_id: 107,
56+
day: "WE",
57+
start: "11:00:00",
58+
end: "12:00:00",
59+
});
60+
61+
const result = getFrequencyTable([offering1, offering2, offering3, offering4, offering5, offering6, offering7]);
62+
63+
expect(result.get("MO")).toBe(2);
64+
expect(result.get("TU")).toBe(2);
65+
expect(result.get("WE")).toBe(3);
66+
expect(result.get("TH")).toBeUndefined();// Day not in data
67+
expect(result.get("FR")).toBeUndefined();// Day not in data
68+
expect(result.size).toBe(3);
69+
});
70+
71+
test("should return an empty map for an empty array", () => {
72+
const result = getFrequencyTable([]);
73+
expect(result.size).toBe(0);
74+
});
75+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expect, it, test } from "@jest/globals";
2+
3+
import {
4+
createOffering,
5+
Offering,
6+
isValidOffering,
7+
Restriction,
8+
RestrictionType
9+
} from "../src/controllers/generatorController";
10+
11+
describe("isValidOffering", () => {
12+
const sampleOffering: Offering = createOffering({
13+
id: 1,
14+
course_id: 101,
15+
day: "MO",
16+
start: "10:00:00",
17+
end: "11:00:00",
18+
});
19+
20+
test("should allow offering if there are no restrictions", () => {
21+
expect(isValidOffering(sampleOffering, [])).toBe(true);
22+
});
23+
24+
test("should allow offering if all restrictions are disabled", () => {
25+
const restrictions: Restriction[] = [
26+
{ type: RestrictionType.RestrictBefore, days: [], startTime: "09:00:00", endTime: "", disabled: true, numDays: 0 },
27+
];
28+
expect(isValidOffering(sampleOffering, restrictions)).toBe(true);
29+
});
30+
31+
test("should reject offering if it starts before restriction start time", () => {
32+
const restrictions: Restriction[] = [
33+
{ type: RestrictionType.RestrictBefore, days: [], startTime: "10:30:00", endTime: "", disabled: false, numDays: 0 },
34+
];
35+
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
36+
});
37+
38+
test("should reject offering if it ends after restriction end time", () => {
39+
const restrictions: Restriction[] = [
40+
{ type: RestrictionType.RestrictAfter, days: [], startTime: "", endTime: "10:30:00", disabled: false, numDays: 0 },
41+
];
42+
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
43+
});
44+
45+
test("should reject offering if it is within restricted time range", () => {
46+
const restrictions: Restriction[] = [
47+
{ type: RestrictionType.RestrictBetween, days: [], startTime: "09:00:00", endTime: "12:00:00", disabled: false, numDays: 0 },
48+
];
49+
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
50+
});
51+
52+
test("should reject offering if the day is restricted", () => {
53+
const restrictions: Restriction[] = [
54+
{ type: RestrictionType.RestrictDay, days: ["MO"], startTime: "", endTime: "", disabled: false, numDays: 0 },
55+
];
56+
expect(isValidOffering(sampleOffering, restrictions)).toBe(false);
57+
});
58+
59+
test("should allow offering if the day is not restricted", () => {
60+
const restrictions: Restriction[] = [
61+
{ type: RestrictionType.RestrictDay, days: ["TU"], startTime: "", endTime: "", disabled: false, numDays: 0 },
62+
];
63+
expect(isValidOffering(sampleOffering, restrictions)).toBe(true);
64+
});
65+
});

0 commit comments

Comments
 (0)