Skip to content

Commit 543f8ca

Browse files
committed
refactored the test files in backend
1 parent 00ea3fb commit 543f8ca

15 files changed

+1127
-18
lines changed

course-matrix/backend/__tests__/analyzeQuery.test.ts renamed to course-matrix/backend/__tests__/unit-tests/analyzeQuery.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { analyzeQuery } from "../src/utils/analyzeQuery";
1+
import { analyzeQuery } from "../../src/utils/analyzeQuery";
22
import { describe, test, expect, jest } from "@jest/globals";
33
import {
44
NAMESPACE_KEYWORDS,
55
ASSISTANT_TERMS,
66
DEPARTMENT_CODES,
7-
} from "../src/constants/promptKeywords";
7+
} from "../../src/constants/promptKeywords";
88

99
// Mock the constants if needed
10-
jest.mock("../src/constants/promptKeywords", () => ({
10+
jest.mock("../../src/constants/promptKeywords", () => ({
1111
NAMESPACE_KEYWORDS: {
1212
courses_v3: ["course", "class", "description"],
1313
offerings: ["offering", "schedule", "timetable"],

course-matrix/backend/__tests__/auth.test.ts renamed to course-matrix/backend/__tests__/unit-tests/auth.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import request from "supertest";
22
import { describe, expect, it, test } from "@jest/globals";
3-
import app from "../src/index";
3+
import app from "../../src/index";
44

55
describe("Authentication API", () => {
66
// The unit tests below are currently commented out because they require a database connection.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { describe, expect, it, test } from "@jest/globals";
2+
3+
import { Offering } from "../../src/types/generatorTypes";
4+
import {
5+
canInsert,
6+
canInsertList,
7+
createOffering,
8+
} from "../../src/utils/generatorHelpers";
9+
10+
describe("canInsert function", () => {
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: "MO",
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: "MO",
29+
start: "11:00:00",
30+
end: "12:00:00",
31+
});
32+
33+
it("should return true if there is no overlap with existing offerings", async () => {
34+
const toInsert: Offering = createOffering({
35+
id: 4,
36+
course_id: 104,
37+
day: "MO",
38+
start: "12:00:00",
39+
end: "13:00:00",
40+
});
41+
const curList: Offering[] = [offering1, offering2, offering3];
42+
43+
const result = await canInsert(toInsert, curList);
44+
45+
expect(result).toBe(true); // No overlap, should return true
46+
});
47+
48+
it("should return false if there is an overlap with an existing offering", async () => {
49+
const toInsert: Offering = createOffering({
50+
id: 4,
51+
course_id: 104,
52+
day: "MO",
53+
start: "09:30:00",
54+
end: "10:30:00",
55+
});
56+
const curList: Offering[] = [offering1, offering2, offering3];
57+
58+
const result = await canInsert(toInsert, curList);
59+
60+
expect(result).toBe(false); // There is an overlap with offering1, should return false
61+
});
62+
63+
it("should return true if the new offering starts after the last one ends", async () => {
64+
const toInsert: Offering = createOffering({
65+
id: 4,
66+
course_id: 104,
67+
day: "MO",
68+
start: "13:00:00",
69+
end: "14:00:00",
70+
});
71+
const curList: Offering[] = [offering1, offering2, offering3];
72+
73+
const result = await canInsert(toInsert, curList);
74+
75+
expect(result).toBe(true); // No overlap, should return true
76+
});
77+
78+
it("should return true if the new offering ends before the first one starts", async () => {
79+
const toInsert: Offering = createOffering({
80+
id: 4,
81+
course_id: 104,
82+
day: "MO",
83+
start: "07:00:00",
84+
end: "08:00:00",
85+
});
86+
const curList: Offering[] = [offering1, offering2, offering3];
87+
88+
const result = await canInsert(toInsert, curList);
89+
90+
expect(result).toBe(true); // No overlap, should return true
91+
});
92+
93+
it("should return false if the new offering is completely inside an existing one", async () => {
94+
const toInsert: Offering = createOffering({
95+
id: 4,
96+
course_id: 104,
97+
day: "MO",
98+
start: "09:30:00",
99+
end: "09:45:00",
100+
});
101+
const curList: Offering[] = [offering1, offering2, offering3];
102+
103+
const result = await canInsert(toInsert, curList);
104+
105+
expect(result).toBe(false); // Overlaps with offering1, should return false
106+
});
107+
108+
it("should return true if the day is different (no overlap)", async () => {
109+
const toInsert: Offering = createOffering({
110+
id: 4,
111+
course_id: 104,
112+
day: "TU",
113+
start: "09:00:00",
114+
end: "10:00:00",
115+
});
116+
const curList: Offering[] = [offering1, offering2, offering3];
117+
118+
const result = await canInsert(toInsert, curList);
119+
120+
expect(result).toBe(true); // Different day, no overlap
121+
});
122+
123+
it("special case", async () => {
124+
const toInsert: Offering = createOffering({
125+
id: 1069,
126+
course_id: 1271,
127+
day: "TH",
128+
start: "05:00:00",
129+
end: "17:00:00",
130+
});
131+
const offering11: Offering = createOffering({
132+
id: 414,
133+
course_id: 337,
134+
day: "TU",
135+
start: "15:00:00",
136+
end: "16:00:00",
137+
});
138+
const offering12: Offering = createOffering({
139+
id: 415,
140+
course_id: 337,
141+
day: "TH",
142+
start: "15:00:00",
143+
end: "17:00:00",
144+
});
145+
const offering13: Offering = createOffering({
146+
id: 1052,
147+
course_id: 1271,
148+
day: "TU",
149+
start: "10:00:00",
150+
end: "11:00:00",
151+
});
152+
const offering14: Offering = createOffering({
153+
id: 1053,
154+
course_id: 1271,
155+
day: "TU",
156+
start: "09:00:00",
157+
end: "11:00:00",
158+
});
159+
const curList: Offering[] = [
160+
offering11,
161+
offering12,
162+
offering13,
163+
offering14,
164+
];
165+
166+
const result = await canInsertList([toInsert], curList);
167+
168+
expect(result).toBe(false); // Special bug-causing case
169+
});
170+
});

course-matrix/backend/__tests__/correctDay.test.ts renamed to course-matrix/backend/__tests__/unit-tests/correctDay.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, jest, test } from "@jest/globals";
2-
import { isDateBetween } from "../src/utils/compareDates";
2+
import { isDateBetween } from "../../src/utils/compareDates";
33

44
// For testing purposes, we need to modify the function to accept a custom "now" date
55
// This allows us to test all scenarios regardless of the current date
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { describe, expect, it, test } from "@jest/globals";
2+
3+
import {
4+
createOffering,
5+
getFrequencyTable,
6+
} from "../../src/utils/generatorHelpers";
7+
import { Offering } from "../../src/types/generatorTypes";
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([
62+
offering1,
63+
offering2,
64+
offering3,
65+
offering4,
66+
offering5,
67+
offering6,
68+
offering7,
69+
]);
70+
71+
expect(result.get("MO")).toBe(2);
72+
expect(result.get("TU")).toBe(2);
73+
expect(result.get("WE")).toBe(3);
74+
expect(result.get("TH")).toBeUndefined(); // Day not in data
75+
expect(result.get("FR")).toBeUndefined(); // Day not in data
76+
expect(result.size).toBe(3);
77+
});
78+
79+
test("should return an empty map for an empty array", () => {
80+
const result = getFrequencyTable([]);
81+
expect(result.size).toBe(0);
82+
});
83+
});

0 commit comments

Comments
 (0)