Changeset ef964d8 in flexoentity for tests/conftest.py


Ignore:
Timestamp:
11/27/25 18:12:23 (7 weeks ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
master
Children:
4e11d58
Parents:
9a50e0b
Message:

new serialization structure adopted and tests fixed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/conftest.py

    r9a50e0b ref964d8  
    1 # tests/stubs/single_choice_question.py
    2 from dataclasses import dataclass, field
    31import pytest
    42import platform
    53from pathlib import Path
    64from datetime import datetime
    7 from typing import List
    8 from flexoentity import FlexOID, FlexoEntity, EntityType, EntityState, Domain, get_signing_backend, CertificateReference
     5from flexoentity import Domain, FlexoSignature
     6from flexoentity import get_signing_backend, CertificateReference
    97
    108
     
    1816    return FixedDate
    1917
    20 @dataclass
    21 class AnswerOption:
    22     id: str
    23     text: str
    24     points: float = 0.0
    25 
    26     def to_dict(self):
    27         return {"id": self.id, "text": self.text, "points": self.points}
    28 
    29     @classmethod
    30     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 @dataclass
    39 class SingleChoiceQuestion(FlexoEntity):
    40     """A minimal stub to test FlexoEntity integration."""
    41     ENTITY_TYPE = EntityType.ITEM
    42 
    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     @classmethod
    58     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     @property
    68     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     @classmethod
    78     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 fields
    83         if "flexo_id" in data:
    84             obj.flexo_id = FlexOID.to_dict(data["flexo_id"])
    85         return obj
    8618
    8719@pytest.fixture
     
    9224                                 description="ALL ABOUT ARITHMETIC IN PYTHON")
    9325
    94 @pytest.fixture
    95 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 q
    10126
    10227SYSTEM = platform.system()
     
    181106    except Exception as e:
    182107        pytest.skip(f"Backend unavailable or misconfigured: {e}")
     108
     109@pytest.fixture
     110def 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.