Merge branch 'main' into feature/table
This commit is contained in:
+69
-3
@@ -1,11 +1,76 @@
|
||||
import json
|
||||
|
||||
from bs4 import BeautifulSoup
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from mistletoe import Document
|
||||
|
||||
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
|
||||
|
||||
|
||||
def question_stub(input):
|
||||
return f"<question>{input}</question>" # TODO implement this stub
|
||||
def parse_katalog():
|
||||
with open('data/fragenkatalog3b.json') as fragenkatalog_file:
|
||||
fragenkatalog = json.load(fragenkatalog_file)
|
||||
|
||||
questions = {}
|
||||
|
||||
for exampart in fragenkatalog["sections"] :
|
||||
for chapter in exampart["sections"] :
|
||||
if "questions" in chapter :
|
||||
for question in chapter["questions"] :
|
||||
questions[question["number"]] = question
|
||||
if "sections" in chapter :
|
||||
for section in chapter["sections"] :
|
||||
for question in section["questions"] :
|
||||
questions[question["number"]] = question
|
||||
|
||||
return questions
|
||||
|
||||
env = Environment(loader=FileSystemLoader('templates/html'))
|
||||
question_template = env.get_template('question.html')
|
||||
questions = parse_katalog()
|
||||
|
||||
def question_stub(input):
|
||||
""" Combines the original question dataset from BNetzA with our internal metadata """
|
||||
with open('data/metadata.json') as metadata_file :
|
||||
metadata = json.load(metadata_file)
|
||||
number = metadata[f"{input}"]["number"] # Fragennummer z.B. AB123
|
||||
|
||||
question = questions[number]
|
||||
metadata = metadata[f"{input}"]
|
||||
|
||||
if "answer_a" in question :
|
||||
answers = [
|
||||
question["answer_a"],
|
||||
question["answer_b"],
|
||||
question["answer_c"],
|
||||
question["answer_d"]
|
||||
]
|
||||
else :
|
||||
answers = []
|
||||
|
||||
if metadata["picture_a"] != "" :
|
||||
answer_pictures = [
|
||||
metadata["picture_a"],
|
||||
metadata["picture_b"],
|
||||
metadata["picture_c"],
|
||||
metadata["picture_d"]
|
||||
]
|
||||
else :
|
||||
answer_pictures = []
|
||||
|
||||
if "picture_question" in question :
|
||||
picture_question = metadata["picture_question"]
|
||||
else :
|
||||
picture_question = ""
|
||||
|
||||
return question_template.render(
|
||||
question=question["question"],
|
||||
number=number,
|
||||
layout=metadata["layout"],
|
||||
picture_question=picture_question,
|
||||
answers=answers,
|
||||
answer_pictures=answer_pictures
|
||||
)
|
||||
|
||||
def test_html(capsys):
|
||||
with capsys.disabled():
|
||||
@@ -14,4 +79,5 @@ def test_html(capsys):
|
||||
with FiftyOhmHtmlRenderer(question_stub) as renderer:
|
||||
output = renderer.render(Document(content))
|
||||
with open("test/acceptanceTest.html", "w") as output_file:
|
||||
output_file.write(output)
|
||||
pretty = BeautifulSoup(output, 'html.parser').prettify()
|
||||
output_file.write(pretty)
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import mistletoe
|
||||
import pytest
|
||||
|
||||
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
|
||||
from renderer.fifty_ohm_latex_renderer import FiftyOhmLaTeXRenderer
|
||||
from test.util import paragraph
|
||||
|
||||
|
||||
@pytest.mark.html
|
||||
def test_photo_html():
|
||||
assertions = {
|
||||
"[photo:123:abc:Text]": FiftyOhmHtmlRenderer.render_photo_helper("123", "abc", "Text", "TODO"),
|
||||
}
|
||||
|
||||
for assertion in assertions:
|
||||
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == paragraph(assertions[assertion])
|
||||
|
||||
|
||||
@pytest.mark.latex
|
||||
def test_photo_latex():
|
||||
assertions = {
|
||||
"[photo:123:abc:Text]": FiftyOhmLaTeXRenderer.render_photo_helper("123", "abc", "Text", "TODO"),
|
||||
}
|
||||
|
||||
for assertion in assertions:
|
||||
assert mistletoe.markdown(assertion, FiftyOhmLaTeXRenderer) == "\n" + assertions[assertion] + "\n"
|
||||
+25
-5
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
|
||||
import pytest
|
||||
from mistletoe import Document
|
||||
|
||||
@@ -22,18 +24,36 @@ def test_question_html():
|
||||
for assertion in assertions:
|
||||
assert renderer.render(Document(assertion)) == assertions[assertion]
|
||||
|
||||
@pytest.mark.html
|
||||
def test_question_translation_html():
|
||||
assertions = {
|
||||
"[question:123]": "ED111\n",
|
||||
"\n[question:123]\n": "ED111\n",
|
||||
"\n[question:123]\n[question:123]": "ED111\nED111\n",
|
||||
"Foo\n[question:123]\nBar": paragraph("Foo") + "ED111\n" + paragraph("Bar"),
|
||||
}
|
||||
|
||||
def test_function(input):
|
||||
with open('data/metadata.json') as file:
|
||||
metadata = json.load(file)
|
||||
return metadata[f"{input}"].get("number")
|
||||
|
||||
with FiftyOhmHtmlRenderer(test_function) as renderer:
|
||||
for assertion in assertions:
|
||||
assert renderer.render(Document(assertion)) == assertions[assertion]
|
||||
|
||||
|
||||
@pytest.mark.latex
|
||||
def test_question_latex():
|
||||
assertions = {
|
||||
"[question:123]": "123",
|
||||
"\n[question:123]\n": "123",
|
||||
"\n[question:123]\n[question:123]": "123123",
|
||||
"Foo\n[question:123]\nBar": "\nFoo\n123\nBar\n",
|
||||
"[question:123]": "\question{123}",
|
||||
"\n[question:123]\n": "\question{123}",
|
||||
"\n[question:123]\n[question:123]": "\question{123}\question{123}",
|
||||
"Foo\n[question:123]\nBar": "\nFoo\n\question{123}\nBar\n",
|
||||
}
|
||||
|
||||
def test_function(input):
|
||||
return f"{input}"
|
||||
return "\\question{"+input+"}"
|
||||
|
||||
with FiftyOhmLaTeXRenderer(test_function) as renderer:
|
||||
for assertion in assertions:
|
||||
|
||||
Reference in New Issue
Block a user