Skip to content

Commit

Permalink
Merge pull request #407 from tcet-opensource/254-topics.endpoints
Browse files Browse the repository at this point in the history
added api-testcase
  • Loading branch information
TejasNair9977 authored Oct 15, 2023
2 parents 32c4ac3 + 77369c8 commit d8abd45
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 4 deletions.
56 changes: 56 additions & 0 deletions _apidoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,3 +1750,59 @@
* @apiSuccess {String} faculty.natureOfAssociation Nature of association with the institution.
* @apiSuccess {String} faculty.additionalResponsibilities Additional responsibilities of the faculty.
*/

//------------------------------------------------------------------------------------------
// Topics.
// ------------------------------------------------------------------------------------------

/**
* @api {post} /topic/add Add Topic
* @apiName AddTopic
* @apiGroup Topic
*
* @apiBody {String} title The title of topic.
*
* @apiSuccess {String} res Success message with the ID of the added topic.
*
* @apiError (Error 500) DatabaseError Error while inserting in the database.
*
* @apiDescription Adds a new topic to the system.
*/

/**
* @api {get} topic/list Get Topic List
* @apiName GetTopic
* @apiGroup Topic
*
* @apiQuery {String} [title] Title of Topic.
*
* @apiSuccess {String} topic.title Title of Topic.
*/

/**
* @api {delete} /topic/delete/:topicId Delete Topic
* @apiName DeleteTopic,
* @apiGroup Topic
*
* @apiParam {String} topicId The ID of the topic document to delete.
*
* @apiSuccess {String} res Success message indicating the deletion.
*
* @apiError (Error 500) err Error message if there was an error during the deletion.
*
* */
/**
* @api {post} /topic/update/:id Update topic details
* @apiName UpdateTopic
* @apiGroup Topic
* @apiDescription update Existing Topic details
*
* @apiParam {String} id The topic document to update.
* @apiBody {String} id Id of the topic to be updated
* @apiBody {String} [title] The title of topic.
*
* @apiSuccess {String} res topic updated.
* @apiError (Error 500) err Error in updating database
*
*/
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,6 @@ app.use("/group", groupRouter);
app.use("/semester", semesterRouter);
app.use("/faculty", facultyRouter);
app.use("/performance", performarouter);
app.use("topic",topicRouter);
app.use("/topic",topicRouter);

export default app;
2 changes: 1 addition & 1 deletion controller/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
try {
// eslint-disable-next-line max-len
const topic = await addNewTopic(title);
res.json({ res: `added accreditation ${topic.name}`, id: topic.id });
res.json({ res: `added topic ${topic.name}`, id: topic.id });
} catch (error) {
logger.error("Error while inserting", error);
res.status(500);
Expand Down
4 changes: 2 additions & 2 deletions services/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export async function addNewTopic(title) {
const newTopic = await Topic.create({
title,
});
if (newTopic.name === name) {
return newAopic;
if (newTopic.title === title) {
return newTopic;
}
throw new databaseError.DataEntryError("Add Topic");
}
Expand Down
59 changes: 59 additions & 0 deletions test/routes/topic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { jest } from "@jest/globals"; // eslint-disable-line import/no-extraneous-dependencies
import topicModel from "#models/topic";
import connector from "#models/databaseUtil";

jest.mock("#util");

const { agent } = global;

function cleanUp(callback) {
topicModel.remove({ title: "xyz" }).then(() => {
connector.disconnect((DBerr) => {
if (DBerr) console.log("Database dissconnnect error: ", DBerr);
});
callback();
});
}

afterAll((done) => {
cleanUp(done);
});

describe("checking topic functions", () => {
it("create topic", async () => {
const response = await agent.post("/topic/add").send({
title: "xyz",
});
expect(response.headers["content-type"]).toMatch(/json/);
expect(response.status).toBe(200);
expect(response.body.res).toMatch(/added topic/);
});
let id;
beforeEach(async () => {
id = await agent.post("/topic/add").send({
title: "xyz",
});
id = JSON.parse(id.res.text).id;
});

afterEach(async () => {
await topicModel.remove({ title: "xyz" });
});

it("read topic", async () => {
const response = await agent
.get("/topic/list")
.send({ title: "xyz" });
expect(response.status).toBe(200);
expect(response.body.res).toBeDefined();
});

it("update topic", async () => {
const response = await agent
.post(`/topic/update/${id}`)
.send({ title: "xyz" }, { title: "123" });
expect(response.headers["content-type"]).toMatch(/json/);
expect(response.status).toBe(200);
expect(response.body.res).toMatch(/topic updated/);
});
});

0 comments on commit d8abd45

Please sign in to comment.