From fc5cbe400992ed260fbb330812eaefd7b42dbea7 Mon Sep 17 00:00:00 2001 From: Zohaib Akhtar Kausar Date: Mon, 29 Apr 2024 02:08:43 +0200 Subject: [PATCH] Mejorado coverage en wikiquery, creo --- questionservice/wikiUtils/wikiQuery.test.js | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/questionservice/wikiUtils/wikiQuery.test.js b/questionservice/wikiUtils/wikiQuery.test.js index 4d048fc..40491cc 100644 --- a/questionservice/wikiUtils/wikiQuery.test.js +++ b/questionservice/wikiUtils/wikiQuery.test.js @@ -11,6 +11,11 @@ describe("WikiQuery", () => { beforeEach(() => { // Limpiar mocks antes de cada test jest.clearAllMocks(); + Question.mockImplementation(({ question, answers, questionCategory }) => ({ + question, + answers, + questionCategory + })); }); it("debería obtener preguntas de Wikidata y formatearlas correctamente", async () => { @@ -54,4 +59,48 @@ describe("WikiQuery", () => { expect(questions).toHaveLength(mockResults.length); }); + it("debería reemplazar respuestas con formato URL y manejar respuestas repetidas usando respuestas de resguardo", async () => { + const mockResults = [ + { questionLabel: { value: "¿Cuál es el río más largo?" }, answerLabel: { value: "http://amazon.com" } }, + { questionLabel: { value: "¿Cuál es el río más largo?1" }, answerLabel: { value: "http://nile.com" } }, + { questionLabel: { value: "¿Cuál es el río más largo?2" }, answerLabel: { value: "Nilo" } }, + { questionLabel: { value: "¿Cuál es el río más largo?3" }, answerLabel: { value: "Nilo" } } // respuesta repetida + ]; + + // Mock para las respuestas de resguardo + const mockBackupAnswers = [ + { itemLabel: { value: "Mississippi" } }, + { itemLabel: { value: "Yangtsé" } } + ]; + + wikiCall.mockImplementation(query => { + if (query.includes("LIMIT 100")) { + return Promise.resolve(mockBackupAnswers); + } else { + return Promise.resolve(mockResults); + } + }); + + const template = { + questionVariable: "?q", + answerVariable: "?a", + question: "¿Cuál es el río más largo de __x__?", + year: false, + questionCategory: "Geografía" + }; + const limitValue = 4; + + const questions = await WikiQuery.getQuestions(template, limitValue, mockBackupAnswers); + + // Verificar que las respuestas URL son reemplazadas por 'No hay' + expect(questions[0].answers.some(ans => ans.answer === "No hay")).toBe(true); + expect(questions[1].answers.some(ans => ans.answer === "No hay")).toBe(true); + + // Verificar que las respuestas repetidas son reemplazadas por respuestas de resguardo + expect(questions.some(q => q.answers.some(ans => ans.answer === "Mississippi" || ans.answer === "Yangtsé"))).toBe(true); + + // Verificar el llamado correcto a wikiCall + expect(wikiCall).toHaveBeenCalledTimes(1); + }); + });