Skip to content

Commit d8abd45

Browse files
Merge pull request #407 from tcet-opensource/254-topics.endpoints
added api-testcase
2 parents 32c4ac3 + 77369c8 commit d8abd45

File tree

5 files changed

+119
-4
lines changed

5 files changed

+119
-4
lines changed

_apidoc.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,3 +1750,59 @@
17501750
* @apiSuccess {String} faculty.natureOfAssociation Nature of association with the institution.
17511751
* @apiSuccess {String} faculty.additionalResponsibilities Additional responsibilities of the faculty.
17521752
*/
1753+
1754+
//------------------------------------------------------------------------------------------
1755+
// Topics.
1756+
// ------------------------------------------------------------------------------------------
1757+
1758+
/**
1759+
* @api {post} /topic/add Add Topic
1760+
* @apiName AddTopic
1761+
* @apiGroup Topic
1762+
*
1763+
* @apiBody {String} title The title of topic.
1764+
*
1765+
* @apiSuccess {String} res Success message with the ID of the added topic.
1766+
*
1767+
* @apiError (Error 500) DatabaseError Error while inserting in the database.
1768+
*
1769+
* @apiDescription Adds a new topic to the system.
1770+
*/
1771+
1772+
/**
1773+
* @api {get} topic/list Get Topic List
1774+
* @apiName GetTopic
1775+
* @apiGroup Topic
1776+
*
1777+
* @apiQuery {String} [title] Title of Topic.
1778+
*
1779+
* @apiSuccess {String} topic.title Title of Topic.
1780+
*/
1781+
1782+
/**
1783+
* @api {delete} /topic/delete/:topicId Delete Topic
1784+
* @apiName DeleteTopic,
1785+
* @apiGroup Topic
1786+
*
1787+
* @apiParam {String} topicId The ID of the topic document to delete.
1788+
*
1789+
* @apiSuccess {String} res Success message indicating the deletion.
1790+
*
1791+
* @apiError (Error 500) err Error message if there was an error during the deletion.
1792+
*
1793+
* */
1794+
/**
1795+
* @api {post} /topic/update/:id Update topic details
1796+
* @apiName UpdateTopic
1797+
* @apiGroup Topic
1798+
* @apiDescription update Existing Topic details
1799+
*
1800+
* @apiParam {String} id The topic document to update.
1801+
* @apiBody {String} id Id of the topic to be updated
1802+
* @apiBody {String} [title] The title of topic.
1803+
1804+
*
1805+
* @apiSuccess {String} res topic updated.
1806+
* @apiError (Error 500) err Error in updating database
1807+
*
1808+
*/

app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,6 @@ app.use("/group", groupRouter);
6969
app.use("/semester", semesterRouter);
7070
app.use("/faculty", facultyRouter);
7171
app.use("/performance", performarouter);
72-
app.use("topic",topicRouter);
72+
app.use("/topic",topicRouter);
7373

7474
export default app;

controller/topic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
try {
1111
// eslint-disable-next-line max-len
1212
const topic = await addNewTopic(title);
13-
res.json({ res: `added accreditation ${topic.name}`, id: topic.id });
13+
res.json({ res: `added topic ${topic.name}`, id: topic.id });
1414
} catch (error) {
1515
logger.error("Error while inserting", error);
1616
res.status(500);

services/topic.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export async function addNewTopic(title) {
55
const newTopic = await Topic.create({
66
title,
77
});
8-
if (newTopic.name === name) {
9-
return newAopic;
8+
if (newTopic.title === title) {
9+
return newTopic;
1010
}
1111
throw new databaseError.DataEntryError("Add Topic");
1212
}

test/routes/topic.test.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
2+
import topicModel from "#models/topic";
3+
import connector from "#models/databaseUtil";
4+
5+
jest.mock("#util");
6+
7+
const { agent } = global;
8+
9+
function cleanUp(callback) {
10+
topicModel.remove({ title: "xyz" }).then(() => {
11+
connector.disconnect((DBerr) => {
12+
if (DBerr) console.log("Database dissconnnect error: ", DBerr);
13+
});
14+
callback();
15+
});
16+
}
17+
18+
afterAll((done) => {
19+
cleanUp(done);
20+
});
21+
22+
describe("checking topic functions", () => {
23+
it("create topic", async () => {
24+
const response = await agent.post("/topic/add").send({
25+
title: "xyz",
26+
});
27+
expect(response.headers["content-type"]).toMatch(/json/);
28+
expect(response.status).toBe(200);
29+
expect(response.body.res).toMatch(/added topic/);
30+
});
31+
let id;
32+
beforeEach(async () => {
33+
id = await agent.post("/topic/add").send({
34+
title: "xyz",
35+
});
36+
id = JSON.parse(id.res.text).id;
37+
});
38+
39+
afterEach(async () => {
40+
await topicModel.remove({ title: "xyz" });
41+
});
42+
43+
it("read topic", async () => {
44+
const response = await agent
45+
.get("/topic/list")
46+
.send({ title: "xyz" });
47+
expect(response.status).toBe(200);
48+
expect(response.body.res).toBeDefined();
49+
});
50+
51+
it("update topic", async () => {
52+
const response = await agent
53+
.post(`/topic/update/${id}`)
54+
.send({ title: "xyz" }, { title: "123" });
55+
expect(response.headers["content-type"]).toMatch(/json/);
56+
expect(response.status).toBe(200);
57+
expect(response.body.res).toMatch(/topic updated/);
58+
});
59+
});

0 commit comments

Comments
 (0)