Skip to content

Commit

Permalink
#7 add a test for multi-file setups
Browse files Browse the repository at this point in the history
  • Loading branch information
SauliusTheBlack committed Dec 29, 2023
1 parent feeab12 commit a6e8f76
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
40 changes: 9 additions & 31 deletions converter/src/converter/convert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import sys, os
from converter.translations import translations
from translations import translations

# this makes classes serialisable
class MyEncoder(json.JSONEncoder):
Expand Down Expand Up @@ -50,15 +50,13 @@ def readSingleFile(filename):
print("Reading " + filename)
currentQuestion = None
with open(filename ,'r', encoding='utf-8') as questionFile:
for line in questionFile:

for line in questionFile:
if not ':' in line:
if currentQuestion and line != "\n":
currentQuestion.appendToLastUsedField(line)
continue

key, value = line.split(':',1)
# print(key,"-",value)
if value.strip() == "/" or value.strip() == "\\":
print("Skipping line " + line + " because content is single forward or backward slash")
continue
Expand Down Expand Up @@ -94,17 +92,15 @@ def getAllQuestionsOrderedByRoundSpec(unhandledQuestions):
import re
import copy
#loop over the rounds, find if there is a round that has a CATEGORY: spec, and create a new round containing these questions
unhandledQuestions.sort(key=lambda question: [round.lower() for round in rounds].index(question.round.lower()))
print(unhandledQuestions)
unhandledQuestions.sort(key=lambda question: [round.lower() for round in rounds].index(question.round.lower()))
questionsToAppend = []
for round in rounds:
if round in specifications:
roundSpecs = specifications[round]
categoryPattern = re.compile("CATEGORY:.*")
matchingPatterns = list(filter(categoryPattern.match, roundSpecs))
if(matchingPatterns):
# print("Category found, need to filter all rounds for this category now")

categoryToExtract = matchingPatterns[0].split(":")[1]
for question in allQuestions:
if question.category == categoryToExtract:
Expand All @@ -116,17 +112,13 @@ def getAllQuestionsOrderedByRoundSpec(unhandledQuestions):

#Sort the question list by the round value for every question object, so that it matches the Rounds value in the spec file
unhandledQuestions.sort(key=lambda question: [round.lower() for round in rounds].index(question.round.lower()))
print("---")
print(unhandledQuestions)
print("---")

print("===")
print(unhandledQuestions)
print("---")

return unhandledQuestions


def getAllQuestionsFromFiles(inFiles):
allQuestions = []
for fileName in inFiles:
allQuestions += readSingleFile(fileName)
return allQuestions

usageText = """
USAGE:
Expand All @@ -149,15 +141,11 @@ def usage():
config = json.load(f)
lines = f.readlines()

print(config)
if("base_dir" in config["settings"]):
print(f'appending custom base dir [{config["settings"]["base_dir"]}] to [{baseInOutDir}]')
baseInOutDir += "/" + config["settings"]["base_dir"]


print(configFile, baseInOutDir)
allQuestionsByRoundMap = {}

outFilename = baseInOutDir + "/questions.js"

with open(baseInOutDir + "/settings.js","w") as h:
Expand All @@ -169,26 +157,19 @@ def usage():
rounds = config["rounds"]
specifications = config["specs"]

for fileName in infiles:
allQuestions = readSingleFile(baseInOutDir+"/"+fileName)

allQuestions = getAllQuestionsFromFiles([baseInOutDir+"/" + infile for infile in infiles])
questionSortedByRound = getAllQuestionsOrderedByRoundSpec(allQuestions)
print("dumping allQuestions")
print(questionSortedByRound)

questions = []
currentRound = None
currentRoundName = ""
currentRoundQuestions = []
for question in questionSortedByRound:
if currentRoundName != question.round.strip():
print(f"switching to the next round: {question.round.strip()}")
if(currentRound != None):
currentRound["name"] = currentRoundName
currentRound["questions"] = currentRoundQuestions
print(rounds, currentRoundName)
if(currentRoundName in rounds):
print("storing questions in main object")
questions.append(currentRound)
else:
print(f"Couldn't find round '{currentRoundName}' in rounds {rounds}")
Expand All @@ -203,9 +184,6 @@ def usage():
if(currentRoundName in rounds):
questions.append(currentRound)


print(questions)

#this next part is not optimized for performance or resource utilisation, but works fine
for round in questions:
if (round["name"] in specifications and 'NO_CATEGORY_SORT' in specifications[round["name"]]):
Expand Down
8 changes: 6 additions & 2 deletions converter/tests/test_generalFunctionality.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src.converter.convert import readSingleFile
from src.converter.convert import readSingleFile, getAllQuestionsFromFiles

def test_singleQuestion():
foundQuestions = readSingleFile("tests/resources/SingleMinimalQuestion_NL.txt")
Expand All @@ -11,7 +11,7 @@ def test_singleQuestion():
assert question.category == "This is the category"
assert question.round == "This is the round"

def test_multipleQuestions():
def test_multipleQuestions_oneFile():
foundQuestions = readSingleFile("tests/resources/MultipleMinimalQuestions_NL.txt")
assert len(foundQuestions) == 2

Expand All @@ -29,6 +29,10 @@ def test_multipleQuestions():
assert question.category == "This is the category 2"
assert question.round == "This is the round 2"

def test_multipleQuestions_twoInputFiles():
allQuestions = getAllQuestionsFromFiles(["tests/resources/SingleMinimalQuestion_NL.txt","tests/resources/SingleQuestion_withNewlines_NL.txt"])
assert len(allQuestions) == 2

#maintain newlines in strings
def test_lineWithoutPrefixShouldBeAddedToPreviousObject():
foundQuestions = readSingleFile("tests/resources/SingleQuestion_withNewlines_NL.txt")
Expand Down
19 changes: 19 additions & 0 deletions test/kwismaster_multiInputFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"category_order": [
"this is the category", "this is the category 2"
],
"input_files": [
"converter/tests/resources/SingleQuestion_withNewlines_NL.txt",
"converter/tests/resources/SingleMinimalQuestion_NL.txt"
],
"rounds": [
"This is the round", "This is the round 2"
],
"specs": {

},
"settings": {
"base_dir": ".."
}

}
18 changes: 18 additions & 0 deletions test/kwismaster_singleInputFile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"category_order": [
"this is the category", "this is the category 2"
],
"input_files": [
"converter/tests/resources/SingleQuestion_withNewlines_NL.txt"
],
"rounds": [
"This is the round", "This is the round 2"
],
"specs": {

},
"settings": {
"base_dir": ".."
}

}

0 comments on commit a6e8f76

Please sign in to comment.