-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_endpoint_parse.py
More file actions
110 lines (88 loc) · 4.37 KB
/
Copy pathtest_endpoint_parse.py
File metadata and controls
110 lines (88 loc) · 4.37 KB
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
import json
from main import CANNED_INGREDIENTS, LOGOS_BASE_URL, LOGOS_MODEL, NO_LLM_NOTE, OPENAI_MODEL
from tests.conftest import _fake_response
def _set_llm_content(mock_openai_client, content):
mock_openai_client.return_value.chat.completions.create.return_value = (
_fake_response(content)
)
def test_happy_path_returns_ingredients(client, mock_openai_client, sample_ingredient_json):
_set_llm_content(mock_openai_client, sample_ingredient_json)
response = client.post("/api/ai/parse", json={"dish": "Pancakes"})
assert response.status_code == 200
body = response.json()
assert body["dish"] == "Pancakes"
assert len(body["ingredients"]) == 2
def test_restricted_and_alternative_fields_roundtrip(
client, mock_openai_client, sample_ingredient_json
):
_set_llm_content(mock_openai_client, sample_ingredient_json)
response = client.post("/api/ai/parse", json={"dish": "Pancakes"})
milk = next(i for i in response.json()["ingredients"] if i["name"] == "milk")
assert milk["restricted"] is True
assert milk["alternative"] == "oat milk"
def test_malformed_llm_json_falls_back_to_canned_response(client, mock_openai_client):
_set_llm_content(mock_openai_client, "not json at all")
response = client.post("/api/ai/parse", json={"dish": "Pancakes"})
assert response.status_code == 200
body = response.json()
assert body["note"] == NO_LLM_NOTE
assert len(body["ingredients"]) == len(CANNED_INGREDIENTS)
def test_missing_ingredients_key_falls_back_to_canned_response(client, mock_openai_client):
_set_llm_content(mock_openai_client, json.dumps({"foo": "bar"}))
response = client.post("/api/ai/parse", json={"dish": "Pancakes"})
assert response.status_code == 200
body = response.json()
assert body["note"] == NO_LLM_NOTE
assert len(body["ingredients"]) == len(CANNED_INGREDIENTS)
def test_openai_error_falls_back_to_canned_response(client, mock_openai_client):
from openai import OpenAIError
mock_openai_client.return_value.chat.completions.create.side_effect = OpenAIError(
"boom"
)
response = client.post(
"/api/ai/parse", json={"dish": "Pancakes", "llm_provider": "openai"}
)
assert response.status_code == 200
body = response.json()
assert body["note"] == NO_LLM_NOTE
assert len(body["ingredients"]) == len(CANNED_INGREDIENTS)
def test_missing_openai_key_falls_back_to_logos(
client, mock_openai_client, sample_ingredient_json, monkeypatch
):
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
_set_llm_content(mock_openai_client, sample_ingredient_json)
response = client.post(
"/api/ai/parse", json={"dish": "Pancakes", "llm_provider": "openai"}
)
assert response.status_code == 200
_, kwargs = mock_openai_client.return_value.chat.completions.create.call_args
assert kwargs["model"] == LOGOS_MODEL
_, client_kwargs = mock_openai_client.call_args
assert client_kwargs["base_url"] == LOGOS_BASE_URL
def test_default_provider_is_logos(client, mock_openai_client, sample_ingredient_json):
_set_llm_content(mock_openai_client, sample_ingredient_json)
client.post("/api/ai/parse", json={"dish": "Pancakes"})
_, kwargs = mock_openai_client.return_value.chat.completions.create.call_args
assert kwargs["model"] == LOGOS_MODEL
assert "response_format" not in kwargs
_, client_kwargs = mock_openai_client.call_args
assert client_kwargs["base_url"] == LOGOS_BASE_URL
def test_openai_provider_selected(client, mock_openai_client, sample_ingredient_json):
_set_llm_content(mock_openai_client, sample_ingredient_json)
client.post(
"/api/ai/parse", json={"dish": "Pancakes", "llm_provider": "openai"}
)
_, kwargs = mock_openai_client.return_value.chat.completions.create.call_args
assert kwargs["model"] == OPENAI_MODEL
assert kwargs["response_format"] == {"type": "json_object"}
def test_dietary_restrictions_passed_into_system_prompt(
client, mock_openai_client, sample_ingredient_json
):
_set_llm_content(mock_openai_client, sample_ingredient_json)
client.post(
"/api/ai/parse",
json={"dish": "Pancakes", "dietary_restrictions": ["Vegan"]},
)
_, kwargs = mock_openai_client.return_value.chat.completions.create.call_args
system_message = next(m for m in kwargs["messages"] if m["role"] == "system")
assert "animal product" in system_message["content"]