1
0
Files
medical-notes/stroma/quiz/utils/tests/test_unified_parser.py
Johan Dahlin 50366b9b9c
All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 2m29s
vault backup: 2025-12-26 02:09:22
2025-12-26 02:09:22 +01:00

188 lines
4.6 KiB
Python

import datetime
from quiz.utils.unified_parser import UnifiedParser, QuestionType
def test_parse_mcq_question():
content = """---
tags: [frågetyp/mcq, ah2]
date: 2024-03-21
---
Question?
- A: Yes
- B: No
- C: Maybe
- D: Never
```spoiler-block:
A och D
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.MCQ
assert data.question == "Question?"
assert data.answer == ["A", "D"]
assert data.num_questions == 1
assert data.is_complete is True
assert data.options == ["A: Yes", "B: No", "C: Maybe", "D: Never"]
assert data.metadata == {"tags": ["frågetyp/mcq", "ah2"], "date": datetime.date(2024, 3, 21)}
assert not data.sub_questions
def test_parse_scq_question():
content = """---
tags: [frågetyp/scq]
---
Pick one:
- A: One
- B: Two
```spoiler-block:
B
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.SCQ
assert data.question == "Pick one:"
assert data.answer == "B"
assert data.num_questions == 1
assert data.is_complete is True
assert data.options == ["A: One", "B: Two"]
assert not data.sub_questions
def test_parse_textfält_question():
content = """---
tags: [frågetyp/textfält]
---
Name these:
a) Part 1
b) Part 2
```spoiler-block:
a) Left
b) Right
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.TEXTFÄLT
assert data.question == "Name these:"
assert data.answer == ["a) Left", "b) Right"]
assert data.num_questions == 2
assert len(data.sub_questions) == 2
assert data.sub_questions[0].id == "a"
assert data.sub_questions[0].text == "Part 1"
assert data.sub_questions[0].answer == "a) Left"
assert data.sub_questions[0].options is None
def test_parse_matching_question():
content = """---
tags: [frågetyp/matching]
---
Match:
- 1
- 2
- A
- B
```spoiler-block:
1: A
2: B
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.MATCHING
assert data.question == "Match:"
assert data.answer == [["1", "A"], ["2", "B"]]
assert data.num_questions == 1
assert data.options == ["1", "2", "A", "B"]
assert not data.sub_questions
def test_parse_question_with_image_and_instruction():
content = """---
tags: [frågetyp/scq]
---
**Välj ett alternativ:**
![[brain.png|300]]
What is this?
- A: Brain
- B: Heart
```spoiler-block:
A
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.SCQ
assert data.question == "What is this?"
assert data.instruction == "Välj ett alternativ:"
assert data.image == "![[brain.png]]"
assert data.is_complete is True
def test_parse_field_question_with_ranges():
content = """---
tags: [frågetyp/sifferfält]
---
Identify the structures:
a) Arachnoidea? (1..10)
(0.5 p)
b) Cortex cerebri (1..10)
(0.5 p)
```spoiler-block:
a) 7
b) 3
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.SIFFERFÄLT
assert data.num_questions == 2
assert len(data.sub_questions) == 2
# Part A
assert data.sub_questions[0].id == "a"
assert data.sub_questions[0].text == "Arachnoidea?"
assert data.sub_questions[0].options == [str(x) for x in range(1, 11)]
assert data.sub_questions[0].answer == "a) 7"
# Part B
assert data.sub_questions[1].id == "b"
assert data.sub_questions[1].text == "Cortex cerebri"
assert data.sub_questions[1].options == [str(x) for x in range(1, 11)]
assert data.sub_questions[1].answer == "b) 3"
def test_parse_field_question_with_list_options():
content = """---
tags: [frågetyp/sifferfält]
---
a) First (A, B, C)
b) Second (1, 2, 3)
```spoiler-block:
a) A
b) 2
```"""
data = UnifiedParser(content).parse()
assert data.sub_questions[0].options == ["A", "B", "C"]
assert data.sub_questions[1].options == ["1", "2", "3"]
def test_parse_hotspot_question():
content = """---
tags: [frågetyp/hotspot]
---
Klicka på hippocampus!
```spoiler-block:
![[brain_atlas.png]]
Det här är hippocampus.
```"""
data = UnifiedParser(content).parse()
assert data.type == QuestionType.HOTSPOT
assert data.answer == "Det här är hippocampus."
assert data.answer_image == "![[brain_atlas.png]]"
assert data.is_complete is True
def test_completeness_missing_sub_questions():
content = """---
tags: [frågetyp/textfält]
---
a) one
b) two
```spoiler-block:
a) found
```"""
data = UnifiedParser(content).parse()
assert data.num_questions == 2
assert data.is_complete is False
assert len(data.sub_questions) == 2
assert data.sub_questions[0].answer == "a) found"
assert data.sub_questions[1].answer is None