| 1 | # tests/stubs/single_choice_question.py
|
|---|
| 2 | import pytest
|
|---|
| 3 | from datetime import datetime
|
|---|
| 4 | from dataclasses import dataclass, field
|
|---|
| 5 | from typing import List
|
|---|
| 6 | from flexoentity import FlexoEntity, EntityType, EntityState, Domain
|
|---|
| 7 |
|
|---|
| 8 | @pytest.fixture
|
|---|
| 9 | def fixed_datetime(monkeypatch):
|
|---|
| 10 | class FixedDate(datetime):
|
|---|
| 11 | @classmethod
|
|---|
| 12 | def now(cls, tz=None):
|
|---|
| 13 | return datetime(2025, 11, 1, tzinfo=tz)
|
|---|
| 14 | monkeypatch.setattr("flexoentity.id_factory.datetime", FixedDate)
|
|---|
| 15 | return FixedDate
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | @dataclass
|
|---|
| 19 | class AnswerOption:
|
|---|
| 20 | id: str
|
|---|
| 21 | text: str
|
|---|
| 22 | points: float = 0.0
|
|---|
| 23 |
|
|---|
| 24 | def to_dict(self):
|
|---|
| 25 | return {"id": self.id, "text": self.text, "points": self.points}
|
|---|
| 26 |
|
|---|
| 27 | @classmethod
|
|---|
| 28 | def from_dict(cls, data):
|
|---|
| 29 | return cls(
|
|---|
| 30 | id=data.get("id", ""),
|
|---|
| 31 | text=data.get("text", ""),
|
|---|
| 32 | points=data.get("points", 0.0)
|
|---|
| 33 | )
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | @dataclass
|
|---|
| 37 | class SingleChoiceQuestion(FlexoEntity):
|
|---|
| 38 | """A minimal stub to test FlexoEntity integration."""
|
|---|
| 39 | text: str = ""
|
|---|
| 40 | options: List[AnswerOption] = field(default_factory=list)
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | @classmethod
|
|---|
| 44 | def default(cls):
|
|---|
| 45 | return cls(domain=Domain(domain="GEN",
|
|---|
| 46 | entity_type=EntityType.DOMAIN,
|
|---|
| 47 | state=EntityState.DRAFT),
|
|---|
| 48 | state=EntityState.DRAFT, entity_type=EntityType.ITEM)
|
|---|
| 49 |
|
|---|
| 50 | def to_dict(self):
|
|---|
| 51 | base = super().to_dict()
|
|---|
| 52 | base.update({
|
|---|
| 53 | "text": self.text,
|
|---|
| 54 | "options": [opt.to_dict() for opt in self.options],
|
|---|
| 55 | })
|
|---|
| 56 | return base
|
|---|
| 57 |
|
|---|
| 58 | @property
|
|---|
| 59 | def text_seed(self) -> str:
|
|---|
| 60 | """Include answer options (and points) for deterministic ID generation."""
|
|---|
| 61 |
|
|---|
| 62 | joined = "|".join(
|
|---|
| 63 | f"{opt.text.strip()}:{opt.points}"
|
|---|
| 64 | for opt in sorted(self.options, key=lambda o: o.text.strip().lower())
|
|---|
| 65 | )
|
|---|
| 66 | return f"{self.text}{joined}"
|
|---|
| 67 |
|
|---|
| 68 | @classmethod
|
|---|
| 69 | def from_dict(cls, data):
|
|---|
| 70 | obj = cls(
|
|---|
| 71 | text=data.get("text", ""),
|
|---|
| 72 | options=[AnswerOption.from_dict(o) for o in data.get("options", [])],
|
|---|
| 73 | )
|
|---|
| 74 | # restore FlexoEntity core fields
|
|---|
| 75 | obj.domain = data.get("domain")
|
|---|
| 76 | obj.entity_type = EntityType[data.get("etype")] if "etype" in data else EntityType.ITEM
|
|---|
| 77 | obj.state = EntityState[data.get("state")] if "state" in data else EntityState.DRAFT
|
|---|
| 78 | if "flexo_id" in data:
|
|---|
| 79 | from flexoentity import FlexOID
|
|---|
| 80 | obj.flexo_id = FlexOID.parsed(data["flexo_id"])
|
|---|
| 81 | return obj
|
|---|
| 82 |
|
|---|
| 83 | @pytest.fixture
|
|---|
| 84 | def domain():
|
|---|
| 85 | return Domain.default()
|
|---|
| 86 |
|
|---|
| 87 | @pytest.fixture
|
|---|
| 88 | def sample_question():
|
|---|
| 89 | return SingleChoiceQuestion(domain=Domain.default(),
|
|---|
| 90 | text="What is 2 + 2?",
|
|---|
| 91 | options=[],
|
|---|
| 92 | entity_type=EntityType.ITEM,
|
|---|
| 93 | state=EntityState.DRAFT)
|
|---|