Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added api-testcase #407

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/);
});
});