Skip to content

Commit 10327a3

Browse files
committed
fixed testcases for department
1 parent 4e21175 commit 10327a3

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

controller/department.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Infrastructure from "#models/infrastructure";
1010
import Organization from "#models/organization";
1111
import { logger } from "#util";
1212

13-
1413
async function addDepartment(req, res) {
1514
const {
1615
name,
@@ -20,21 +19,24 @@ async function addDepartment(req, res) {
2019
infrastructures,
2120
organization,
2221
} = req.body;
23-
const isAccredationValid = await isEntityIdValid(accreditations, Accreditation);
24-
const isInfrastructureValid = await isEntityIdValid(infrastructures, Infrastructure);
22+
const isAccredationValid = await isEntityIdValid(
23+
accreditations,
24+
Accreditation,
25+
);
26+
const isInfrastructureValid = await isEntityIdValid(
27+
infrastructures,
28+
Infrastructure,
29+
);
2530
const isOrganizationValid = await isEntityIdValid(organization, Organization);
2631

27-
2832
try {
29-
3033
if (!isAccredationValid && !isInfrastructureValid && !isOrganizationValid) {
31-
3234
const error = "";
33-
if (!isBranchValid) error.concat("Invalid branch");
34-
if (!isCourseValid) error.concat(" Invalid course opted");
35+
if (!isAccredationValid) error.concat("Invalid Accreditation");
36+
if (!isInfrastructureValid) error.concat(" Invalid Infrastruction");
37+
if (!isOrganizationValid) error.concat(" Invalid Organization");
3538
res.status(400).json({ err: error });
36-
}
37-
else {
39+
} else {
3840
const department = await createnewdepartment(
3941
name,
4042
acronym,

test/routes/department.test.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2-
import mongoose from "mongoose";
32
import departmentmodel from "#models/department";
43
import connector from "#models/databaseUtil";
4+
import accreditationModel from "#models/accreditation";
5+
import infrastructureModel from "#models/infrastructure";
6+
import organizationModel from "#models/organization";
57

68
jest.mock("#util");
79
const { agent } = global;
810

11+
let accreditationIds;
12+
let infrastructureIds;
13+
let organizationIds;
14+
915
// test case for deletion
1016

1117
function cleanUp(callback) {
@@ -14,9 +20,9 @@ function cleanUp(callback) {
1420
name: "Electronics",
1521
acronym: "COMPS",
1622
yearOfStarting: "2020-09-01T00:00:00.000Z",
17-
accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")],
18-
infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
19-
organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
23+
accreditations: [accreditationIds],
24+
infrastructures: [infrastructureIds],
25+
organization: [organizationIds],
2026
})
2127
.then(() => {
2228
connector.disconnect((DBerr) => {
@@ -26,6 +32,21 @@ function cleanUp(callback) {
2632
});
2733
}
2834

35+
/* eslint-disable no-underscore-dangle */
36+
async function getIds(callback) {
37+
accreditationIds = await accreditationModel.read({}, 1);
38+
accreditationIds = accreditationIds.data[0]._id;
39+
infrastructureIds = await infrastructureModel.read({}, 1);
40+
infrastructureIds = infrastructureIds.data[0]._id;
41+
organizationIds = await organizationModel.read({}, 1);
42+
organizationIds = organizationIds.data[0]._id;
43+
callback();
44+
}
45+
46+
beforeAll((done) => {
47+
getIds(done);
48+
});
49+
2950
afterAll((done) => {
3051
cleanUp(done);
3152
});
@@ -36,9 +57,9 @@ describe("Department CRUD", () => {
3657
name: "Computer",
3758
acronym: "COMPS",
3859
yearOfStarting: "2020-09-01T00:00:00.000Z",
39-
accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")],
40-
infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
41-
organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
60+
accreditations: [accreditationIds],
61+
infrastructures: [infrastructureIds],
62+
organization: [organizationIds],
4263
});
4364

4465
expect(response.status).toBe(200);
@@ -52,9 +73,9 @@ describe("Department CRUD", () => {
5273
name: "Computer",
5374
acronym: "COMPS",
5475
yearOfStarting: "2020-09-01T00:00:00.000Z",
55-
accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")],
56-
infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
57-
organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
76+
accreditations: [accreditationIds],
77+
infrastructures: [infrastructureIds],
78+
organization: [organizationIds],
5879
});
5980
id = JSON.parse(id.res.text).id;
6081
});
@@ -64,9 +85,9 @@ describe("Department CRUD", () => {
6485
name: "Computer",
6586
acronym: "COMPS",
6687
yearOfStarting: "2020-09-01T00:00:00.000Z",
67-
accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")],
68-
infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
69-
organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
88+
accreditations: [accreditationIds],
89+
infrastructures: [infrastructureIds],
90+
organization: [organizationIds],
7091
});
7192
});
7293

@@ -75,9 +96,9 @@ describe("Department CRUD", () => {
7596
name: "Computer",
7697
acronym: "COMPS",
7798
yearOfStarting: "2020-09-01T00:00:00.000Z",
78-
accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")],
79-
infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
80-
organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
99+
accreditations: [accreditationIds],
100+
infrastructures: [infrastructureIds],
101+
organization: [organizationIds],
81102
});
82103
expect(response.body.res).not.toBeNull();
83104
});
@@ -87,9 +108,9 @@ describe("Department CRUD", () => {
87108
name: "Electronics",
88109
acronym: "COMPS",
89110
yearOfStarting: "2020-09-01T00:00:00.000Z",
90-
accreditations: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03a")],
91-
infrastructures: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
92-
organization: [mongoose.Types.ObjectId("5f8778b54b553439ac49a03b")],
111+
accreditations: [accreditationIds],
112+
infrastructures: [infrastructureIds],
113+
organization: [organizationIds],
93114
});
94115
expect(response.status).toBe(200);
95116
expect(response.body.res).toMatch(/department updated/);

0 commit comments

Comments
 (0)