Skip to content

Commit db281be

Browse files
authored
Merge pull request #46 from feliciagan/qn_service_test
Add read questions and categories tests
2 parents 98ab0fa + 88508b5 commit db281be

File tree

4 files changed

+148
-22
lines changed

4 files changed

+148
-22
lines changed

backend/question-service/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Question Service
22

3+
> 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)
4+
35
## Setting-up MongoDB
46

57
> :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.

backend/question-service/tests/questionRoutes.spec.ts

Lines changed: 140 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import supertest from "supertest";
44
import app from "../app";
55
import Question from "../src/models/Question";
66
import {
7+
PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE,
8+
PAGE_LIMIT_REQUIRED_MESSAGE,
79
QN_NOT_FOUND_MESSAGE,
810
SERVER_ERROR_MESSAGE,
911
} from "../src/utils/constants";
@@ -21,31 +23,147 @@ jest.mock("../src/middlewares/basicAccessControl", () => ({
2123
}));
2224

2325
describe("Question routes", () => {
24-
it("Delete existing question", async () => {
25-
const title = faker.lorem.lines(1);
26-
const complexity = "Easy";
27-
const categories = ["Algorithms"];
28-
const description = faker.lorem.lines();
29-
const newQuestion = new Question({
30-
title,
31-
complexity,
32-
category: categories,
33-
description,
34-
});
35-
await newQuestion.save();
36-
const res = await request.delete(`${BASE_URL}/${newQuestion.id}`);
37-
expect(res.status).toBe(200);
26+
describe("GET /", () => {
27+
it("Reads existing questions", async () => {
28+
const qnLimit = 10;
29+
const res = await request.get(`${BASE_URL}?page=1&qnLimit=${qnLimit}`);
30+
expect(res.status).toBe(200);
31+
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
32+
});
33+
34+
it("Reads existing questions with title filter", async () => {
35+
const qnLimit = 10;
36+
const title = "tree";
37+
const res = await request.get(
38+
`${BASE_URL}?page=1&qnLimit=${qnLimit}&title=${title}`,
39+
);
40+
expect(res.status).toBe(200);
41+
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
42+
for (const qn of res.body.questions) {
43+
expect(qn.title.toLowerCase()).toContain(title);
44+
}
45+
});
46+
47+
it("Reads existing questions with complexity filter", async () => {
48+
const qnLimit = 10;
49+
const complexity = "Easy";
50+
const res = await request.get(
51+
`${BASE_URL}?page=1&qnLimit=${qnLimit}&complexities=${complexity}`,
52+
);
53+
expect(res.status).toBe(200);
54+
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
55+
for (const qn of res.body.questions) {
56+
expect(qn.complexity).toBe(complexity);
57+
}
58+
});
59+
60+
it("Reads existing questions with category filters", async () => {
61+
const qnLimit = 10;
62+
const category = "Algorithms";
63+
const res = await request.get(
64+
`${BASE_URL}?page=1&qnLimit=${qnLimit}&categories=${category}`,
65+
);
66+
expect(res.status).toBe(200);
67+
expect(res.body.questions.length).toBeLessThanOrEqual(qnLimit);
68+
for (const qn of res.body.questions) {
69+
expect(qn.categories).toContain(category);
70+
}
71+
});
72+
73+
it("Does not read without page", async () => {
74+
const res = await request.get(`${BASE_URL}?qnLimit=10`);
75+
expect(res.status).toBe(400);
76+
expect(res.body.message).toBe(PAGE_LIMIT_REQUIRED_MESSAGE);
77+
});
78+
79+
it("Does not read without qnLimit", async () => {
80+
const res = await request.get(`${BASE_URL}?page=1`);
81+
expect(res.status).toBe(400);
82+
expect(res.body.message).toBe(PAGE_LIMIT_REQUIRED_MESSAGE);
83+
});
84+
85+
it("Does not read with negative page", async () => {
86+
const res = await request.get(`${BASE_URL}?page=-1&qnLimit=10`);
87+
expect(res.status).toBe(400);
88+
expect(res.body.message).toBe(PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE);
89+
});
90+
91+
it("Does not read with negative qnLimit", async () => {
92+
const res = await request.get(`${BASE_URL}?page=1&qnLimit=-10`);
93+
expect(res.status).toBe(400);
94+
expect(res.body.message).toBe(PAGE_LIMIT_INCORRECT_FORMAT_MESSAGE);
95+
});
96+
});
97+
98+
describe("GET /:id", () => {
99+
it("Reads existing question", async () => {
100+
const title = faker.lorem.lines(1);
101+
const complexity = "Easy";
102+
const categories = ["Algorithms"];
103+
const description = faker.lorem.lines();
104+
const newQuestion = new Question({
105+
title,
106+
complexity,
107+
category: categories,
108+
description,
109+
});
110+
await newQuestion.save();
111+
const res = await request.get(`${BASE_URL}/${newQuestion.id}`);
112+
expect(res.status).toBe(200);
113+
expect(res.body.question.title).toBe(title);
114+
expect(res.body.question.complexity).toBe(complexity);
115+
expect(res.body.question.categories).toEqual(categories);
116+
expect(res.body.question.description).toBe(description);
117+
});
118+
119+
it("Does not read non-existing question with invalid object id", async () => {
120+
const res = await request.get(`${BASE_URL}/blah`);
121+
expect(res.status).toBe(500);
122+
expect(res.body.message).toBe(SERVER_ERROR_MESSAGE);
123+
});
124+
125+
it("Does not read non-existing question with valid object id", async () => {
126+
const res = await request.get(`${BASE_URL}/66f77e9f27ab3f794bdae664`);
127+
expect(res.status).toBe(404);
128+
expect(res.body.message).toBe(QN_NOT_FOUND_MESSAGE);
129+
});
38130
});
39131

40-
it("Delete non-existing question with invalid object id", async () => {
41-
const res = await request.delete(`${BASE_URL}/blah`);
42-
expect(res.status).toBe(500);
43-
expect(res.body.message).toBe(SERVER_ERROR_MESSAGE);
132+
describe("GET /categories", () => {
133+
it("Reads existing question categories", async () => {
134+
const res = await request.get(`${BASE_URL}/categories`);
135+
expect(res.status).toBe(200);
136+
expect(res.body).toHaveProperty("categories");
137+
});
44138
});
45139

46-
it("Delete non-existing question with valid object id", async () => {
47-
const res = await request.delete(`${BASE_URL}/66f77e9f27ab3f794bdae664`);
48-
expect(res.status).toBe(404);
49-
expect(res.body.message).toBe(QN_NOT_FOUND_MESSAGE);
140+
describe("DELETE /:id", () => {
141+
it("Deletes existing question", async () => {
142+
const title = faker.lorem.lines(1);
143+
const complexity = "Easy";
144+
const categories = ["Algorithms"];
145+
const description = faker.lorem.lines();
146+
const newQuestion = new Question({
147+
title,
148+
complexity,
149+
category: categories,
150+
description,
151+
});
152+
await newQuestion.save();
153+
const res = await request.delete(`${BASE_URL}/${newQuestion.id}`);
154+
expect(res.status).toBe(200);
155+
});
156+
157+
it("Does not delete non-existing question with invalid object id", async () => {
158+
const res = await request.delete(`${BASE_URL}/blah`);
159+
expect(res.status).toBe(500);
160+
expect(res.body.message).toBe(SERVER_ERROR_MESSAGE);
161+
});
162+
163+
it("Does not delete non-existing question with valid object id", async () => {
164+
const res = await request.delete(`${BASE_URL}/66f77e9f27ab3f794bdae664`);
165+
expect(res.status).toBe(404);
166+
expect(res.body.message).toBe(QN_NOT_FOUND_MESSAGE);
167+
});
50168
});
51169
});

backend/user-service/MongoDBSetup.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
> 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)
2+
13
# Setting up MongoDB Instance
24

35
1. Visit the MongoDB Atlas Site [https://www.mongodb.com/atlas](https://www.mongodb.com/atlas) and click on "Try Free"

backend/user-service/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# User Service Guide
22

3+
> User Service was adapted from [PeerPrep-UserService repository](https://github.com/CS3219-AY2425S1/PeerPrep-UserService)
4+
5+
> 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)
6+
37
## Setting-up MongoDB
48

59
> :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.

0 commit comments

Comments
 (0)