From 46de0aabb3feb68027686d372b0f147e28b6bffa Mon Sep 17 00:00:00 2001 From: uo283055 Date: Mon, 19 Feb 2024 23:24:44 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Test=20a=C3=B1adir=20respuestas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../answerservice/answer-service.test.js | 47 +++++++++++++++++++ questions/createservice/create-service.js | 19 -------- webapp/src/components/Game.js | 2 +- 3 files changed, 48 insertions(+), 20 deletions(-) create mode 100644 questions/answerservice/answer-service.test.js diff --git a/questions/answerservice/answer-service.test.js b/questions/answerservice/answer-service.test.js new file mode 100644 index 00000000..994763dc --- /dev/null +++ b/questions/answerservice/answer-service.test.js @@ -0,0 +1,47 @@ +const request = require('supertest'); +const { MongoMemoryServer } = require('mongodb-memory-server'); +const bcrypt = require('bcrypt'); +const Answer = require('./answer-model'); + +let mongoServer; +let app; + +//test answer +const answerTest = { + + answerBody: "Cervantes", + typeAnswer: "autor" +}; + +async function addanswer(answerTest){ + const newAnswer = new Answer({ + answerBody: answerTest.answerBody, + typeAnswer: answerTest.typeAnswer + }); + + await newAnswer.save(); +} + +beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + const mongoUri = mongoServer.getUri(); + process.env.MONGODB_URI = mongoUri; + app = require('./answer-service'); + //Load database with initial conditions + await addanswer(answerTest); +}); + +afterAll(async () => { + app.close(); + await mongoServer.stop(); +}); + +describe('Answer Service', () => { + it('Should perform an addRecord operation /addAnswer', async () => { + const response = await request(app).post('/addAnswer').send(answerTest); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('answerBody', 'Cervantes'); + expect(response.body).toHaveProperty('typeAnswer', 'autor'); + }); + +}); diff --git a/questions/createservice/create-service.js b/questions/createservice/create-service.js index b21b8905..df995757 100644 --- a/questions/createservice/create-service.js +++ b/questions/createservice/create-service.js @@ -39,25 +39,6 @@ app.post('/getQuestionBody', async (req, res) => { //saco una pregunta de forma aleatoria const rQuestion = await Question.aggregate([{ $sample: { size: 1 } }]); - /*const rQuestionResult=null; - if (rQuestion && rQuestion.length > 0) { - - //obtengo la pregunta - const rQuestionResult = rQuestion[0]; - - const newQuestion1 = new Question({ - questionBody: rQuestionResult.questionBody, - typeQuestion: rQuestionResult.typeQuestion, - typeAnswer: rQuestionResult.typeAnswer, - }); - - res.json(newQuestion1); - - - } else { - console.log("No hay preguntas"); - }*/ - res.json(rQuestion[0]); } catch (error) { res.status(500).json({ error: 'Internal Server Error' }); diff --git a/webapp/src/components/Game.js b/webapp/src/components/Game.js index 2d6b8ccc..f99aa2a6 100644 --- a/webapp/src/components/Game.js +++ b/webapp/src/components/Game.js @@ -25,7 +25,7 @@ const Game=() =>{ try { const response = await axios.post(`${apiEndpoint}/getQuestionBody`); - // const { questionBody: questionBodyData } = response.data; + setquestionBody(response.data.questionBody);//obtengo los datos del cuerpo de la pregunta //setQuestionType(response.data.typeQuestion); From b0b7ab90156e7df284ee40d0e89b71a5deef024e Mon Sep 17 00:00:00 2001 From: uo283055 Date: Tue, 20 Feb 2024 02:27:02 +0100 Subject: [PATCH 2/3] generacion de preguntas aleatorias --- questions/createservice/create-service.js | 5 ++- webapp/src/components/Game.js | 51 ++++++++++------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/questions/createservice/create-service.js b/questions/createservice/create-service.js index df995757..ec2814a8 100644 --- a/questions/createservice/create-service.js +++ b/questions/createservice/create-service.js @@ -1,7 +1,9 @@ const express = require('express'); const mongoose = require('mongoose'); const bodyParser = require('body-parser'); -const Question = require('./create-model') +const Question = require('./create-model'); + + const app = express(); const port = 8005; @@ -30,6 +32,7 @@ app.post('/addQuestion', async (req, res) => { } }); + //obtiene una pregunta de forma aleatoria app.post('/getQuestionBody', async (req, res) => { try { diff --git a/webapp/src/components/Game.js b/webapp/src/components/Game.js index f99aa2a6..ab548350 100644 --- a/webapp/src/components/Game.js +++ b/webapp/src/components/Game.js @@ -28,21 +28,18 @@ const Game=() =>{ setquestionBody(response.data.questionBody);//obtengo los datos del cuerpo de la pregunta - //setQuestionType(response.data.typeQuestion); - //setQuestionType(response.data.typeAnswer); + setQuestionType(response.data.typeQuestion); + setAnswerType(response.data.typeAnswer); } catch (error) { console.error("Error al obtener la pregunta aleatoria", error); } }; - - - - + // Obtener info de wikidata segun el tipo de la pregunta y la respuesta para esa pregunta //questionType, answerType - const obtenerInformacionWikidata = async () => { + const obtenerPais = async () => { try { // Consulta SPARQL//obtengo @@ -76,7 +73,7 @@ const Game=() =>{ const index = Math.floor(Math.random() * numEles);//index al azar const result = data.results.bindings[index]; - setInformacionWikidata(result.countryLabel.value); + setInformacionWikidata(result.countryLabel.value+`?`); } else { console.error("Error al realizar la consulta en Wikidata. Estado de respuesta:", respuestaWikidata.status); @@ -84,37 +81,35 @@ const Game=() =>{ } catch (error) { console.error("Error al realizar la consulta en Wikidata", error); } + }; + const handleButtonClick = () => { + obtenerPreguntaAleatoria(); + //pais, libro,actor, edificio + if (questionType=='pais'){ + obtenerPais();} + else if (questionType =='libro'){ + obtenerPais();} + else if (questionType ==='actor'){ + obtenerPais();} + else if (questionType=='edificio'){ + obtenerPais();} + //etc... - - //obtenerInformacionWikidata(); - /* useEffect(() => { - obtenerPreguntaAleatoria(); - }, []);*/ - - - //obtenerPreguntaAleatoria(); + }; return (
-

Pregunta

+

Pregunta:

- Base de pregunta al azar de mongo: {questionBody} - - - - - - Prueba de Wikidata, pais al azar:{informacionWikidata} + {questionBody} {informacionWikidata} -
From adeaf65f429b9244b8daca35efed8029bff4f540 Mon Sep 17 00:00:00 2001 From: uo283055 Date: Tue, 20 Feb 2024 02:34:15 +0100 Subject: [PATCH 3/3] ediciones menores de generacion preguntas aleatorias --- webapp/src/components/Game.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webapp/src/components/Game.js b/webapp/src/components/Game.js index ab548350..1106afe1 100644 --- a/webapp/src/components/Game.js +++ b/webapp/src/components/Game.js @@ -96,7 +96,7 @@ const Game=() =>{ else if (questionType=='edificio'){ obtenerPais();} //etc... - + obtenerPais();//para que de momento genere un pais hasta tener el resto de queries }; return (