File size: 1,569 Bytes
35e80ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import sys
from pathlib import Path

import jsonschema
import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools"))
from export_schema import DOCS, render

from ai_coach.input_builder import build_coach_input
from ai_coach.schemas import CoachInput, CoachNote

from .helpers import example_names, good_note, load_example, make_input


def test_exported_schemas_are_up_to_date():
    """If this fails run: python tools/export_schema.py  (the ML engineer reads these files)."""
    assert (DOCS / "coach_input.schema.json").read_text(encoding="utf-8") == render(CoachInput)
    assert (DOCS / "coach_note.schema.json").read_text(encoding="utf-8") == render(CoachNote)


@pytest.mark.parametrize("name", example_names())
def test_compact_inputs_validate_against_the_published_schema(name):
    schema = json.loads((DOCS / "coach_input.schema.json").read_text(encoding="utf-8"))
    data = json.loads(build_coach_input(load_example(name)).model_dump_json())
    jsonschema.validate(data, schema)


def test_schema_rejects_unknown_fields():
    schema = json.loads((DOCS / "coach_input.schema.json").read_text(encoding="utf-8"))
    data = json.loads(make_input().model_dump_json())
    data["video_url"] = "https://example.com/x.mp4"
    with pytest.raises(jsonschema.ValidationError):
        jsonschema.validate(data, schema)


def test_note_schema_accepts_a_good_note():
    schema = json.loads((DOCS / "coach_note.schema.json").read_text(encoding="utf-8"))
    jsonschema.validate(json.loads(good_note().model_dump_json()), schema)