From eeeb1c5b34c058f2c1a7ea2f3c6b179f503d2aa4 Mon Sep 17 00:00:00 2001 From: Tom Flux Date: Tue, 23 Jun 2026 21:24:09 +0100 Subject: Fix bulk-pantry-add crash on null unit (found in live MCP testing) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit End-to-end testing (MCP client -> FastMCP -> Django) surfaced a 500: adding an item with an explicit `unit: null` (as add_to_pantry sends for items without a unit) hit a NOT NULL violation, because `item_data.get("unit", "items")` returns None when the key is present. - views.bulk_pantry_add: `item_data.get("unit") or "items"` — tolerate null/empty unit. - mcp_server add_to_pantry: omit quantity/unit from the payload when unset, so the API applies its own defaults. - test: bulk-add with unit/quantity = null returns 201 (25 pass). Co-Authored-By: Claude Opus 4.8 --- kitchen/tests.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'kitchen/tests.py') diff --git a/kitchen/tests.py b/kitchen/tests.py index 9ce9098..f3f68e2 100644 --- a/kitchen/tests.py +++ b/kitchen/tests.py @@ -224,6 +224,19 @@ class BulkAddApiTests(_ApiTestCase): self.assertEqual(rows.count(), 1) self.assertEqual(rows.first().state, "in") + def test_tolerates_null_unit_and_quantity(self): + # An MCP client may send unit/quantity as null for unknown items. + r = self.client.post( + "/api/bulk-pantry-add/", + {"items": [{"ingredient_name": "pork mince", "location": "fridge", + "unit": None, "quantity": None}]}, + format="json", + ) + self.assertEqual(r.status_code, 201) + self.assertTrue( + PantryItem.objects.filter(ingredient__name="pork mince", location="fridge").exists() + ) + class WhatCanICookApiTests(_ApiTestCase): def setUp(self): -- cgit v1.2.3