Skip to content

Commit

Permalink
Merge branch 'develop' into ray
Browse files Browse the repository at this point in the history
  • Loading branch information
UO290054 committed Apr 27, 2024
2 parents 2245b20 + d9493fd commit 74bc474
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
5 changes: 4 additions & 1 deletion questions/creationservice/creation-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ afterAll(async () => {
await mongoServer.stop();
});

describe('Retrieve Service', () => {
describe('Creation Service', () => {
it('should add a new user on GET /createquestion', async () => {

const response = await request(app).get('/createquestion');
expect(response.status).toBe(200);
expect(response.body).toHaveProperty("responseQuestionObject");
expect(response.body).toHaveProperty("responseCorrectOption");
expect(response.body).toHaveProperty("responseAnswerOptions");
});
});
83 changes: 82 additions & 1 deletion questions/retrieveservice/retrieve-service.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const Question = require('./questionshistory-model');

let mongoServer;
let app;

async function addQuestion(question){
const newQuestion = new Question({
question: question.question,
correctAnswer: question.correctAnswer,
incorrectAnswer1: question.incorrectAnswer1,
incorrectAnswer2: question.incorrectAnswer2,
incorrectAnswer3: question.incorrectAnswer3,
});

await newQuestion.save();
}

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
Expand All @@ -17,9 +30,77 @@ afterAll(async () => {
});

describe('Retrieve Service', () => {
it('should add a new user on GET /getquestionshistory', async () => {

it('should add a new game on POST /addgame', async () => {
const newGame = ({
username: "testUsername",
duration: "23",
questions: [{question: "¿Cual es la capital de España?", answers: "Madrid", useranswer: "Madrid"}],
percentage: 38,
totalQuestions: 1,
correctAnswers: 1,
incorrectAnswers: 0
})
const response = await request(app).post('/addgame').send(newGame);
expect(response.status).toBe(200);
expect(response.body.message).toBe( "Partida guardada exitosamente");
});

it('error on savind a new game /addgame', async () => {
const newGame = ({
username: "testUsername2",
duration: "23",
questions: 8,
percentage: 38,
totalQuestions: 1,
correctAnswers: 1,
incorrectAnswers: 0
})
const response = await request(app).post('/addgame').send(newGame);
expect(response.status).toBe(400);
expect(response.body).toHaveProperty("error");
});

it('should get the scoreBoard on GET /getquestionshistory', async () => {
const quest1 = {
question: "Cual es la capital de España",
correctAnswer: "Madrid",
incorrectAnswer1: "Londres",
incorrectAnswer2: "Berlin",
incorrectAnswer3: "Rabat",
};

const quest2 = {
question: "Cual es la capital de Alemania",
correctAnswer: "Berlin",
incorrectAnswer1: "Beijin",
incorrectAnswer2: "Angola",
incorrectAnswer3: "Mexico",
};
addQuestion(quest1);
addQuestion(quest2);
const response = await request(app).get('/getquestionshistory');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual([["Cual es la capital de España", "Madrid", "Londres", "Berlin", "Rabat"],
["Cual es la capital de Alemania", "Berlin", "Beijin", "Angola", "Mexico"]]);
});

it('should get the game history of a specific user on GET /getgamehistory/:username', async () => {
const username = "testUsername"
//const response = (await request(app).get('/getgamehistory/:username').query({username: "testUsername"}));
const response = await request(app).get(`/getgamehistory/${username}`);
expect(response.status).toBe(200);
expect(response.body[0].username).toBe("testUsername");
expect(response.body[0].duration).toBe(23);
expect(response.body[0].totalQuestions).toBe(1);
expect(response.body[0].correctAnswers).toBe(1);
});

it('should get the scoreBoard on GET /getScoreBoard', async () => {
const response = await request(app).get('/getScoreBoard');
expect(response.status).toBe(200);
expect(response.body).toStrictEqual([{"points":15,"totalCorrect":1,"totalIncorrect":0,"username":"testUsername"}]);
});


});

0 comments on commit 74bc474

Please sign in to comment.