diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c926570..b1fe5c5 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,7 +18,9 @@ jobs: - run: npm --prefix users/authservice ci - run: npm --prefix users/userservice ci - run: npm --prefix gatewayservice ci + - run: npm --prefix questionservice ci - run: npm --prefix webapp ci + - run: npm --prefix questionservice test -- --coverage - run: npm --prefix users/authservice test -- --coverage - run: npm --prefix users/userservice test -- --coverage - run: npm --prefix gatewayservice test -- --coverage diff --git a/questionservice/question-service.js b/questionservice/question-service.js index fc2d896..16e0c98 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -55,7 +55,7 @@ class WIQ_API{ let finalChosenLabels = [] //I filter in case the label does not have a proper name //and just a wikidata identifier (Q followed by numbers) - const regex = /^Q\d+$/ + const regex = /Q\d*/ while(regex.test(data.results.bindings[chosenNums[0]].itemLabel.value)){ this.#getRandomNumNotInSetAndUpdate(itemsNum,chosenNums) chosenNums[0] = chosenNums.pop() @@ -72,12 +72,12 @@ class WIQ_API{ finalChosenLabels.push(data.results.bindings[chosenNums[i+1]].itemLabel.value) } - let counter = chosenNums.length + let counter = 0 while(chosenNums.length>0){ imgs.push(data.results.bindings[chosenNums.pop()].image.value) associates.push(finalChosenLabels.pop()) imgToAssociatedMap.set(imgs[counter], associates[counter]) - counter-- + counter++ } //Choose a random item of the chosen to make the question @@ -206,8 +206,6 @@ app.get('/imgs/foods/question', async (req, res) => { app.post('/imgs/answer', (req, res) => { const answer = req.body; - console.log(correctImg) - if(correctImg==answer){ res.status(200).json({ correct: "true" diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js index a548864..61c035c 100644 --- a/questionservice/question-service.test.js +++ b/questionservice/question-service.test.js @@ -15,8 +15,8 @@ jest.spyOn(global, 'fetch').mockImplementation(() => { let result = { results: {bindings: []} } for(let i=1;i<=100;i++){ //Simulating there is maximum number of repeated itemLabels (between the valid ones) - result.results.bindings.push({itemLabel: {value: "itemName1"} , image:{value: "imageUrlX"+i}}) - imgsToAssociatesMap.set("imageUrlX"+i, "itemName1") + result.results.bindings.push({itemLabel: {value: "itemName1"} , image:{value: "imageUrl1_"+i}}) + imgsToAssociatesMap.set("imageUrl1_"+i, "itemName1") } for(let i=101;i<=195;i++){ //Simulating there are invalid itemLabels @@ -26,7 +26,7 @@ jest.spyOn(global, 'fetch').mockImplementation(() => { for(let i=1;i<=4;i++){ //Chosen elements (One of them will be one of the 196 elems with itemLabel 'itemName1' and 'imageUrlX') result.results.bindings.push({itemLabel: {value: "itemName"+i} , image:{value: "imageUrl"+i}}) - imgsToAssociatesMap.set("imageUrl"+i, "itemName"+i) + imgsToAssociatesMap.set("imageUrl"+i,"itemName"+i) } return Promise.resolve({ json: () => Promise.resolve(result) }); }); @@ -35,7 +35,7 @@ describe('Question Service', () => { // Test /imgs/flags/question endpoint it('should return a flags question with 4 images as options', async () => { const response = await request(app).get('/imgs/flags/question'); - const itemLabelsSet = getItemLabelsIfDifferent(response.body); + const itemLabelsSet = getItemLabelsIfDifferent(response.body.images); checkInvalidElementsNotChosen(itemLabelsSet); checkQuestionValidity("Which of the following flags belongs to",response.body.question, itemLabelsSet); @@ -77,17 +77,23 @@ describe('Question Service', () => { it('should inform if the answer is correct', async () => { //First I ask a question const response = await request(app).get('/imgs/foods/question'); - regex = new RegExp(`Which of the following foods corresponds to (\\w+)\\?`); + regex = new RegExp(`Which of the following images corresponds to (\\w+)\\?`); const match = response.body.question.match(regex); const correctAnswerLabel = match && match[1]; //I get and send the correct answer - const correctImage = imgsToAssociatesMap.get(correctAnswerLabel) - console.log(response.body.question) + let correctImage + let counter = 0 + while(true){ + if(correctAnswerLabel==imgsToAssociatesMap.get(response.body.images[counter])){ + correctImage = response.body.images[counter] + break; + } + counter++ + } const responseAnswer = await request(app) .post("/imgs/answer") .set('Content-Type', 'text/plain') .send(correctImage) - //console.log(responseAnswer.body) expect(responseAnswer.body.correct).toBe("true") }); @@ -95,24 +101,25 @@ describe('Question Service', () => { it('should inform the answer is incorrect and what is the element associated to the answer', async () => { //First I ask a question const response = await request(app).get('/imgs/foods/question'); - regex = new RegExp(`Which of the following foods corresponds to (\\w+)\\?`); + regex = new RegExp(`Which of the following images corresponds to (\\w+)\\?`); const match = response.body.question.match(regex); const correctAnswerLabel = match && match[1]; - //I get the correct answer - const correctImage = imgsToAssociatesMap.get(correctAnswerLabel) - //I choose an incorrect answer - let incorrectAnswer - for(let i=0;i<4;i++){ - if(response.body.images[i]!=correctImage){ - incorrectAnswer = response.body.images[i] - break + //I get an incorrect answer + let incorrectImageAnswer + let counter = 0 + while(true){ + if(correctAnswerLabel!=imgsToAssociatesMap.get(response.body.images[counter])){ + incorrectImageAnswer = response.body.images[counter] + break; } + counter++ } const responseAnswer = await request(app) .post("/imgs/answer") - .send(incorrectAnswer) + .set('Content-Type', 'text/plain') + .send(incorrectImageAnswer) expect(responseAnswer.body.correct).toBe("false") - // expect(responseAnswer.body.associate).toBe(imgsToAssociatesMap.get(incorrectAnswer)) + expect(responseAnswer.body.associate).toBe(imgsToAssociatesMap.get(incorrectImageAnswer)) }); });