File size: 1,615 Bytes
55626ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

from app import asgi
from app.config import config


def _post(path, content):
    with (
        patch.dict(config.app, {"api_key": ""}),
        patch("app.controllers.v1.video.task_manager.add_task") as schedule,
        patch("app.controllers.v1.llm.llm.generate_script") as generate,
        TestClient(asgi.get_application(), raise_server_exceptions=False) as client,
    ):
        response = client.post(
            path, content=content, headers={"content-type": "application/json"}
        )
    schedule.assert_not_called()
    generate.assert_not_called()
    return response


@pytest.mark.parametrize(
    "content, expected",
    [
        (
            '{"video_subject": "sea", "paragraph_number": 0}',
            "body.paragraph_number: Input should be greater than or equal to 1",
        ),
        ('{"video_subject": ', "body.18: JSON decode error"),
    ],
)
def test_validation_message_describes_the_rejected_field(content, expected):
    # Out-of-range values and malformed JSON used to be reported as
    # "field required", pointing clients at a field that was present.
    response = _post("/api/v1/scripts", content)

    assert response.status_code == 400
    body = response.json()
    assert body["message"] == expected
    assert body["data"][0]["msg"] in expected


def test_missing_field_message_still_says_field_required():
    response = _post("/api/v1/videos", "{}")

    assert response.status_code == 400
    assert response.json()["message"] == "body.video_subject: Field required"