Skip to content

Commit

Permalink
Merge pull request #449 from tcet-opensource/testcasehotfix
Browse files Browse the repository at this point in the history
fixed all testcases
  • Loading branch information
TejasNair9977 authored Nov 8, 2023
2 parents 7633aaf + 411b5a0 commit 09824b0
Show file tree
Hide file tree
Showing 11 changed files with 256 additions and 73 deletions.
8 changes: 2 additions & 6 deletions controller/course.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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);
Expand Down
17 changes: 8 additions & 9 deletions controller/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion controller/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions controller/student.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions services/group.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
141 changes: 141 additions & 0 deletions test/routes/course.test.js
Original file line number Diff line number Diff line change
@@ -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/);
});
});
});
28 changes: 20 additions & 8 deletions test/routes/group.test.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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);
});
Expand All @@ -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);
Expand All @@ -38,29 +52,27 @@ 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();
});

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/);
Expand Down
Loading

0 comments on commit 09824b0

Please sign in to comment.