From 446f2387f0f623b65386a42f88798ed263a03e04 Mon Sep 17 00:00:00 2001 From: Matthias Jung Date: Mon, 17 Aug 2026 19:18:56 +0200 Subject: [PATCH] feature: handle SWL questions --- src/build.py | 28 +++++++++++- src/config.py | 1 + test/test_swl_question_catalog.py | 73 +++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 test/test_swl_question_catalog.py diff --git a/src/build.py b/src/build.py index 41be79f..19b974b 100644 --- a/src/build.py +++ b/src/build.py @@ -26,10 +26,32 @@ class Build: self.env.filters["shuffle_answers"] = self.__filter_shuffle_answers with self.config.p_data_fragenkatalog.open(encoding="utf-8") as file: self.fragenkatalog = json.load(file) + + supplemental_questions = {} + if self.config.p_data_fragenkatalog_swl.exists(): + with self.config.p_data_fragenkatalog_swl.open(encoding="utf-8") as file: + swl_fragenkatalog = json.load(file) + supplemental_questions = self.__parse_katalog(swl_fragenkatalog) + self.fragenkatalog["sections"].extend(swl_fragenkatalog.get("sections", [])) + with self.config.p_data_metadata.open(encoding="utf-8") as file: self.question_metadata = json.load(file) self.questions = self.__parse_katalog(self.fragenkatalog) + for number in supplemental_questions: + self.question_metadata.setdefault( + number, + { + "directus_id": "", + "picture_question": "", + "picture_a": "", + "picture_b": "", + "picture_c": "", + "picture_d": "", + "layout": "", + }, + ) + self.question_index = {} self.question_token_pattern = re.compile(r"^\s*\[question:([\w\d]+)\]", re.MULTILINE) @@ -143,8 +165,10 @@ class Build: def __build_exam_simulator(self): self.config.p_build_assets.mkdir(parents=True, exist_ok=True) - shutil.copyfile(self.config.p_data_fragenkatalog, self.config.p_build_assets / "fragenkatalog.json") - shutil.copyfile(self.config.p_data_metadata, self.config.p_build_assets / "metadata.json") + with (self.config.p_build_assets / "fragenkatalog.json").open("w", encoding="utf-8") as file: + json.dump(self.fragenkatalog, file, ensure_ascii=False, indent=4) + with (self.config.p_build_assets / "metadata.json").open("w", encoding="utf-8") as file: + json.dump(self.question_metadata, file, ensure_ascii=False, indent=4) (self.config.p_build_assets / "exam-questions.json").unlink(missing_ok=True) picture_fields = ("picture_question", "picture_a", "picture_b", "picture_c", "picture_d") diff --git a/src/config.py b/src/config.py index 97d1dbb..e18e344 100644 --- a/src/config.py +++ b/src/config.py @@ -29,6 +29,7 @@ class Config: self.p_data_solutions = self.p_data_contents / "solutions" self.p_data_fragenkatalog = self.p_data_questions / "fragenkatalog3b.json" + self.p_data_fragenkatalog_swl = self.p_data_questions / "fragenkatalog_swl.json" self.p_data_metadata = self.p_data_questions / "metadata3b.json" self.p_build = Path(self.get_config_value("output", "./build") if build_path is None else build_path) diff --git a/test/test_swl_question_catalog.py b/test/test_swl_question_catalog.py new file mode 100644 index 0000000..ef1cea6 --- /dev/null +++ b/test/test_swl_question_catalog.py @@ -0,0 +1,73 @@ +import json + +from src.build import Build +from src.config import Config + + +def write_catalog(path, number): + path.write_text( + json.dumps( + { + "sections": [ + { + "title": f"Katalog {number}", + "questions": [ + { + "number": number, + "class": "SWL" if number.startswith("SWL") else "1", + "question": f"Frage {number}", + "answer_a": "A", + "answer_b": "B", + "answer_c": "C", + "answer_d": "D", + } + ], + } + ] + } + ), + encoding="utf-8", + ) + + +def prepare_content(tmp_path, include_swl): + questions_path = tmp_path / "contents" / "questions" + questions_path.mkdir(parents=True) + write_catalog(questions_path / "fragenkatalog3b.json", "NA101") + (questions_path / "metadata3b.json").write_text( + json.dumps( + { + "NA101": { + "directus_id": "1", + "picture_question": "", + "picture_a": "", + "picture_b": "", + "picture_c": "", + "picture_d": "", + "layout": "", + } + } + ), + encoding="utf-8", + ) + if include_swl: + write_catalog(questions_path / "fragenkatalog_swl.json", "SWL001") + + +def test_optional_swl_catalog_is_loaded_with_default_metadata(tmp_path): + prepare_content(tmp_path, include_swl=True) + + build = Build(Config(content_path=tmp_path, build_path=tmp_path / "build")) + + assert set(build.questions) == {"NA101", "SWL001"} + assert build.question_metadata["SWL001"]["layout"] == "" + assert len(build.fragenkatalog["sections"]) == 2 + + +def test_missing_optional_swl_catalog_is_allowed(tmp_path): + prepare_content(tmp_path, include_swl=False) + + build = Build(Config(content_path=tmp_path, build_path=tmp_path / "build")) + + assert set(build.questions) == {"NA101"} + assert len(build.fragenkatalog["sections"]) == 1