generated from Arquisoft/wiq_0
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from Arquisoft/QuestionGeneration
Question generation
- Loading branch information
Showing
2 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
backend/wiq/src/test/java/com/wiq/wiq/services/QuestionGeneratorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
backend/wiq/src/test/java/com/wiq/wiq/services/questionGenerator/question/QuestionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |