Spaces:
Running on T4
Running on T4
Download AI-coach-agent/tests/test_swing_report_validation.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 12.5 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_swing_report_validation.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/tests/test_swing_report_validation.py
-
curl -L -o test_swing_report_validation.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_swing_report_validation.py
12.5 kB
| """Contract 1.0 receiver validation: every runtime rule (1 to 11) and the structural constraints.""" | |
| from __future__ import annotations | |
| import copy | |
| import math | |
| import pytest | |
| from ai_coach.swing_report.validation import validate_payload | |
| def payload() -> dict: | |
| """The ML engineer's contract example (right-handed, contact unverified, stride limited).""" | |
| return copy.deepcopy({ | |
| "schema_version": "1.0", "analysis_id": "analysis_001", "analysis_revision": 1, "swing_id": "swing_001", | |
| "status": "completed", | |
| "athlete": {"athlete_id": "player_001", "batting_hand": "right", "lead_foot": "left", "age_years": None, | |
| "height_cm": None}, | |
| "context": {"sport": "baseball", "practice_type": "tee"}, | |
| "measurement_definitions_version": "1.0", | |
| "events": { | |
| "stride_start": {"video_time_s": 1.0, "capture_time_s": 1.0, "status": "available", "source": "manual_review"}, | |
| "front_foot_down": {"video_time_s": 1.2, "capture_time_s": 1.2, "status": "available", | |
| "source": "manual_review"}, | |
| "contact": {"video_time_s": None, "capture_time_s": None, "status": "unavailable", "source": None, | |
| "reasons": ["contact_not_verified"]}, | |
| }, | |
| "measurements": [ | |
| {"metric_id": "hip_shoulder_separation_at_foot_down", "group": "stride", "value": 3, "unit": "deg", | |
| "event_id": "front_foot_down", "reference": "signed_horizontal_shoulder_minus_hip_heading", | |
| "status": "available", "confidence": None, "uses_interpolated_input": False, "reasons": [], "benchmark": None}, | |
| {"metric_id": "stride_relative_length", "group": "stride", "value": 24, "unit": "cm", | |
| "start_event_id": "stride_start", "end_event_id": "front_foot_down", | |
| "reference": "lead_ankle_displacement_relative_to_rear_ankle", "status": "limited", "confidence": None, | |
| "uses_interpolated_input": False, "reasons": ["scale_is_model_estimated"], "benchmark": None}, | |
| {"metric_id": "hip_shoulder_separation_at_contact", "group": "contact_follow_through", "value": None, | |
| "unit": "deg", "event_id": "contact", "reference": "signed_horizontal_shoulder_minus_hip_heading", | |
| "status": "unavailable", "confidence": None, "uses_interpolated_input": False, | |
| "reasons": ["contact_not_verified"], "benchmark": None}, | |
| ], | |
| }) | |
| def with_contact(p: dict, time_s: float = 1.4, source: str = "manual_review") -> dict: | |
| p["events"]["contact"] = {"video_time_s": time_s, "capture_time_s": time_s, "status": "available", "source": source} | |
| p["measurements"][2].update(value=-2.0, status="available", reasons=[]) | |
| return p | |
| def problems(p) -> dict[str, str]: | |
| return validate_payload(p)[1] | |
| def test_contract_example_is_valid(): | |
| parsed, found = validate_payload(payload()) | |
| assert found == {} | |
| assert parsed is not None and parsed.analysis_revision == 1 | |
| def test_not_an_object(): | |
| assert problems([1, 2]) == {"payload": "Must be a JSON object."} | |
| # Rule 1 ------------------------------------------------------------------------------------------ | |
| def test_rule1_versions(field): | |
| p = payload() | |
| p[field] = "2.0" | |
| found = problems(p) | |
| assert list(found) == [field] | |
| assert "1.0" in found[field] | |
| # Rule 2 ------------------------------------------------------------------------------------------ | |
| def test_rule2_numbers_and_ids(mutate, path): | |
| p = payload() | |
| mutate(p) | |
| assert path in problems(p) | |
| def test_rule2_values_accepted(): | |
| p = payload() | |
| p["athlete"].update(age_years=15, height_cm=168.5) | |
| p["measurements"][0]["confidence"] = 0.8 | |
| assert problems(p) == {} | |
| def test_nullable_fields_must_be_present_and_unknown_fields_rejected(): | |
| p = payload() | |
| del p["athlete"]["height_cm"] | |
| p["extra"] = 1 | |
| found = problems(p) | |
| assert "athlete.height_cm" in found and "extra" in found | |
| # Rule 3 ------------------------------------------------------------------------------------------ | |
| def test_rule3_all_event_keys_required(): | |
| p = payload() | |
| del p["events"]["contact"] | |
| assert "events.contact" in problems(p) | |
| def test_rule3_one_entry_per_metric(): | |
| p = payload() | |
| p["measurements"][1] = copy.deepcopy(p["measurements"][0]) | |
| found = problems(p) | |
| assert "measurements[1].metric_id" in found | |
| assert "stride_relative_length" in found["measurements"] | |
| def test_rule3_unregistered_metric(): | |
| p = payload() | |
| p["measurements"][0]["metric_id"] = "bat_speed" | |
| assert "measurements[0].metric_id" in problems(p) | |
| # Rule 4 ------------------------------------------------------------------------------------------ | |
| def test_rule4_metric_specification(i, field, value): | |
| p = payload() | |
| p["measurements"][i][field] = value | |
| assert f"measurements[{i}].{field}" in problems(p) | |
| def test_rule4_point_measurement_cannot_carry_interval(): | |
| p = payload() | |
| p["measurements"][0]["start_event_id"] = "stride_start" | |
| assert problems(p) == {"measurements[0].start_event_id": "A point measurement cannot also carry an interval."} | |
| def test_rule4_interval_measurement_cannot_carry_point_event(): | |
| p = payload() | |
| p["measurements"][1]["event_id"] = None | |
| assert "measurements[1].event_id" in problems(p) | |
| # Rule 5 ------------------------------------------------------------------------------------------ | |
| def test_rule5_measurement_status_combinations(i, changes, path): | |
| p = payload() | |
| p["measurements"][i].update(changes) | |
| assert path in problems(p) | |
| def test_rule5_event_status_combinations(event, changes, path): | |
| p = payload() | |
| p["events"][event].update(changes) | |
| assert path in problems(p) | |
| # Rule 6 ------------------------------------------------------------------------------------------ | |
| def test_rule6_negative_times(): | |
| p = payload() | |
| p["events"]["stride_start"].update(video_time_s=-0.1, capture_time_s=-1.0) | |
| found = problems(p) | |
| assert "events.stride_start.video_time_s" in found and "events.stride_start.capture_time_s" in found | |
| def test_rule6_stride_start_before_landing(): | |
| p = payload() | |
| p["events"]["stride_start"]["video_time_s"] = 1.2 | |
| assert problems(p) == {"events.stride_start.video_time_s": "Must be before front_foot_down."} | |
| def test_rule6_landing_not_after_contact(): | |
| p = with_contact(payload(), time_s=1.1) | |
| assert problems(p) == {"events.front_foot_down.video_time_s": "Must be before contact when both are available."} | |
| assert problems(with_contact(payload(), time_s=1.4)) == {} | |
| # Rule 7 ------------------------------------------------------------------------------------------ | |
| def test_rule7_missing_event_makes_measurement_unavailable(): | |
| p = payload() | |
| p["events"]["front_foot_down"] = {"video_time_s": None, "capture_time_s": None, "status": "unavailable", | |
| "source": None, "reasons": ["event_not_detected"]} | |
| found = problems(p) | |
| assert "measurements[0].status" in found and "measurements[1].status" in found | |
| def test_rule7_limited_event_cannot_give_available_measurement(): | |
| p = payload() | |
| p["events"]["front_foot_down"].update(status="limited", reasons=["event_timing_uncertain"]) | |
| assert problems(p) == {"measurements[0].status": "Cannot be available because front_foot_down is limited."} | |
| p["measurements"][0].update(status="limited", reasons=["required_event_limited"]) | |
| assert problems(p) == {} | |
| # Rule 8 ------------------------------------------------------------------------------------------ | |
| def test_rule8_pose_estimate_cannot_establish_contact(): | |
| assert "events.contact.source" in problems(with_contact(payload(), source="pose_estimated")) | |
| assert problems(with_contact(payload(), source="bat_ball_detector")) == {} | |
| def test_rule8_detector_only_for_contact(): | |
| p = payload() | |
| p["events"]["front_foot_down"]["source"] = "bat_ball_detector" | |
| assert "events.front_foot_down.source" in problems(p) | |
| # Rule 9 ------------------------------------------------------------------------------------------ | |
| def test_rule9_unknown_scale_makes_cm_unavailable(): | |
| p = payload() | |
| p["measurements"][1]["reasons"] = ["scale_unknown"] | |
| assert "measurements[1].status" in problems(p) | |
| def test_rule9_model_estimated_scale_needs_limited(): | |
| p = payload() | |
| p["measurements"][1].update(status="unavailable", value=None) | |
| assert "measurements[1].status" in problems(p) | |
| def test_rule9_unknown_lead_foot(): | |
| p = payload() | |
| p["athlete"]["lead_foot"] = None | |
| found = problems(p) | |
| assert "measurements[1].status" in found and "measurements[1].reasons" in found | |
| p["measurements"][1].update(status="unavailable", value=None, reasons=["lead_foot_unknown"]) | |
| assert problems(p) == {} | |
| # Rule 10 ----------------------------------------------------------------------------------------- | |
| def test_rule10_ranges(i, value, ok): | |
| p = payload() | |
| p["measurements"][i]["value"] = value | |
| assert (problems(p) == {}) is ok | |
| # Rule 11 ----------------------------------------------------------------------------------------- | |
| def bench(**kw) -> dict: | |
| return {"benchmark_id": "b", "version": "1", "kind": "reference_interval", "lower": -20.0, "upper": -5.0, | |
| "unit": "deg", "source_reference": "s", **kw} | |
| def test_rule11_benchmark(b, path): | |
| p = payload() | |
| p["measurements"][0]["benchmark"] = b | |
| assert path in problems(p) | |
| def test_rule11_coherent_benchmark_accepted(): | |
| p = payload() | |
| p["measurements"][0]["benchmark"] = bench(upper=None) | |
| assert problems(p) == {} | |