Changeset ef964d8 in flexoentity for tests/conftest.py
- Timestamp:
- 11/27/25 18:12:23 (7 weeks ago)
- Branches:
- master
- Children:
- 4e11d58
- Parents:
- 9a50e0b
- File:
-
- 1 edited
-
tests/conftest.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tests/conftest.py
r9a50e0b ref964d8 1 # tests/stubs/single_choice_question.py2 from dataclasses import dataclass, field3 1 import pytest 4 2 import platform 5 3 from pathlib import Path 6 4 from datetime import datetime 7 from typing import List8 from flexoentity import FlexOID, FlexoEntity, EntityType, EntityState, Domain,get_signing_backend, CertificateReference5 from flexoentity import Domain, FlexoSignature 6 from flexoentity import get_signing_backend, CertificateReference 9 7 10 8 … … 18 16 return FixedDate 19 17 20 @dataclass21 class AnswerOption:22 id: str23 text: str24 points: float = 0.025 26 def to_dict(self):27 return {"id": self.id, "text": self.text, "points": self.points}28 29 @classmethod30 def from_dict(cls, data):31 return cls(32 id=data.get("id", ""),33 text=data.get("text", ""),34 points=data.get("points", 0.0)35 )36 37 38 @dataclass39 class SingleChoiceQuestion(FlexoEntity):40 """A minimal stub to test FlexoEntity integration."""41 ENTITY_TYPE = EntityType.ITEM42 43 text: str = ""44 options: List[AnswerOption] = field(default_factory=list)45 46 def __post_init__(self):47 # If no FlexOID yet, generate a draft ID now.48 if not getattr(self, "flexo_id", None):49 self.flexo_id = FlexOID.safe_generate(50 domain_id=self.domain_id,51 entity_type=SingleChoiceQuestion.ENTITY_TYPE.value, # 'I'52 state=EntityState.DRAFT.value, # 'D'53 text=self.text_seed or self.text,54 version=1,55 )56 57 @classmethod58 def default(cls):59 return cls()60 61 def _serialize_content(self):62 return {63 "text": self.text,64 "options": [opt.to_dict() for opt in self.options],65 }66 67 @property68 def text_seed(self) -> str:69 """Include answer options (and points) for deterministic ID generation."""70 71 joined = "|".join(72 f"{opt.text.strip()}:{opt.points}"73 for opt in sorted(self.options, key=lambda o: o.text.strip().lower())74 )75 return f"{self.text}{joined}"76 77 @classmethod78 def from_dict(cls, data):79 obj = cls(text=data.get("text", ""),80 options=[AnswerOption.from_dict(o) for o in data.get("options", [])],81 )82 # restore FlexoEntity core fields83 if "flexo_id" in data:84 obj.flexo_id = FlexOID.to_dict(data["flexo_id"])85 return obj86 18 87 19 @pytest.fixture … … 92 24 description="ALL ABOUT ARITHMETIC IN PYTHON") 93 25 94 @pytest.fixture95 def sample_question(sample_domain):96 q = SingleChoiceQuestion.with_domain_id(domain_id=sample_domain.domain_id,97 text="What is 2 + 2?",98 options=[])99 q._update_fingerprint()100 return q101 26 102 27 SYSTEM = platform.system() … … 181 106 except Exception as e: 182 107 pytest.skip(f"Backend unavailable or misconfigured: {e}") 108 109 @pytest.fixture 110 def sample_signature(sample_domain, cert_ref_linux): 111 return FlexoSignature.with_domain_id(domain_id="SIG", signed_entity=sample_domain, 112 certificate_reference=cert_ref_linux, 113 comment="This is a mock signature") 114
Note:
See TracChangeset
for help on using the changeset viewer.
