idea151-aidmesh-federated-p.../tests/test_models.py

43 lines
1.3 KiB
Python

from __future__ import annotations
import pytest
from datetime import datetime
from idea151_aidmesh_federated_privacy import models
def test_models_to_dict_and_immutability():
lp = models.LocalProblem(
id="lp-1",
domain="shelter",
assets={"tents": 100},
objectives=["max_coverage"],
constraints={"max_deploy": 50},
)
sd = models.SharedSignals(
forecast={"demand": 120}, capacity_proxies={"depotA": 80}, privacy_budget=1.0, version=1
)
now = datetime.utcnow()
pd = models.PlanDelta(
delta={"alloc": {"depotA": 40}},
timestamp=now,
author="planner-1",
contract_id="c-123",
signature="sig",
)
al = models.AuditLog(entry="created", signer="alice", timestamp=now, contract_id="c-123", version=1)
# to_dict should produce serializable dicts and timestamp should be ISO string
assert lp.to_dict()["id"] == "lp-1"
assert sd.to_dict()["version"] == 1
assert isinstance(pd.to_dict()["timestamp"], str) and "T" in pd.to_dict()["timestamp"]
assert isinstance(al.to_dict()["timestamp"], str) and "T" in al.to_dict()["timestamp"]
# dataclasses are frozen: setting an attribute should raise
with pytest.raises(Exception):
lp.id = "other"