Add MCP nutrition ingredient import and classification

This commit is contained in:
Joren de Graaf
2026-11-02 11:40:12 +01:00
parent 2f84a9f359
commit 4497771b21
9 changed files with 500 additions and 1 deletions
+39
View File
@@ -99,6 +99,45 @@ class McpHttpTests(LiveServerTestCase):
async with Client(streamable_http_client(self.mcp_url, http_client=http)) as client:
return await operation(client)
def test_nutrition_tools_discover_and_preview(self):
import hashlib
payload = {'source': {'programme': 'Body Support', 'document_name': 'sample.pdf',
'sha256': hashlib.sha256(b'sample').hexdigest()},
'ingredients': [{'row_key': 'p1-1', 'original_label': 'Broccoli vers',
'canonical_name': 'broccoli', 'quantity': '250', 'unit': 'g',
'page': 1, 'preparation': 'raw', 'category': 'vegetable'}]}
async def check(client):
names = {tool.name for tool in (await client.list_tools()).tools}
self.assertTrue({'nutrition_import_preview', 'nutrition_import_commit',
'nutrition_ingredients_search', 'nutrition_ingredient_correct'} <= names)
result = (await client.call_tool('nutrition_import_preview', {'payload': payload})).structured_content
self.assertEqual(result['blocking'], [])
self.assertEqual(result['ingredients'][0]['category'], 'vegetable')
self.assertFalse(result['existing_source_id'])
committed = (await client.call_tool('nutrition_import_commit', {
'payload': payload, 'confirmation': result['confirmation'], 'idempotency_key': 'nutrition-mcp-1'})).structured_content
self.assertEqual(committed['created'], 1)
repeated = (await client.call_tool('nutrition_import_commit', {
'payload': payload, 'confirmation': result['confirmation'], 'idempotency_key': 'nutrition-mcp-1'})).structured_content
self.assertTrue(repeated['idempotent'])
found = (await client.call_tool('nutrition_ingredients_search', {
'category': 'vegetable', 'programme': 'Body Support'})).structured_content
self.assertEqual(found['count'], 1)
self.assertEqual(found['results'][0]['sources'][0]['page'], 1)
corrected = (await client.call_tool('nutrition_ingredient_correct', {
'ingredient_id': found['results'][0]['id'], 'changes': {'subcategory': 'cruciferous'}})).structured_content
self.assertTrue(corrected['verified'])
asyncio.run(self._client(self.secret, check))
async def isolated(client):
found = (await client.call_tool('nutrition_ingredients_search', {'category': 'vegetable'})).structured_content
self.assertEqual(found['count'], 0)
denied = await client.call_tool('nutrition_import_preview', {'payload': payload})
self.assertTrue(denied.is_error)
asyncio.run(self._client(self.b_secret, isolated))
@override_settings(SECURE_SSL_REDIRECT=True)
def test_authentication_and_scope_boundaries(self):
async def check():