openbench-privacy-preservin.../tests/test_core.py

25 lines
1.1 KiB
Python

import os
from openbench_privacy_preserving_benchmarkin import KPIRecord, LocalStore, SecureAggregator, GrowthCalculator
def test_store_and_aggregate_simple():
path = "tests_data/kpi_records.jsonl"
if os.path.exists(path):
os.remove(path)
store = LocalStore(path=path)
r1 = KPIRecord(revenue=100.0, cogs=60.0, inventory_turns=3.0, lead_time=5.0, cac=20.0, ltv=120.0, region="NA", industry="Retail", anon_id="anon1", timestamp="2020-01-01T00:00:00Z")
r2 = KPIRecord(revenue=200.0, cogs=110.0, inventory_turns=4.0, lead_time=6.0, cac=30.0, ltv=240.0, region="NA", industry="Retail", anon_id="anon2", timestamp="2020-01-02T00:00:00Z")
store.add_kpi(r1)
store.add_kpi(r2)
all_recs = store.get_all()
assert len(all_recs) == 2
mean_rev = SecureAggregator.aggregate(all_recs, "revenue", anonymize=False)
assert mean_rev == (100.0 + 200.0) / 2
def test_roi_calc():
r = KPIRecord(revenue=100.0, cogs=50.0, inventory_turns=2.0, lead_time=3.0, cac=10.0, ltv=100.0)
roI = GrowthCalculator.roi(r)
assert roI == (100.0 - 10.0) / 10.0