Spaces:
Sleeping
Sleeping
File size: 5,085 Bytes
09da05c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | """
Tests for persona schema models and parse helpers.
"""
import json
from pathlib import Path
import pytest
from adsp.data_pipeline.persona_data_pipeline.parse import (
load_individual_personas,
load_personas_bundle,
)
from adsp.data_pipeline.schema import (
ContentFilters,
Indicator,
Influences,
Metric,
PersonaProfileModel,
ReasoningPolicies,
Salience,
Source,
Statement,
StyleProfile,
ValueFrame,
load_persona_profile,
)
def test_source_defaults_and_extra_fields():
source = Source(extra_field="value")
assert source.doc_id is None
assert source.pages == []
assert source.extra_field == "value"
def test_metric_defaults_and_extra_fields():
metric = Metric(extra_field="value")
assert metric.value is None
assert metric.unit is None
assert metric.description is None
assert metric.extra_field == "value"
def test_salience_defaults_and_extra_fields():
salience = Salience(extra_field=1)
assert salience.is_salient is None
assert salience.extra_field == 1
def test_influences_defaults_and_extra_fields():
influences = Influences(extra_field=True)
assert influences.tone is None
assert influences.extra_field is True
def test_statement_defaults_and_extra_fields():
statement = Statement(extra_field="value")
assert statement.metrics == []
assert statement.extra_field == "value"
def test_indicator_defaults_and_extra_fields():
indicator = Indicator(extra_field="value")
assert indicator.sources == []
assert indicator.statements == []
assert indicator.extra_field == "value"
def test_style_profile_defaults_and_extra_fields():
style = StyleProfile(extra_field="value")
assert style.tone_adjectives == []
assert style.extra_field == "value"
def test_value_frame_defaults_and_extra_fields():
frame = ValueFrame(extra_field="value")
assert frame.priority_rank == []
assert frame.extra_field == "value"
def test_reasoning_policies_defaults_and_extra_fields():
policies = ReasoningPolicies(extra_field="value")
assert policies.purchase_advice is None
assert policies.extra_field == "value"
def test_content_filters_defaults_and_extra_fields():
filters = ContentFilters(extra_field="value")
assert filters.avoid_styles == []
assert filters.extra_field == "value"
def test_persona_profile_defaults():
profile = PersonaProfileModel()
assert profile.indicators == []
assert profile.key_indicators == []
assert profile.source_pages == []
def test_persona_profile_nested_objects():
profile = PersonaProfileModel(
persona_id="p1",
indicators=[
Indicator(
label="Price",
statements=[
Statement(
label="Budget",
metrics=[Metric(value=10, unit="$")],
)
],
)
],
)
assert profile.indicators[0].label == "Price"
assert profile.indicators[0].statements[0].metrics[0].unit == "$"
def test_load_persona_profile_reads_file(tmp_path: Path):
payload = {"persona_id": "p1", "persona_name": "Alpha"}
path = tmp_path / "persona.json"
path.write_text(json.dumps(payload), encoding="utf-8")
profile = load_persona_profile(path)
assert profile.persona_id == "p1"
assert profile.persona_name == "Alpha"
def test_load_persona_profile_missing_file_raises(tmp_path: Path):
missing_path = tmp_path / "missing.json"
with pytest.raises(FileNotFoundError):
load_persona_profile(missing_path)
def test_load_personas_bundle_from_dict(tmp_path: Path):
payload = {
"personas": [
{"persona_id": "p1", "persona_name": "One"},
{"persona_id": "p2", "persona_name": "Two"},
]
}
path = tmp_path / "bundle.json"
path.write_text(json.dumps(payload), encoding="utf-8")
personas = load_personas_bundle(path)
assert len(personas) == 2
assert personas[0].persona_id == "p1"
assert personas[1].persona_name == "Two"
def test_load_personas_bundle_handles_missing_list(tmp_path: Path):
payload = {"not_personas": []}
path = tmp_path / "bundle.json"
path.write_text(json.dumps(payload), encoding="utf-8")
personas = load_personas_bundle(path)
assert personas == []
def test_load_individual_personas_from_directory(tmp_path: Path):
payload = {"persona_id": "p1", "persona_name": "One"}
path = tmp_path / "p1.json"
path.write_text(json.dumps(payload), encoding="utf-8")
results = load_individual_personas(tmp_path)
assert list(results.keys()) == ["p1"]
assert results["p1"].persona_name == "One"
def test_load_individual_personas_sets_persona_id_from_filename(tmp_path: Path):
payload = {"persona_name": "Anon"}
path = tmp_path / "anon.json"
path.write_text(json.dumps(payload), encoding="utf-8")
results = load_individual_personas(tmp_path)
assert "anon" in results
assert results["anon"].persona_id == "anon"
|