recipe links
This commit is contained in:
+3
-2
@@ -751,8 +751,9 @@ def _inventory_validation(field, reason):
|
||||
|
||||
def _inventory_batch_input(request, data, *, deleting=False):
|
||||
rows = data.get('item_ids' if deleting else 'items')
|
||||
if not isinstance(rows, list) or not 1 <= len(rows) <= 20:
|
||||
raise ValueError('Provide between 1 and 20 inventory items.')
|
||||
limit = 20 if deleting else 50
|
||||
if not isinstance(rows, list) or not 1 <= len(rows) <= limit:
|
||||
raise ValueError(f'Provide between 1 and {limit} inventory items.')
|
||||
if set(data) - ({'item_ids', 'dry_run', 'atomic', 'confirm'} if deleting else {'items', 'dry_run', 'atomic'}):
|
||||
raise ValueError('Unknown bulk inventory fields.')
|
||||
if any(type(data.get(key, default)) is not bool for key, default in (('dry_run', False), ('atomic', False))):
|
||||
|
||||
@@ -42,7 +42,11 @@ urlpatterns = [
|
||||
path('recipes/cook-later/', recipe_api.recipe_cook_later_collection),
|
||||
path('recipes/cook-later/<uuid:identifier>/', recipe_api.recipe_cook_later_item),
|
||||
path('recipes/create/', recipe_api.recipe_create),
|
||||
path('recipes/compositions/preview/', recipe_api.recipe_composition_preview),
|
||||
path('recipes/compositions/', recipe_api.recipe_composition_create),
|
||||
path('recipes/<uuid:identifier>/', recipe_api.recipe_item),
|
||||
path('recipes/<uuid:identifier>/links/', recipe_api.recipe_links),
|
||||
path('recipes/<uuid:identifier>/links/<uuid:link_id>/', recipe_api.recipe_link_item),
|
||||
path('recipes/<uuid:identifier>/variations/', recipe_api.recipe_variation),
|
||||
path('recipes/<uuid:identifier>/ingredients/', recipe_api.recipe_ingredient_create),
|
||||
path('recipes/<uuid:identifier>/ingredients/<int:item_id>/', recipe_api.recipe_ingredient_item),
|
||||
|
||||
+21
-6
@@ -112,7 +112,7 @@ class FoodApiTests(TestCase):
|
||||
path = '/api/v1/kitchen/bulk-update/'
|
||||
row = {'item_id': str(item.api_id), 'changes': {'notes': 'new'}}
|
||||
self.assertEqual(self.call('post', path, {'items': [row, row], 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='dup').status_code, 400)
|
||||
self.assertEqual(self.call('post', path, {'items': [row] * 21, 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='large').status_code, 400)
|
||||
self.assertEqual(self.call('post', path, {'items': [row] * 51, 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='large').status_code, 400)
|
||||
self.assertEqual(self.call('post', path, {'items': [row], 'dry_run': False}).json()['error']['code'], 'idempotency_key_required')
|
||||
result = self.call('post', path, {'items': [{**row, 'expected_updated_at': '2000-01-01T00:00:00+00:00'}],
|
||||
'dry_run': False}, HTTP_IDEMPOTENCY_KEY='stale')
|
||||
@@ -120,16 +120,31 @@ class FoodApiTests(TestCase):
|
||||
item.refresh_from_db()
|
||||
self.assertEqual(item.notes, '')
|
||||
|
||||
def test_bulk_inventory_accepts_twenty_updates_in_one_request(self):
|
||||
def test_bulk_inventory_accepts_twenty_three_updates_in_one_request(self):
|
||||
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
|
||||
items = [KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location)
|
||||
for _ in range(20)]
|
||||
for _ in range(23)]
|
||||
payload = {'items': [{'item_id': str(item.api_id), 'changes': {'notes': 'Checked'}} for item in items],
|
||||
'dry_run': False}
|
||||
result = self.call('post', '/api/v1/kitchen/bulk-update/', payload, HTTP_IDEMPOTENCY_KEY='twenty-items')
|
||||
path = '/api/v1/kitchen/bulk-update/'
|
||||
preview = self.call('post', path, {**payload, 'dry_run': True})
|
||||
self.assertEqual(preview.json()['counts']['validated'], 23)
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 0)
|
||||
result = self.call('post', path, payload, HTTP_IDEMPOTENCY_KEY='twenty-three-items')
|
||||
self.assertEqual(result.status_code, 200, result.content)
|
||||
self.assertEqual(result.json()['counts']['changed'], 20)
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 20)
|
||||
self.assertEqual(result.json()['counts']['changed'], 23)
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 23)
|
||||
self.assertEqual(self.call('post', path, payload, HTTP_IDEMPOTENCY_KEY='twenty-three-items').json(), result.json())
|
||||
|
||||
def test_bulk_inventory_rejects_more_than_fifty_distinct_items(self):
|
||||
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
|
||||
items = [KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location)
|
||||
for _ in range(51)]
|
||||
payload = {'items': [{'item_id': str(item.api_id), 'changes': {'notes': 'Checked'}} for item in items],
|
||||
'dry_run': False, 'atomic': True}
|
||||
result = self.call('post', '/api/v1/kitchen/bulk-update/', payload, HTTP_IDEMPOTENCY_KEY='over-limit')
|
||||
self.assertEqual(result.status_code, 400)
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(user=self.a, notes='Checked').count(), 0)
|
||||
|
||||
def test_bulk_inventory_delete_preview_confirm_and_history(self):
|
||||
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
|
||||
|
||||
Reference in New Issue
Block a user