Skip to content

Commit

Permalink
Finallized question-service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFN2 committed Apr 5, 2024
1 parent 0d26788 commit 8d0561d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
45 changes: 26 additions & 19 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) });
});
Expand All @@ -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);
Expand Down Expand Up @@ -77,42 +77,49 @@ 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")
});

//Test /imgs/answer endpoint (Incorrect answer)
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))
});
});

Expand Down

0 comments on commit 8d0561d

Please sign in to comment.