Skip to content

Commit

Permalink
Merge pull request #51 from Arquisoft/QuestionGeneration
Browse files Browse the repository at this point in the history
Question generation
  • Loading branch information
Mister-Mario authored Mar 9, 2024
2 parents 73ba39d + 45cca92 commit b7a3a2b
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package com.wiq.wiq.services;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.wiq.wiq.services.questionGenerator.QuestionGenerator;
import com.wiq.wiq.services.questionGenerator.question.QuestionType;

import static org.junit.jupiter.api.Assertions.*;

import java.util.LinkedList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QuestionGeneratorTests {

private QuestionGenerator qgEN;
private QuestionGenerator qgES;

private static QuestionType[] types = {QuestionType.POPULATION, QuestionType.CAPITAL, QuestionType.SIZE, QuestionType.LANGUAGE};

@BeforeEach
void setUp(){
qgEN = new QuestionGenerator("en");
qgES = new QuestionGenerator("es");
}

@Test
void testGenerateQuestionsEnglish() {

String question;

for(QuestionType t : types) {
for(int i=0; i<3; i++) {
question = qgEN.generateQuestion(t);
JSONObject json = null;

//Check correct format
try {
json = new JSONObject(question);
} catch (JSONException e) {
fail("Not a JSON");
}

assertNotNull(json, "JSONObject was not initialized");

//Check for expected fields
try {
// Test question
assertTrue(json.has("question"));


// Test answers
assertTrue(json.has("answers"));

// Retrieve the "answers" field and check if it's an array
Object answersField = json.get("answers");
assertTrue(answersField instanceof JSONArray, "The 'answers' field is not an array");

// Convert the field to a JSONArray
JSONArray answersArray = (JSONArray) answersField;

// Check if the array has size 4
assertEquals(4, answersArray.length(), "There aren't 4 answers");

// Check for unique answers
List<String> seenAnswers = new LinkedList<String>();
for (int j = 0; j < answersArray.length(); j++) {
String answer = answersArray.getString(j);
assertFalse(seenAnswers.contains(answer), "Answer: " + answer + " is duplicated");
seenAnswers.add(answer);
}
} catch (JSONException e) {
fail("Exception occurred while parsing JSON: " + e.getMessage());
}
}
}

}

@Test
void testGenerateQuestionsSpanish() {

String question;

for(QuestionType t : types) {
for(int i=0; i<3; i++) {
question = qgES.generateQuestion(t);
JSONObject json = null;

//Check correct format
try {
json = new JSONObject(question);
} catch (JSONException e) {
fail("Not a JSON");
}

assertNotNull(json, "JSONObject was not initialized");

//Check for expected fields
try {
// Test question
assertTrue(json.has("question"));


// Test answers
assertTrue(json.has("answers"));

// Retrieve the "answers" field and check if it's an array
Object answersField = json.get("answers");
assertTrue(answersField instanceof JSONArray, "The 'answers' field is not an array");

// Convert the field to a JSONArray
JSONArray answersArray = (JSONArray) answersField;

// Check if the array has size 4
assertEquals(4, answersArray.length(), "There aren't 4 answers");

// Check for unique answers
List<String> seenAnswers = new LinkedList<String>();
for (int j = 0; j < answersArray.length(); j++) {
String answer = answersArray.getString(j);
assertFalse(seenAnswers.contains(answer), "Answer: " + answer + " is duplicated");
seenAnswers.add(answer);
}
} catch (JSONException e) {
fail("Exception occurred while parsing JSON: " + e.getMessage());
}
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.wiq.wiq.services.questionGenerator.question;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class QuestionTests {

private Question question;

@BeforeEach
void setUp() {
List<String> answers = Arrays.asList("A", "B", "C");
question = new Question("What is the capital of France?", answers);
}

@Test
void testGetQuestion() {
assertEquals("What is the capital of France?", question.getQuestion());
}

@Test
void testSetQuestion() {
question.setQuestion("What is the capital of Germany?");
assertEquals("What is the capital of Germany?", question.getQuestion());
}

@Test
void testGetAnswers() {
List<String> expectedAnswers = Arrays.asList("A", "B", "C");
assertEquals(expectedAnswers, question.getAnswers());
}

@Test
void testSetAnswers() {
List<String> newAnswers = Arrays.asList("X", "Y", "Z");
question.setAnswers(newAnswers);
assertEquals(newAnswers, question.getAnswers());
}

@Test
void testAddRightAnswer() {
question.addRightAnswer("Paris");
List<String> expectedAnswers = Arrays.asList("Paris", "A", "B", "C");
assertEquals(expectedAnswers, question.getAnswers());
}

@Test
void testGetJSON() {
try {
JSONObject expectedJson = new JSONObject()
.put("question", "What is the capital of France?")
.put("answers", new JSONArray().put("A").put("B").put("C"));
assertEquals(expectedJson.toString(), question.getJSON().toString());
} catch (JSONException e) {
fail("JSONException occurred: " + e.getMessage());
}
}
}

0 comments on commit b7a3a2b

Please sign in to comment.