Skip to content

Commit

Permalink
Merge branch 'webapp_interface' of https://github.com/Arquisoft/wiq_es6c
Browse files Browse the repository at this point in the history
 into webapp_interface
  • Loading branch information
uo288574 committed Apr 27, 2024
2 parents 48723ac + 17d1dfa commit 4ec1cd9
Show file tree
Hide file tree
Showing 4 changed files with 7,901 additions and 1,634 deletions.
4 changes: 0 additions & 4 deletions gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ app.get('/questions', async (req, res) => {
try {
// TODO: Implement logic to fetch questions from MongoDB and send response
// const questions = await Question.find()
console.log("Llegamos a pedir preguntas")
console.log(req.body.n_preg)
console.log(req.body.topics)
const questionGenerated = await axios.get(questionService + req.url);
console.log("Pedimos las preguntas")
res.json(questionGenerated.data);
} catch (error) {
// res.status(500).json({ message: error.message })
Expand Down
82 changes: 82 additions & 0 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const request = require('supertest');
const axios = require('axios');
const app = require('./game-service');

afterAll(async () => {
app.close();
});

jest.mock('axios');

describe('Game Service', () => {
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/storeGame')) {
return Promise.resolve("status ok")
}
})
axios.get.mockImplementation((url, data) => {
if (url.endsWith('/questions?n_preguntas=1&n_respuestas=4&tema=capital')) {
return Promise.resolve({data: {
pregunta: '¿Cuál es la capital Italia?',
respuesta_correcta: 'Roma',
respuestas_incorrectas: ['Nápoles', 'Florencia', 'Milán'],
}})
}
})


it('should return a game id', async () => {
const response = await request(app)
.get('/generateGame');
expect(response.statusCode).toBe(200);
expect(response.body).toHaveLength(24);
})

it('should fail', async () => {
const response = await request(app)
.get('/questions?n_preguntas=2&n_respuestas=5&tema=capital&tema=lenguaje')
expect(response.statusCode).toBe(500)
})

it('should return a number o questions of a diferent types of topics', async () => {
const response = await request(app)
.get('/questions?n_preguntas=1&n_respuestas=4&tema=capital')
expect(response.statusCode).toBe(200)
expect(response.body.pregunta).toBe('¿Cuál es la capital Italia?')
expect(response.body.respuesta_correcta).toBe('Roma')
expect(response.body.respuestas_incorrectas[0]).toBe("Nápoles")
expect(response.body.respuestas_incorrectas[1]).toBe("Florencia")
expect(response.body.respuestas_incorrectas[2]).toBe("Milán")

})
})

describe('Test the store game', () => {
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/storeGame')) {
return Promise.resolve(200)
}
})

it('should store the data of a game', async () => {
const newGame = {
id: "1",
username: 'testuser',
points: 100,
questions: [{
title: 'Question 1',
answers: ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4'],
ansIndex: [1, 2]
}, {
title: 'Question 2',
answers: ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4'],
ansIndex: [1, 1]
}]
};
const response = await request(app)
.post('/storeGame')
.send(newGame)
console.log(response)
expect(response.statusCode).toBe(200)
})
})
Loading

0 comments on commit 4ec1cd9

Please sign in to comment.