Skip to content

Commit 09824b0

Browse files
Merge pull request #449 from tcet-opensource/testcasehotfix
fixed all testcases
2 parents 7633aaf + 411b5a0 commit 09824b0

File tree

11 files changed

+256
-73
lines changed

11 files changed

+256
-73
lines changed

controller/course.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Department from "#models/department";
1111
import Module from "#models/module";
1212
import Practical from "#models/practical";
1313
import Tutorial from "#models/tutorial";
14-
import Assignment from "#models/assignment";
1514

1615
async function addCourse(req, res) {
1716
const {
@@ -43,16 +42,13 @@ async function addCourse(req, res) {
4342
const isModuleValid = await isEntityIdValid(modules, Module);
4443
const isPracticalValid = await isEntityIdValid(practicals, Practical);
4544
const isTutorialValid = await isEntityIdValid(tutorials, Tutorial);
46-
const isAssignmentValid = await isEntityIdValid(assignments, Assignment);
47-
4845
try {
4946
if (
5047
!isSemesterValid ||
5148
!isDepartmentValid ||
5249
!isModuleValid ||
5350
!isPracticalValid ||
54-
!isTutorialValid ||
55-
!isAssignmentValid
51+
!isTutorialValid
5652
) {
5753
res.status(400).json({
5854
error: "Invalid ID(s)",
@@ -81,7 +77,7 @@ async function addCourse(req, res) {
8177
reccTextbooks,
8278
refBooks,
8379
);
84-
res.json({ res: `added course ${newCourse.ERPID}` });
80+
res.json({ res: `added course ${newCourse.ERPID}`, id: newCourse.id });
8581
}
8682
} catch (error) {
8783
logger.error("Error while inserting", error);

controller/group.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ import {
66
} from "#services/group";
77
import { logger } from "#util";
88
import { isEntityIdValid } from "#middleware/entityIdValidation";
9-
import Student from '#models/student';
9+
import Student from "#models/student";
1010

1111
async function addGroup(req, res) {
12-
const { title, student } = req.body;
13-
const isStudentValid = await isEntityIdValid(student, Student);
12+
const { title, students } = req.body;
13+
const isStudentValid = await isEntityIdValid(students, Student);
1414
try {
15-
if(isStudentValid){
16-
const group = await createGroup(title, student);
17-
res.json({ res: `added group ${group.id}`, id: group.id });
18-
}
19-
else{
20-
res.status(400).json({err: "Invalid Id"});
15+
if (isStudentValid) {
16+
const group = await createGroup(title, students);
17+
res.json({ res: `added group ${group.id}`, id: group.id });
18+
} else {
19+
res.status(400).json({ err: "Invalid Id" });
2120
}
2221
} catch (error) {
2322
logger.error("Error while inserting", error);

controller/module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async function addModule(req, res) {
3535
);
3636
res.json({ res: `added module ${newModule.name} ${newModule.id}` });
3737
} else {
38-
res.status(400).json({ err: "Invalid name" , err: "Invalid id"});
38+
res.status(400).json({ cause: "Invalid name", err: "Invalid id" });
3939
}
4040
} catch (error) {
4141
logger.error("Error while inserting", error);

controller/student.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ async function addStudent(req, res) {
2727
);
2828
res.json({ res: `added user ${newStudent.id}`, id: newStudent.id });
2929
} else {
30-
var error = "";
31-
if (!isBranchValid) error.concat('Invalid branch');
32-
if (!isCourseValid) error.concat(' Invalid course opted');
30+
let error = ""; // eslint-disable-line prefer-const
31+
if (!isBranchValid) error.concat("Invalid branch");
32+
if (!isCourseValid) error.concat(" Invalid course opted");
3333
res.status(400).json({ err: error });
3434
}
35-
} catch (error) {
36-
logger.error("Error while inserting", error);
35+
} catch (caughtError) {
36+
logger.error("Error while inserting", caughtError);
3737
res.status(500);
3838
res.json({ err: "Error while inserting in DB" });
3939
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
"devstart": "nodemon ./bin/www",
2222
"serverstart": "DEBUG=api:* npm run devstart",
2323
"serverstartWin": "SET DEBUG=api:* && npm run devstart",
24-
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand",
24+
"test": "NODE_OPTIONS=--experimental-vm-modules npx jest",
2525
"test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --watch",
2626
"test:openHandles": "NODE_OPTIONS=--experimental-vm-modules npx jest --detectOpenHandles",
27-
"testWin": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --runInBand",
27+
"testWin": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest",
2828
"testWin:watch": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --watch",
2929
"testWin:openHandles": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --detectOpenHandles",
3030
"eslint": "eslint",

services/group.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import Group from "#models/group";
22
import databaseError from "#error/database";
33

4-
export async function createGroup(title, student) {
4+
export async function createGroup(title, students) {
55
const newGroup = await Group.create({
66
title,
7-
student,
7+
students,
88
});
99
if (newGroup.title === title) {
1010
return newGroup;

test/routes/course.test.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2+
import connector from "#models/databaseUtil";
3+
import course from "#models/course";
4+
import semesterModel from "#models/semester";
5+
import departmentModel from "#models/department";
6+
import moduleModel from "#models/module";
7+
import practicalModel from "#models/practical";
8+
import tutorialModel from "#models/tutorial";
9+
10+
jest.mock("#util");
11+
const { agent } = global;
12+
let semesterId;
13+
let departmentId;
14+
let moduleIds;
15+
let practicalIds;
16+
let tutorialIds;
17+
18+
function cleanUp(callback) {
19+
course
20+
.remove({
21+
semester: semesterId,
22+
})
23+
.then(() => {
24+
connector.disconnect((DBerr) => {
25+
if (DBerr) console.log("Database disconnect error: ", DBerr);
26+
});
27+
callback();
28+
});
29+
}
30+
/* eslint-disable no-underscore-dangle */
31+
async function getIds(callback) {
32+
semesterId = await semesterModel.read({}, 1);
33+
semesterId = semesterId.data[0]._id;
34+
departmentId = await departmentModel.read({}, 1);
35+
departmentId = departmentId.data[0]._id;
36+
moduleIds = await moduleModel.read({}, 3);
37+
moduleIds = moduleIds.data.flatMap((obj) => obj._id);
38+
practicalIds = await practicalModel.read({}, 3);
39+
practicalIds = practicalIds.data.flatMap((obj) => obj._id);
40+
tutorialIds = await tutorialModel.read({}, 3);
41+
tutorialIds = tutorialIds.data.flatMap((obj) => obj._id);
42+
callback();
43+
}
44+
45+
beforeAll((done) => {
46+
getIds(done);
47+
});
48+
49+
afterAll((done) => {
50+
cleanUp(done);
51+
});
52+
53+
describe("Course API", () => {
54+
it("should create course", async () => {
55+
const response = await agent.post("/course/create").send({
56+
name: "my favourite course",
57+
code: "DDSABUB123",
58+
theoryHours: 12,
59+
tutorialHours: 4,
60+
practicalHours: 3,
61+
ISAMarks: 60,
62+
ESEMarks: 60,
63+
tutorialMarks: 20,
64+
practicalMarks: 20,
65+
semester: semesterId,
66+
department: departmentId,
67+
subType: "open",
68+
prerequisites: ["prereq"], // array of strings
69+
objective: "objective",
70+
outcomes: [
71+
{
72+
outcome: "outcome 1",
73+
RBTLevel: ["L1", "L2"],
74+
},
75+
], // this is the modules from syllabus
76+
modules: moduleIds,
77+
practicals: practicalIds,
78+
tutorials: tutorialIds,
79+
reccTextbooks: ["random book"],
80+
refBooks: ["random book"],
81+
});
82+
83+
expect(response.status).toBe(200);
84+
expect(response.body.res).toMatch(/added course/);
85+
});
86+
87+
describe("after adding course", () => {
88+
let id;
89+
beforeEach(async () => {
90+
id = await agent.post("/course/create").send({
91+
name: "my second favourite course",
92+
code: "EEEABUB123",
93+
theoryHours: 12,
94+
tutorialHours: 4,
95+
practicalHours: 3,
96+
ISAMarks: 60,
97+
ESEMarks: 60,
98+
tutorialMarks: 20,
99+
practicalMarks: 20,
100+
semester: semesterId,
101+
department: departmentId,
102+
subType: "open",
103+
prerequisites: ["prereq"], // array of strings
104+
objective: "objective",
105+
outcomes: [
106+
{
107+
outcome: "outcome 1",
108+
RBTLevel: ["L1", "L2"],
109+
},
110+
], // this is the modules from syllabus
111+
modules: moduleIds,
112+
practicals: practicalIds,
113+
tutorials: tutorialIds,
114+
reccTextbooks: ["random book"],
115+
refBooks: ["random book"],
116+
});
117+
id = JSON.parse(id.res.text).id;
118+
});
119+
120+
afterEach(async () => {
121+
await course.remove({
122+
code: "EEEABUB123",
123+
});
124+
});
125+
126+
it("should read course", async () => {
127+
const response = await agent.get("/course/list");
128+
expect(response.status).toBe(200);
129+
expect(response.body.res).toBeDefined();
130+
});
131+
132+
it("should update course", async () => {
133+
const response = await agent
134+
.post(`/course/update/${id}`)
135+
.send({ subType: "professional" });
136+
137+
expect(response.status).toBe(200);
138+
expect(response.body.res).toMatch(/updated course with id/);
139+
});
140+
});
141+
});

test/routes/group.test.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
22
import connector from "#models/databaseUtil";
33
import groupModel from "#models/group";
4+
import studentModel from "#models/student";
45

56
jest.mock("#util");
67
const { agent } = global;
78

9+
let studentIds;
10+
811
function cleanUp(callback) {
912
groupModel
1013
.remove({
11-
title: "Group 1",
14+
student: "Group 1",
1215
})
1316
.then(() => {
1417
connector.disconnect((DBerr) => {
@@ -18,6 +21,17 @@ function cleanUp(callback) {
1821
});
1922
}
2023

24+
/* eslint-disable no-underscore-dangle */
25+
async function getIds(callback) {
26+
studentIds = await studentModel.read({}, 3);
27+
studentIds = studentIds.data.flatMap((obj) => obj._id);
28+
callback();
29+
}
30+
31+
beforeAll((done) => {
32+
getIds(done);
33+
});
34+
2135
afterAll((done) => {
2236
cleanUp(done);
2337
});
@@ -26,7 +40,7 @@ describe("group API", () => {
2640
it("should create group", async () => {
2741
const response = await agent.post("/group/add").send({
2842
title: "Group 1",
29-
student: "64fdc67feca8a69f01b33614",
43+
students: studentIds,
3044
});
3145
expect(response.headers["content-type"]).toMatch(/json/);
3246
expect(response.status).toBe(200);
@@ -38,29 +52,27 @@ describe("group API", () => {
3852
beforeEach(async () => {
3953
id = await agent.post("/group/add").send({
4054
title: "Group 1",
41-
student: "64fdc67feca8a69f01b33614",
55+
students: studentIds,
4256
});
4357
id = JSON.parse(id.res.text).id;
4458
});
4559

4660
afterEach(async () => {
4761
await groupModel.remove({
48-
id: "6500594e2b7b532006c073dd",
62+
title: "Group 1",
4963
});
5064
});
5165

5266
it("should read group", async () => {
53-
const response = await agent
54-
.get("/group/list")
55-
.send({ name: "Building A" });
67+
const response = await agent.get("/group/list");
5668
expect(response.status).toBe(200);
5769
expect(response.body.res).toBeDefined();
5870
});
5971

6072
it("should update group", async () => {
6173
const response = await agent
6274
.post(`/group/update/${id}`)
63-
.send({ title: "Group 1" }, { title: "Group 2" });
75+
.send({ title: "Group 2" });
6476
expect(response.headers["content-type"]).toMatch(/json/);
6577
expect(response.status).toBe(200);
6678
expect(response.body.res).toMatch(/updated group/);

0 commit comments

Comments
 (0)