From a15c9cb317dc3e9f0de10a267efe8a56b0469977 Mon Sep 17 00:00:00 2001 From: "Xenos F." Date: Fri, 8 Nov 2024 13:23:16 +0800 Subject: [PATCH] Add tests for demo --- .../routes/questionRoute.test.js | 208 +++++++++++++++--- 1 file changed, 175 insertions(+), 33 deletions(-) diff --git a/server/question-service/routes/questionRoute.test.js b/server/question-service/routes/questionRoute.test.js index 9022d9290f..2347791b34 100644 --- a/server/question-service/routes/questionRoute.test.js +++ b/server/question-service/routes/questionRoute.test.js @@ -1,39 +1,181 @@ - -test('placeholder test', () => { - expect(true).toBe(true) -}) +const request = require("supertest"); +const express = require("express"); +const questionRoute = require("./questionRoute"); +const Question = require("../model/questionModel"); +jest.mock("../model/questionModel"); + +const app = express(); +app.use(express.json()); +app.use(express.urlencoded({ extended: false })); +app.use("/", questionRoute); + +afterEach(() => { + jest.clearAllMocks(); +}); + +const sampleQuestion1 = { + title: "Sample Question 1", + description: "Sample description", + categories: "Strings, Data Structures", + complexity: "Medium", + link: "localhost", +}; +const sampleQuestion2 = { + title: "Sample Question 2", + description: "Lorem Ipsum", + categories: "Algorithms", + complexity: "Hard", + link: "localhost", +}; describe("GET /", () => { - it('should return all questions', () => { - - }); -}) - + it("should return all questions with status 200", async () => { + Question.find.mockResolvedValue([sampleQuestion1]); + const res = await request(app).get("/"); + expect(res.status).toBe(200); + expect(res.body).toEqual([sampleQuestion1]); + }); + + it("should handle errors with status 500", async () => { + Question.find.mockRejectedValue(new Error("Error")); + const res = await request(app).get("/"); + expect(res.status).toBe(500); + expect(res.body.message).toBe("Error"); + }); +}); describe("GET /:id", () => { - -}) - + it("should return question with the right ID with status 200", async () => { + Question.findById.mockImplementation(async (id) => + id == 123 ? sampleQuestion1 : sampleQuestion2 + ); + const res = await request(app).get("/123"); + expect(res.status).toEqual(200); + expect(res.body).toEqual(sampleQuestion1); + }); + + it("should handle errors with status 500", async () => { + Question.findById.mockRejectedValue(new Error("Error")); + const res = await request(app).get("/123"); + expect(res.status).toBe(500); + expect(res.body.message).toBe("Error"); + }); +}); describe("GET /categories/unique", () => { - -}) - -describe("GET /complexity/___", () => { - -// get easy - - -// get medium - - -// get hard -}) - - - -// create question -// post add -// put id - -// delete question + it("should return unique categories with status 200", async () => { + const uniqueCategories = ["A", "B"]; + Question.aggregate.mockResolvedValue(uniqueCategories); + const res = await request(app).get("/categories/unique"); + expect(res.status).toBe(200); + expect(res.body).toEqual(uniqueCategories); + }); + + it("should handle errors with status 500", async () => { + Question.aggregate.mockRejectedValue(new Error("Error")); + const res = await request(app).get("/categories/unique"); + expect(res.status).toBe(500); + expect(res.body.message).toBe("Error"); + }); +}); + + +describe("POST /add", () => { + it("should create a new question with status 200", async () => { + Question.findOne.mockReturnValue({ exec: jest.fn().mockResolvedValue(null) }); + Question.mockReturnValue({ + ...sampleQuestion1, + save: jest.fn().mockResolvedValue(sampleQuestion1), + }); + const res = await request(app) + .post("/add") + .send(sampleQuestion1) + .set("Content-Type", "application/json") + .set("Accept", "application/json"); + expect(res.status).toBe(200); + expect(res.body).toEqual(sampleQuestion1); + }); + + it("should return 400 if question already exists", async () => { + Question.findOne.mockReturnValue({ + exec: jest.fn().mockResolvedValue({ + ...sampleQuestion1, + }), + }); + const res = await request(app) + .post("/add") + .send(sampleQuestion1) + .set("Content-Type", "application/json") + .set("Accept", "application/json"); + expect(res.status).toBe(400); + expect(res.text).toBe(`Question with the title "${sampleQuestion1.title}" already exists.`); + }); + + it("should handle errors with status 500", async () => { + Question.findOne.mockReturnValue({ exec: jest.fn().mockResolvedValue(null) }); + Question.mockReturnValue({ + ...sampleQuestion1, + save: jest.fn().mockRejectedValue(new Error("Error")), + }); + const res = await request(app) + .post("/add") + .send(sampleQuestion1) + .set("Content-Type", "application/json") + .set("Accept", "application/json, text/html"); + expect(res.status).toBe(500); + }); +}); + +describe("PUT /:id", () => { + const updatedQuestion = { + title: "Updated Question", + description: "", + categories: "", + complexity: "", + link: "", + }; + + it("should update a question by ID with status 200", async () => { + Question.findById.mockResolvedValue({ save: jest.fn().mockResolvedValue(updatedQuestion) }); + const res = await request(app).put("/123").send(updatedQuestion); + expect(res.status).toBe(200); + expect(res.body).toEqual(updatedQuestion); + }); + + it("should return 404 if question not found", async () => { + Question.findById.mockResolvedValue(null); + const res = await request(app).put("/123").send(updatedQuestion); + expect(res.status).toBe(404); + expect(res.body.message).toBe("Question not found"); + }); + + it("should handle errors with status 500", async () => { + Question.findById.mockRejectedValue(new Error("Update error")); + const res = await request(app).put("/123").send(updatedQuestion); + expect(res.status).toBe(500); + expect(res.body.message).toBe("Update error"); + }); +}); + +describe("DELETE /:id", () => { + it("should delete a question by ID with status 200", async () => { + Question.findByIdAndDelete.mockResolvedValue({ _id: "123" }); + const res = await request(app).delete("/123"); + expect(res.status).toBe(200); + expect(res.body.message).toBe("Question deleted successfully"); + }); + + it("should return 404 if question not found", async () => { + Question.findByIdAndDelete.mockResolvedValue(null); + const res = await request(app).delete("/123"); + expect(res.status).toBe(404); + expect(res.body.message).toBe("Question not found"); + }); + + it("should handle errors with status 500", async () => { + Question.findByIdAndDelete.mockRejectedValue(new Error("Delete error")); + const res = await request(app).delete("/123"); + expect(res.status).toBe(500); + expect(res.body.message).toBe("Delete error"); + }); +});