Skip to content

Commit

Permalink
Updated question model and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uo276976 committed Mar 4, 2024
1 parent 498e04e commit 178a608
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ app.post('/addgame', async (req, res) => {
// Guarda el nuevo juego en la base de datos
const savedGame = await newGame.save();

res.status(201).json(savedGame);
res.status(200).json(savedGame);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
Expand Down
17 changes: 14 additions & 3 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,19 @@ afterAll(async () => {
describe('Game Service', () => {
it('should add a new game on POST /addgame', async () => {
const newGame = {
user: mongoose.Types.ObjectId(), // ID de usuario simulado
questions: mongoose.Types.ObjectId(), // ID de pregunta simulado
user: 'testUser',
questions: [
{
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
},
{
question: 'Mocked Question2',
correct: 'Mocked Correct Answer2',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
}
],
answers: [
{
response: 'User response',
Expand All @@ -32,7 +43,7 @@ describe('Game Service', () => {
};

const response = await request(app).post('/addgame').send(newGame);
expect(response.status).toBe(201);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('user', newGame.user.toString());
});
});
12 changes: 6 additions & 6 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ describe('Gateway Service', () => {
}else if (url.endsWith('/addquestion')) {
return Promise.resolve({
data: {
question: 'What is the capital of France?',
options: ['Paris', 'Berlin', 'Madrid', 'Rome'],
correctOptionIndex: 0
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
}
});
}
Expand Down Expand Up @@ -100,9 +100,9 @@ describe('Gateway Service', () => {
const response = await request(app)
.post('/addquestion')
.send({
question: 'What is the capital of France?',
options: ['Paris', 'Berlin', 'Madrid', 'Rome'],
correctOptionIndex: 0,
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
});

expect(response.statusCode).toBe(200);
Expand Down
22 changes: 8 additions & 14 deletions questionservice/question-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,22 @@ const questionSchema = new mongoose.Schema({
type: String,
required: true,
},
options: {
correct: {
type: String,
required: true,
},
incorrects: {
type: [String],
required: true,
validate: {
validator: function (options) {
return options.length >= 2 && options.length <= 4;
return options.length >= 1 && options.length <= 3;
},
message: 'Options must have between 2 and 4 elements.',
},
},
correctOptionIndex: {
type: Number,
required: true,
validate: {
validator: function (index) {
return index >= 0 && index < 4; // Max of 4 options
},
message: 'Correct option index must be between 0 and 3.',
message: 'Options must be between 2 and 4 elements.',
},
},
});

const Question = mongoose.model('Question', questionSchema);

module.exports = Question;
module.exports = Question;
10 changes: 5 additions & 5 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ const validateRequiredFields = (req, fields) => {
// Ruta para agregar una nueva pregunta
app.post('/addquestion', async (req, res) => {
try {
validateRequiredFields(req, ['question', 'options', 'correctOptionIndex']);
validateRequiredFields(req, ['question', 'correct', 'incorrects']);

const { question, options, correctOptionIndex } = req.body;
const { question, correct, incorrects } = req.body;

// Crea una nueva instancia del modelo de preguntas
const newQuestion = new Question({
question,
options,
correctOptionIndex,
correct,
incorrects,
});

// Guarda la nueva pregunta en la base de datos
const savedQuestion = await newQuestion.save();

res.status(201).json(savedQuestion);
res.status(200).json(savedQuestion);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
Expand Down
8 changes: 4 additions & 4 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ afterAll(async () => {
describe('Question Service', () => {
it('should add a new question on POST /addquestion', async () => {
const newQuestion = {
question: 'What is the capital of France?',
options: ['Paris', 'Berlin', 'Madrid', 'Rome'],
correctOptionIndex: 0,
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
};

const response = await request(app).post('/addquestion').send(newQuestion);
expect(response.status).toBe(201);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('question', 'What is the capital of France?');
});

Expand Down

0 comments on commit 178a608

Please sign in to comment.