Skip to content

Commit

Permalink
Question service tests fixed to include new answer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFN2 committed Apr 7, 2024
1 parent 7d672e2 commit 190269f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
28 changes: 1 addition & 27 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ const port = 8010;
const axios = require('axios');
const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';

/*
const User = require('../users/userservice/user-model');
const mongoose = require('mongoose');
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/wiq-en1a-users';
mongoose.connect(mongoUri);
const mongo = mongoose.connection;
// Check connection done correctly
mongo.on('error', console.error.bind(console, 'MongoDB connection error:'));
mongo.once('open', function () {
console.log('Connected to MongoDB successfully');
});
*/


app.use(express.static('public'));
app.use(express.json());
Expand Down Expand Up @@ -256,15 +241,4 @@ const server = app.listen(port, () => {
console.log(`Questions service listening on http://localhost:${port}`);
});

module.exports = server


/**
*
* const { answer, username, category } =JSON.parse(req.body);
if(correctImg==answer){
await axios.post(userServiceUrl+'/addpoints',
{username: username, category: category, correct: "true" } );
*
*/
module.exports = server
18 changes: 16 additions & 2 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const request = require('supertest');
const axios = require('axios');
const app = require('./question-service');

afterAll(async () => {
Expand All @@ -7,6 +8,8 @@ afterAll(async () => {

// Mock the fetch function
global.fetch = jest.fn();
// Mock axios
jest.mock('axios');

var imgsToAssociatesMap = new Map()

Expand All @@ -32,6 +35,17 @@ jest.spyOn(global, 'fetch').mockImplementation(() => {
});

describe('Question Service', () => {
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/addpoints')) {
if(data.correct=="true"){
return Promise.resolve({ response: "Correct answer handled" });
} else if (data.correct=="false"){
return Promise.resolve({ response: "Incorrect answer handled" });
}
return Promise.resolve({ response: "Something went wrong" });
}
});

// Test /imgs/flags/question endpoint
it('should return a flags question with 4 images as options', async () => {
const response = await request(app).get('/imgs/flags/question');
Expand Down Expand Up @@ -94,7 +108,7 @@ describe('Question Service', () => {
const responseAnswer = await request(app)
.post("/imgs/answer")
.set('Content-Type', 'application/json')
.send({answer:correctImage, question:question})
.send({answer:correctImage, question:question, username:"username", category:"foods"})
expect(responseAnswer.body.correct).toBe("true")
});

Expand All @@ -119,7 +133,7 @@ describe('Question Service', () => {
const responseAnswer = await request(app)
.post("/imgs/answer")
.set('Content-Type', 'application/json')
.send({answer:incorrectImageAnswer, question:question})
.send({answer:incorrectImageAnswer, question:question, username:"username", category:"foods"})
expect(responseAnswer.body.correct).toBe("false")
expect(responseAnswer.body.associate).toBe(imgsToAssociatesMap.get(incorrectImageAnswer))
});
Expand Down

0 comments on commit 190269f

Please sign in to comment.