Skip to content

Commit

Permalink
Merge pull request #46 from feliciagan/qn_service_test
Browse files Browse the repository at this point in the history
Add read questions and categories tests
  • Loading branch information
nicolelim02 authored Oct 3, 2024
2 parents 98ab0fa + 88508b5 commit db281be
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 22 deletions.
2 changes: 2 additions & 0 deletions backend/question-service/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Question Service

> This guide references the [user-service README in the PeerPrep-UserService repository](https://github.com/CS3219-AY2425S1/PeerPrep-UserService/blob/main/user-service/README.md)
## Setting-up MongoDB

> :notebook: If you are familiar to MongoDB and wish to use a local instance, please feel free to do so. This guide utilizes MongoDB Cloud Services.
Expand Down
162 changes: 140 additions & 22 deletions backend/question-service/tests/questionRoutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import supertest from "supertest";
import app from "../app";
import Question from "../src/models/Question";
import {
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
PAGE_LIMIT_REQUIRED_MESSAGE,
QN_NOT_FOUND_MESSAGE,
SERVER_ERROR_MESSAGE,
} from "../src/utils/constants";
Expand All @@ -21,31 +23,147 @@ jest.mock("../src/middlewares/basicAccessControl", () => ({
}));

describe("Question routes", () => {
it("Delete existing question", async () => {
const title = faker.lorem.lines(1);
const complexity = "Easy";
const categories = ["Algorithms"];
const description = faker.lorem.lines();
const newQuestion = new Question({
title,
complexity,
category: categories,
description,
});
await newQuestion.save();
const res = await request.delete(`${BASE_URL}/${newQuestion.id}`);
expect(res.status).toBe(200);
describe("GET /", () => {
it("Reads existing questions", async () => {
const qnLimit = 10;
const res = await request.get(`${BASE_URL}?page=1&qnLimit=${qnLimit}`);
expect(res.status).toBe(200);
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
});

it("Reads existing questions with title filter", async () => {
const qnLimit = 10;
const title = "tree";
const res = await request.get(
`${BASE_URL}?page=1&qnLimit=${qnLimit}&title=${title}`,
);
expect(res.status).toBe(200);
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
for (const qn of res.body.questions) {
expect(qn.title.toLowerCase()).toContain(title);
}
});

it("Reads existing questions with complexity filter", async () => {
const qnLimit = 10;
const complexity = "Easy";
const res = await request.get(
`${BASE_URL}?page=1&qnLimit=${qnLimit}&complexities=${complexity}`,
);
expect(res.status).toBe(200);
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
for (const qn of res.body.questions) {
expect(qn.complexity).toBe(complexity);
}
});

it("Reads existing questions with category filters", async () => {
const qnLimit = 10;
const category = "Algorithms";
const res = await request.get(
`${BASE_URL}?page=1&qnLimit=${qnLimit}&categories=${category}`,
);
expect(res.status).toBe(200);
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
for (const qn of res.body.questions) {
expect(qn.categories).toContain(category);
}
});

it("Does not read without page", async () => {
const res = await request.get(`${BASE_URL}?qnLimit=10`);
expect(res.status).toBe(400);
expect(res.body.message).toBe(PAGE_LIMIT_REQUIRED_MESSAGE);
});

it("Does not read without qnLimit", async () => {
const res = await request.get(`${BASE_URL}?page=1`);
expect(res.status).toBe(400);
expect(res.body.message).toBe(PAGE_LIMIT_REQUIRED_MESSAGE);
});

it("Does not read with negative page", async () => {
const res = await request.get(`${BASE_URL}?page=-1&qnLimit=10`);
expect(res.status).toBe(400);
expect(res.body.message).toBe(PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE);
});

it("Does not read with negative qnLimit", async () => {
const res = await request.get(`${BASE_URL}?page=1&qnLimit=-10`);
expect(res.status).toBe(400);
expect(res.body.message).toBe(PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE);
});
});

describe("GET /:id", () => {
it("Reads existing question", async () => {
const title = faker.lorem.lines(1);
const complexity = "Easy";
const categories = ["Algorithms"];
const description = faker.lorem.lines();
const newQuestion = new Question({
title,
complexity,
category: categories,
description,
});
await newQuestion.save();
const res = await request.get(`${BASE_URL}/${newQuestion.id}`);
expect(res.status).toBe(200);
expect(res.body.question.title).toBe(title);
expect(res.body.question.complexity).toBe(complexity);
expect(res.body.question.categories).toEqual(categories);
expect(res.body.question.description).toBe(description);
});

it("Does not read non-existing question with invalid object id", async () => {
const res = await request.get(`${BASE_URL}/blah`);
expect(res.status).toBe(500);
expect(res.body.message).toBe(SERVER_ERROR_MESSAGE);
});

it("Does not read non-existing question with valid object id", async () => {
const res = await request.get(`${BASE_URL}/66f77e9f27ab3f794bdae664`);
expect(res.status).toBe(404);
expect(res.body.message).toBe(QN_NOT_FOUND_MESSAGE);
});
});

it("Delete non-existing question with invalid object id", async () => {
const res = await request.delete(`${BASE_URL}/blah`);
expect(res.status).toBe(500);
expect(res.body.message).toBe(SERVER_ERROR_MESSAGE);
describe("GET /categories", () => {
it("Reads existing question categories", async () => {
const res = await request.get(`${BASE_URL}/categories`);
expect(res.status).toBe(200);
expect(res.body).toHaveProperty("categories");
});
});

it("Delete non-existing question with valid object id", async () => {
const res = await request.delete(`${BASE_URL}/66f77e9f27ab3f794bdae664`);
expect(res.status).toBe(404);
expect(res.body.message).toBe(QN_NOT_FOUND_MESSAGE);
describe("DELETE /:id", () => {
it("Deletes existing question", async () => {
const title = faker.lorem.lines(1);
const complexity = "Easy";
const categories = ["Algorithms"];
const description = faker.lorem.lines();
const newQuestion = new Question({
title,
complexity,
category: categories,
description,
});
await newQuestion.save();
const res = await request.delete(`${BASE_URL}/${newQuestion.id}`);
expect(res.status).toBe(200);
});

it("Does not delete non-existing question with invalid object id", async () => {
const res = await request.delete(`${BASE_URL}/blah`);
expect(res.status).toBe(500);
expect(res.body.message).toBe(SERVER_ERROR_MESSAGE);
});

it("Does not delete non-existing question with valid object id", async () => {
const res = await request.delete(`${BASE_URL}/66f77e9f27ab3f794bdae664`);
expect(res.status).toBe(404);
expect(res.body.message).toBe(QN_NOT_FOUND_MESSAGE);
});
});
});
2 changes: 2 additions & 0 deletions backend/user-service/MongoDBSetup.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
> This guide is taken from the [user-service MongoDBSetup.md in the PeerPrep-UserService repository](https://github.com/CS3219-AY2425S1/cs3219-ay2425s1-project-g28/blob/main/backend/user-service/MongoDBSetup.md)
# Setting up MongoDB Instance

1. Visit the MongoDB Atlas Site [https://www.mongodb.com/atlas](https://www.mongodb.com/atlas) and click on "Try Free"
Expand Down
4 changes: 4 additions & 0 deletions backend/user-service/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# User Service Guide

> User Service was adapted from [PeerPrep-UserService repository](https://github.com/CS3219-AY2425S1/PeerPrep-UserService)
> This guide references the [user-service README in the PeerPrep-UserService repository](https://github.com/CS3219-AY2425S1/PeerPrep-UserService/blob/main/user-service/README.md)
## Setting-up MongoDB

> :notebook: If you are familiar to MongoDB and wish to use a local instance, please feel free to do so. This guide utilizes MongoDB Cloud Services.
Expand Down

0 comments on commit db281be

Please sign in to comment.