opengrowth-privacy-preservi.../tests/test_mvp.py

51 lines
1.7 KiB
Python

from opengrowth_privacy_preserving_federated_ import (
SchemaRegistry,
ExperimentTemplate,
SecureAggregator,
CloudLedger,
AccessControl,
Governance,
GA4Adapter,
SegmentAdapter,
)
from opengrowth_privacy_preserving_federated_ import schema_registry as _unused # type: ignore
def test_schema_and_templates_basic():
reg = SchemaRegistry()
reg.register_schema("Experiment", {"type": "object"})
assert reg.get_schema("Experiment") == {"type": "object"}
tmpl = ExperimentTemplate("pricing_v1", "Pricing Experiment v1", {"type": "pricing"})
reg.register_template(tmpl.template_id, tmpl.definition)
assert reg.get_template("pricing_v1") == {"type": "pricing"}
def test_adapters_and_aggregation_and_ledger():
ga4 = GA4Adapter()
seg = SegmentAdapter()
local1 = ga4.fill({"activation_rate": 0.25, "funnel_dropoff": 0.4, "time_to_value": 12, "cac": 300, "ltv": 1000})
local2 = seg.fill({"activation_rate": 0.3, "funnel_dropoff": 0.35, "time_to_value": 10, "cac": 320, "ltv": 1200})
results = [local1, local2]
aggregated = SecureAggregator.aggregate(results)
assert "activation_rate" in aggregated
assert "mean" in aggregated["activation_rate"]
anchor = CloudLedger.anchor({"template": "pricing_v1", "aggregated": aggregated})
assert isinstance(anchor, str)
latest = CloudLedger.latest()
assert latest["anchor_id"] == anchor
def test_governance_basic():
ac = AccessControl()
ac.grant("alice", "admin")
assert ac.has_role("alice", "admin")
gov = Governance()
gov.register_policy("template_access", {"roles": ["admin", "viewer"]})
policy = gov.get_policy("template_access")
assert policy["roles"] == ["admin", "viewer"]