From 411b5a0491601c87c784644f69f5c7c578fe28a5 Mon Sep 17 00:00:00 2001 From: Tejas Nair <85873779+TejasNair9977@users.noreply.github.com> Date: Wed, 8 Nov 2023 16:34:34 +0530 Subject: [PATCH] fixed all testcases --- controller/course.js | 8 +- controller/group.js | 17 ++-- controller/module.js | 2 +- controller/student.js | 10 +-- package.json | 4 +- services/group.js | 4 +- test/routes/course.test.js | 141 ++++++++++++++++++++++++++++++++++ test/routes/group.test.js | 28 +++++-- test/routes/module.test.js | 32 ++++---- test/routes/student.test.js | 50 +++++++----- test/routes/timetable.test.js | 33 ++++++-- 11 files changed, 256 insertions(+), 73 deletions(-) create mode 100644 test/routes/course.test.js diff --git a/controller/course.js b/controller/course.js index b27b73c..6b29ee4 100644 --- a/controller/course.js +++ b/controller/course.js @@ -11,7 +11,6 @@ import Department from "#models/department"; import Module from "#models/module"; import Practical from "#models/practical"; import Tutorial from "#models/tutorial"; -import Assignment from "#models/assignment"; async function addCourse(req, res) { const { @@ -43,16 +42,13 @@ async function addCourse(req, res) { const isModuleValid = await isEntityIdValid(modules, Module); const isPracticalValid = await isEntityIdValid(practicals, Practical); const isTutorialValid = await isEntityIdValid(tutorials, Tutorial); - const isAssignmentValid = await isEntityIdValid(assignments, Assignment); - try { if ( !isSemesterValid || !isDepartmentValid || !isModuleValid || !isPracticalValid || - !isTutorialValid || - !isAssignmentValid + !isTutorialValid ) { res.status(400).json({ error: "Invalid ID(s)", @@ -81,7 +77,7 @@ async function addCourse(req, res) { reccTextbooks, refBooks, ); - res.json({ res: `added course ${newCourse.ERPID}` }); + res.json({ res: `added course ${newCourse.ERPID}`, id: newCourse.id }); } } catch (error) { logger.error("Error while inserting", error); diff --git a/controller/group.js b/controller/group.js index 9623ce3..86c7b4d 100644 --- a/controller/group.js +++ b/controller/group.js @@ -6,18 +6,17 @@ import { } from "#services/group"; import { logger } from "#util"; import { isEntityIdValid } from "#middleware/entityIdValidation"; -import Student from '#models/student'; +import Student from "#models/student"; async function addGroup(req, res) { - const { title, student } = req.body; - const isStudentValid = await isEntityIdValid(student, Student); + const { title, students } = req.body; + const isStudentValid = await isEntityIdValid(students, Student); try { - if(isStudentValid){ - const group = await createGroup(title, student); - res.json({ res: `added group ${group.id}`, id: group.id }); - } - else{ - res.status(400).json({err: "Invalid Id"}); + if (isStudentValid) { + const group = await createGroup(title, students); + res.json({ res: `added group ${group.id}`, id: group.id }); + } else { + res.status(400).json({ err: "Invalid Id" }); } } catch (error) { logger.error("Error while inserting", error); diff --git a/controller/module.js b/controller/module.js index 9340291..c7d3212 100644 --- a/controller/module.js +++ b/controller/module.js @@ -35,7 +35,7 @@ async function addModule(req, res) { ); res.json({ res: `added module ${newModule.name} ${newModule.id}` }); } else { - res.status(400).json({ err: "Invalid name" , err: "Invalid id"}); + res.status(400).json({ cause: "Invalid name", err: "Invalid id" }); } } catch (error) { logger.error("Error while inserting", error); diff --git a/controller/student.js b/controller/student.js index 8b20ecf..7bef762 100644 --- a/controller/student.js +++ b/controller/student.js @@ -27,13 +27,13 @@ async function addStudent(req, res) { ); res.json({ res: `added user ${newStudent.id}`, id: newStudent.id }); } else { - var error = ""; - if (!isBranchValid) error.concat('Invalid branch'); - if (!isCourseValid) error.concat(' Invalid course opted'); + let error = ""; // eslint-disable-line prefer-const + if (!isBranchValid) error.concat("Invalid branch"); + if (!isCourseValid) error.concat(" Invalid course opted"); res.status(400).json({ err: error }); } - } catch (error) { - logger.error("Error while inserting", error); + } catch (caughtError) { + logger.error("Error while inserting", caughtError); res.status(500); res.json({ err: "Error while inserting in DB" }); } diff --git a/package.json b/package.json index 53e7716..2d303d8 100644 --- a/package.json +++ b/package.json @@ -21,10 +21,10 @@ "devstart": "nodemon ./bin/www", "serverstart": "DEBUG=api:* npm run devstart", "serverstartWin": "SET DEBUG=api:* && npm run devstart", - "test": "NODE_OPTIONS=--experimental-vm-modules npx jest --runInBand", + "test": "NODE_OPTIONS=--experimental-vm-modules npx jest", "test:watch": "NODE_OPTIONS=--experimental-vm-modules npx jest --watch", "test:openHandles": "NODE_OPTIONS=--experimental-vm-modules npx jest --detectOpenHandles", - "testWin": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --runInBand", + "testWin": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest", "testWin:watch": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --watch", "testWin:openHandles": "SET NODE_OPTIONS=--experimental-vm-modules && npx jest --detectOpenHandles", "eslint": "eslint", diff --git a/services/group.js b/services/group.js index 18e5575..171a3b9 100644 --- a/services/group.js +++ b/services/group.js @@ -1,10 +1,10 @@ import Group from "#models/group"; import databaseError from "#error/database"; -export async function createGroup(title, student) { +export async function createGroup(title, students) { const newGroup = await Group.create({ title, - student, + students, }); if (newGroup.title === title) { return newGroup; diff --git a/test/routes/course.test.js b/test/routes/course.test.js new file mode 100644 index 0000000..b8fbd2a --- /dev/null +++ b/test/routes/course.test.js @@ -0,0 +1,141 @@ +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies +import connector from "#models/databaseUtil"; +import course from "#models/course"; +import semesterModel from "#models/semester"; +import departmentModel from "#models/department"; +import moduleModel from "#models/module"; +import practicalModel from "#models/practical"; +import tutorialModel from "#models/tutorial"; + +jest.mock("#util"); +const { agent } = global; +let semesterId; +let departmentId; +let moduleIds; +let practicalIds; +let tutorialIds; + +function cleanUp(callback) { + course + .remove({ + semester: semesterId, + }) + .then(() => { + connector.disconnect((DBerr) => { + if (DBerr) console.log("Database disconnect error: ", DBerr); + }); + callback(); + }); +} +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + semesterId = await semesterModel.read({}, 1); + semesterId = semesterId.data[0]._id; + departmentId = await departmentModel.read({}, 1); + departmentId = departmentId.data[0]._id; + moduleIds = await moduleModel.read({}, 3); + moduleIds = moduleIds.data.flatMap((obj) => obj._id); + practicalIds = await practicalModel.read({}, 3); + practicalIds = practicalIds.data.flatMap((obj) => obj._id); + tutorialIds = await tutorialModel.read({}, 3); + tutorialIds = tutorialIds.data.flatMap((obj) => obj._id); + callback(); +} + +beforeAll((done) => { + getIds(done); +}); + +afterAll((done) => { + cleanUp(done); +}); + +describe("Course API", () => { + it("should create course", async () => { + const response = await agent.post("/course/create").send({ + name: "my favourite course", + code: "DDSABUB123", + theoryHours: 12, + tutorialHours: 4, + practicalHours: 3, + ISAMarks: 60, + ESEMarks: 60, + tutorialMarks: 20, + practicalMarks: 20, + semester: semesterId, + department: departmentId, + subType: "open", + prerequisites: ["prereq"], // array of strings + objective: "objective", + outcomes: [ + { + outcome: "outcome 1", + RBTLevel: ["L1", "L2"], + }, + ], // this is the modules from syllabus + modules: moduleIds, + practicals: practicalIds, + tutorials: tutorialIds, + reccTextbooks: ["random book"], + refBooks: ["random book"], + }); + + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/added course/); + }); + + describe("after adding course", () => { + let id; + beforeEach(async () => { + id = await agent.post("/course/create").send({ + name: "my second favourite course", + code: "EEEABUB123", + theoryHours: 12, + tutorialHours: 4, + practicalHours: 3, + ISAMarks: 60, + ESEMarks: 60, + tutorialMarks: 20, + practicalMarks: 20, + semester: semesterId, + department: departmentId, + subType: "open", + prerequisites: ["prereq"], // array of strings + objective: "objective", + outcomes: [ + { + outcome: "outcome 1", + RBTLevel: ["L1", "L2"], + }, + ], // this is the modules from syllabus + modules: moduleIds, + practicals: practicalIds, + tutorials: tutorialIds, + reccTextbooks: ["random book"], + refBooks: ["random book"], + }); + id = JSON.parse(id.res.text).id; + }); + + afterEach(async () => { + await course.remove({ + code: "EEEABUB123", + }); + }); + + it("should read course", async () => { + const response = await agent.get("/course/list"); + expect(response.status).toBe(200); + expect(response.body.res).toBeDefined(); + }); + + it("should update course", async () => { + const response = await agent + .post(`/course/update/${id}`) + .send({ subType: "professional" }); + + expect(response.status).toBe(200); + expect(response.body.res).toMatch(/updated course with id/); + }); + }); +}); diff --git a/test/routes/group.test.js b/test/routes/group.test.js index e36d6a0..461fc2c 100644 --- a/test/routes/group.test.js +++ b/test/routes/group.test.js @@ -1,14 +1,17 @@ import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies import connector from "#models/databaseUtil"; import groupModel from "#models/group"; +import studentModel from "#models/student"; jest.mock("#util"); const { agent } = global; +let studentIds; + function cleanUp(callback) { groupModel .remove({ - title: "Group 1", + student: "Group 1", }) .then(() => { connector.disconnect((DBerr) => { @@ -18,6 +21,17 @@ function cleanUp(callback) { }); } +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + studentIds = await studentModel.read({}, 3); + studentIds = studentIds.data.flatMap((obj) => obj._id); + callback(); +} + +beforeAll((done) => { + getIds(done); +}); + afterAll((done) => { cleanUp(done); }); @@ -26,7 +40,7 @@ describe("group API", () => { it("should create group", async () => { const response = await agent.post("/group/add").send({ title: "Group 1", - student: "64fdc67feca8a69f01b33614", + students: studentIds, }); expect(response.headers["content-type"]).toMatch(/json/); expect(response.status).toBe(200); @@ -38,21 +52,19 @@ describe("group API", () => { beforeEach(async () => { id = await agent.post("/group/add").send({ title: "Group 1", - student: "64fdc67feca8a69f01b33614", + students: studentIds, }); id = JSON.parse(id.res.text).id; }); afterEach(async () => { await groupModel.remove({ - id: "6500594e2b7b532006c073dd", + title: "Group 1", }); }); it("should read group", async () => { - const response = await agent - .get("/group/list") - .send({ name: "Building A" }); + const response = await agent.get("/group/list"); expect(response.status).toBe(200); expect(response.body.res).toBeDefined(); }); @@ -60,7 +72,7 @@ describe("group API", () => { it("should update group", async () => { const response = await agent .post(`/group/update/${id}`) - .send({ title: "Group 1" }, { title: "Group 2" }); + .send({ title: "Group 2" }); expect(response.headers["content-type"]).toMatch(/json/); expect(response.status).toBe(200); expect(response.body.res).toMatch(/updated group/); diff --git a/test/routes/module.test.js b/test/routes/module.test.js index a5e05b4..7ad1e4d 100644 --- a/test/routes/module.test.js +++ b/test/routes/module.test.js @@ -1,10 +1,13 @@ import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies import moduleModel from "#models/module"; import connector from "#models/databaseUtil"; +import topicModel from "#models/topic"; jest.mock("#util"); const { agent } = global; +let topicIds; + function cleanUp(callback) { moduleModel.remove({ startDate: "2023-06-18T14:11:30Z" }).then(() => { connector.disconnect((DBerr) => { @@ -16,6 +19,17 @@ function cleanUp(callback) { }); } +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + topicIds = await topicModel.read({}, 3); + topicIds = topicIds.data.flatMap((obj) => obj._id); + callback(); +} + +beforeAll((done) => { + getIds(done); +}); + afterAll((done) => { cleanUp(done); }); @@ -25,13 +39,9 @@ describe("checking module functions", () => { const response = await agent.post("/module/add").send({ no: 1, name: "Module 1", - contents: [ - "64fc3c8bde9fa947ea1f412f", - "64fc3c8bde9fa947ea1f412f", - "64fc3c8bde9fa947ea1f412f", - ], + contents: topicIds, hrsPerModule: 3, - cognitiveLevels: "L1", + cognitiveLevels: ["L1", "L2"], }); expect(response.status).toBe(200); expect(response.body.res).toMatch(/added module/); @@ -40,14 +50,10 @@ describe("checking module functions", () => { beforeEach(async () => { await agent.post("/module/add").send({ no: 1, - name: "Module 1", - contents: [ - "64fc3c8bde9fa947ea1f412f", - "64fc3c8bde9fa947ea1f412f", - "64fc3c8bde9fa947ea1f412f", - ], + name: "Module 2", + contents: topicIds, hrsPerModule: 3, - cognitiveLevels: "L1", + cognitiveLevels: ["L1", "L2"], }); }); diff --git a/test/routes/student.test.js b/test/routes/student.test.js index 04414dd..a5e6a84 100644 --- a/test/routes/student.test.js +++ b/test/routes/student.test.js @@ -1,20 +1,18 @@ -import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies -import connector from "#models/databaseUtil"; -import studentModel from "#models/student"; +import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies +import connector from "#models/databaseUtil"; +import studentModel from "#models/student"; +import courseModel from "#models/course"; // Update this import +import departmentModel from "#models/department"; jest.mock("#util"); const { agent } = global; +let courseIds; +let branchId; function cleanUp(callback) { studentModel .remove({ - ERPID: "S1032220999", - name: "Arya", - joiningYear: 2020, - branch: "64fc3c8bde9fa947ea1f412f", - division: "A", - rollNo: 12, - coursesOpted: "64fc3c8bde9fa947ea1f412f", + branch: branchId, }) .then(() => { connector.disconnect((DBerr) => { @@ -24,6 +22,18 @@ function cleanUp(callback) { }); } +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + branchId = await departmentModel.read({}); + branchId = branchId.data[0]._id; + courseIds = await courseModel.read({}, 3); + courseIds = courseIds.data.flatMap((obj) => obj._id); + callback(); +} + +beforeAll((done) => { + getIds(done); +}); afterAll((done) => { cleanUp(done); @@ -35,12 +45,12 @@ describe("Student API", () => { ERPID: "S1032220999", name: "Arya", joiningYear: 2020, - branch: "64fc3c8bde9fa947ea1f412f", + branch: branchId, division: "A", rollNo: 12, - coursesOpted: "64fc3c8bde9fa947ea1f412f", + coursesOpted: courseIds, }); - + expect(response.status).toBe(200); expect(response.body.res).toMatch(/added user/); }); @@ -52,10 +62,10 @@ describe("Student API", () => { ERPID: "S1032220999", name: "Arya", joiningYear: 2020, - branch: "64fc3c8bde9fa947ea1f412f", + branch: branchId, division: "A", rollNo: 12, - coursesOpted: "64fc3c8bde9fa947ea1f412f", + coursesOpted: courseIds, }); id = JSON.parse(id.res.text).id; }); @@ -65,17 +75,15 @@ describe("Student API", () => { ERPID: "S1032220999", name: "Arya", joiningYear: 2020, - branch: "64fc3c8bde9fa947ea1f412f", + branch: branchId, division: "A", rollNo: 12, - coursesOpted: "64fc3c8bde9fa947ea1f412f", + coursesOpted: courseIds, }); }); it("should read student", async () => { - const response = await agent - .get("/student/list") - .send({ name: "Arya" }); + const response = await agent.get("/student/list").send({ name: "Arya" }); expect(response.status).toBe(200); expect(response.body.res).toBeDefined(); }); @@ -83,7 +91,7 @@ describe("Student API", () => { it("should update student", async () => { const response = await agent .post(`/student/update/${id}`) - .send({ERPID: "S1032220999"},{ joiningYear: 2021 }); + .send({ ERPID: "S1032220999" }, { joiningYear: 2021 }); expect(response.status).toBe(200); expect(response.body.res).toMatch(`updated Student with id ${id}`); diff --git a/test/routes/timetable.test.js b/test/routes/timetable.test.js index 1a88561..69de606 100644 --- a/test/routes/timetable.test.js +++ b/test/routes/timetable.test.js @@ -1,10 +1,17 @@ import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies import timetableModel from "#models/timetable"; import connector from "#models/databaseUtil"; +import facultyModel from "#models/faculty"; +import groupModel from "#models/group"; +import activityBPMode from "#models/activityBlueprint"; jest.mock("#util"); const { agent } = global; +let facultyId; +let groupId; +let activityBPId; + function cleanUp(callback) { timetableModel.remove({ lunchbreakStartTime: "test:45 PM" }).then(() => { connector.disconnect((DBerr) => { @@ -13,6 +20,20 @@ function cleanUp(callback) { }); }); } +/* eslint-disable no-underscore-dangle */ +async function getIds(callback) { + facultyId = await facultyModel.read({}, 1); + facultyId = facultyId.data[0]._id; + groupId = await groupModel.read({}, 1); + groupId = groupId.data[0]._id; + activityBPId = await activityBPMode.read({}, 1); + activityBPId = activityBPId.data[0]._id; + callback(); +} + +beforeAll((done) => { + getIds(done); +}); afterAll((done) => { cleanUp(done); @@ -23,9 +44,9 @@ describe("checking timetable functions", () => { const response = await agent.post("/timetable/add").send({ startDate: "2023-06-18T14:11:30Z", endDate: "2023-05-28T14:10:30Z", - classIncharge: "60a0e7e9a09c3f001c834e06", - group: "60a0e7e9a09c3f001c834e07", - activityBlueprints: "60a0e7e9a09c3f001c834e08", + classIncharge: facultyId, + group: groupId, + activityBlueprints: activityBPId, lunchbreakStartTime: "test:45 PM", lunchbreakDuration: 45, // minutes teabreakStartTime: "11:30 AM", @@ -40,9 +61,9 @@ describe("checking timetable functions", () => { id = await agent.post("/timetable/add").send({ startDate: "2023-06-18T14:11:30Z", endDate: "2023-05-28T14:10:30Z", - classIncharge: "60a0e7e9a09c3f001c834e06", - group: "60a0e7e9a09c3f001c834e07", - activityBlueprints: "60a0e7e9a09c3f001c834e08", + classIncharge: facultyId, + group: groupId, + activityBlueprints: activityBPId, lunchbreakStartTime: "test:45 PM", lunchbreakDuration: 45, // minutes teabreakStartTime: "11:30 AM",