Skip to content

Commit 2cae650

Browse files
committed
Update matching test case to mock post request to question service.
1 parent c24cdca commit 2cae650

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

MatchingService/config/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ module.exports = {
55
mongodbUri: "mongodb://matching-service-database:27017/peer-prep",
66
// mongodbUri: "mongodb://127.0.0.1:27020/peer-prep",
77
// mongodbUri: "mongodb://127.0.0.1:27017/peer-prep",
8-
rabbitmqUrl: "amqp://rabbitmq:5672",
9-
// rabbitmqUrl: "amqp://127.0.0.1:5672",
8+
// rabbitmqUrl: "amqp://rabbitmq:5672",
9+
rabbitmqUrl: "amqp://127.0.0.1:5672",
1010
refreshDuration: 3000, // 3 seconds
1111
waitingDuration: 3000,
1212
matchingDuration: 57000,

MatchingService/package-lock.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MatchingService/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"amqplib": "^0.10.3",
2121
"axios": "^1.6.0",
22+
"axios-mock-adapter": "^1.22.0",
2223
"choco": "^0.2.1",
2324
"cors": "^2.8.5",
2425
"express": "^4.18.2",

MatchingService/test/matchingService.test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ const { findMatch, cancelMatch } = require('../services/matchingService');
22
const { getCurrentMatchedPair, deleteAllMatchedPairs } = require('../database/matchedPairDb');
33
const mongoose = require('mongoose');
44
const { MongoMemoryServer } = require('mongodb-memory-server');
5+
const config = require('../config/config');
6+
const axios = require('axios');
7+
const MockAdapter = require('axios-mock-adapter');
58

69
jest.setTimeout(200000);
710

811
describe('Matching Service', () => {
912

1013
let mongod;
14+
15+
const testQuestion = {
16+
_id: new mongoose.Types.ObjectId("65378371752185e6e1b5b342"),
17+
title: "test1",
18+
description: "test1 desp",
19+
complexity: "Test",
20+
category: "Test",
21+
language:"Test",
22+
userTags: [],
23+
};
1124

1225
beforeAll(async () => {
1326
mongod = await MongoMemoryServer.create();
@@ -19,6 +32,14 @@ describe('Matching Service', () => {
1932
});
2033

2134
console.log("testDB connected");
35+
36+
const mock = new MockAdapter(axios);
37+
38+
mock.onPost(`${config.questionServiceUrl}/match`)
39+
.reply(200, {
40+
questionId: "65378371752185e6e1b5b342",
41+
question: testQuestion,
42+
});
2243
});
2344

2445
afterAll(async () => {
@@ -144,7 +165,7 @@ describe('Matching Service', () => {
144165
status: 'success',
145166
isMatched: true,
146167
sessionId: matchPair.sessionId,
147-
questionId: null,
168+
questionId: testQuestion._id,
148169
collaboratorId: javaRequest2.id,
149170
request: javaRequest1
150171
}
@@ -153,7 +174,7 @@ describe('Matching Service', () => {
153174
status: 'success',
154175
isMatched: true,
155176
sessionId: matchPair.sessionId,
156-
questionId: null,
177+
questionId: testQuestion._id,
157178
collaboratorId: javaRequest1.id,
158179
request: javaRequest2
159180
}
@@ -209,7 +230,7 @@ describe('Matching Service', () => {
209230
status: 'success',
210231
isMatched: true,
211232
sessionId: matchPair.sessionId,
212-
questionId: null,
233+
questionId: testQuestion._id,
213234
collaboratorId: cppFullRequest2.id,
214235
request: cppFullRequest1
215236
}
@@ -218,7 +239,7 @@ describe('Matching Service', () => {
218239
status: 'success',
219240
isMatched: true,
220241
sessionId: matchPair.sessionId,
221-
questionId: null,
242+
questionId: testQuestion._id,
222243
collaboratorId: cppFullRequest1.id,
223244
request: cppFullRequest2
224245
}
@@ -297,7 +318,7 @@ describe('Matching Service', () => {
297318
test('Cancel an existing match', async() => {
298319
const [matchResult, cancelResult] = await Promise.all([
299320
findMatch(javaRequest1),
300-
new Promise(resolve => setTimeout(resolve, 5000)).then(() => cancelMatch(javaRequest1.id))
321+
new Promise(resolve => setTimeout(resolve, 8000)).then(() => cancelMatch(javaRequest1.id))
301322
]);
302323

303324
const expectResult1 = {

0 commit comments

Comments
 (0)