latest changes
This commit is contained in:
@@ -39,6 +39,98 @@ class FoodApiTests(TestCase):
|
||||
data = json.dumps(data)
|
||||
return getattr(self.client, method)(path, data=data, **args)
|
||||
|
||||
def test_bulk_inventory_update_preview_partial_atomic_and_retry(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(2)]
|
||||
path = '/api/v1/kitchen/bulk-update/'
|
||||
rows = [{'item_id': str(item.api_id), 'changes': {'location': ' Metal shelf ', 'best_before': '2026-11-01'}}
|
||||
for item in items]
|
||||
preview = self.call('post', path, {'items': rows, 'dry_run': True})
|
||||
self.assertEqual(preview.status_code, 200, preview.content)
|
||||
self.assertEqual(preview.json()['counts']['validated'], 2)
|
||||
self.assertFalse(InventoryLocation.objects.filter(user=self.a, name='Metal shelf').exists())
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(pk__in=[item.pk for item in items], location=location).count(), 2)
|
||||
rows.append({'item_id': str(self.item_b.api_id), 'changes': {'notes': 'foreign'}})
|
||||
atomic = self.call('post', path, {'items': rows, 'dry_run': False, 'atomic': True}, HTTP_IDEMPOTENCY_KEY='bulk-atomic')
|
||||
self.assertEqual(atomic.json()['status'], 'rolled_back')
|
||||
self.assertEqual(atomic.json()['counts']['rolled_back'], 2)
|
||||
self.assertFalse(InventoryLocation.objects.filter(user=self.a, name='Metal shelf').exists())
|
||||
partial_payload = {'items': rows, 'dry_run': False}
|
||||
partial = self.call('post', path, partial_payload, HTTP_IDEMPOTENCY_KEY='bulk-partial')
|
||||
self.assertEqual(partial.json()['counts']['changed'], 2)
|
||||
self.assertEqual(partial.json()['counts']['failed'], 1)
|
||||
self.assertEqual(self.call('post', path, partial_payload, HTTP_IDEMPOTENCY_KEY='bulk-partial').json(), partial.json())
|
||||
self.assertEqual(self.call('post', path, {**partial_payload, 'atomic': True}, HTTP_IDEMPOTENCY_KEY='bulk-partial').status_code, 409)
|
||||
self.assertEqual(KitchenInventoryItem.objects.filter(pk__in=[item.pk for item in items], location__name='Metal shelf').count(), 2)
|
||||
self.assertEqual(KitchenInventoryItem.objects.get(pk=self.item_b.pk).notes, '')
|
||||
|
||||
def test_bulk_inventory_rejects_duplicates_limits_and_stale_timestamp(self):
|
||||
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
|
||||
item = KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location)
|
||||
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], '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')
|
||||
self.assertEqual(result.json()['items'][0]['error']['code'], 'inventory_conflict')
|
||||
item.refresh_from_db()
|
||||
self.assertEqual(item.notes, '')
|
||||
|
||||
def test_bulk_inventory_accepts_twenty_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)]
|
||||
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')
|
||||
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)
|
||||
|
||||
def test_bulk_inventory_delete_preview_confirm_and_history(self):
|
||||
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
|
||||
item = KitchenInventoryItem.objects.create(user=self.a, product=self.product_a, location=location)
|
||||
path = '/api/v1/kitchen/bulk-delete/'
|
||||
payload = {'item_ids': [str(item.api_id), str(self.item_b.api_id)]}
|
||||
preview = self.call('post', path, payload).json()
|
||||
self.assertEqual(preview['counts']['validated'], 1)
|
||||
self.assertEqual(preview['counts']['failed'], 1)
|
||||
self.assertEqual(preview['items'][0]['before']['product_name'], 'Pasta')
|
||||
item.refresh_from_db(); self.assertIsNone(item.archived_at)
|
||||
self.assertEqual(self.call('post', path, {'item_ids': [str(item.api_id)] * 2}).status_code, 400)
|
||||
self.assertEqual(self.call('post', path, {'item_ids': [str(item.api_id)] * 21}).status_code, 400)
|
||||
self.assertEqual(self.call('post', path, {**payload, 'dry_run': False}, HTTP_IDEMPOTENCY_KEY='delete-no-confirm').status_code, 400)
|
||||
atomic = self.call('post', path, {**payload, 'dry_run': False, 'confirm': True, 'atomic': True},
|
||||
HTTP_IDEMPOTENCY_KEY='bulk-delete-atomic')
|
||||
self.assertEqual(atomic.json()['status'], 'rolled_back')
|
||||
item.refresh_from_db(); self.assertIsNone(item.archived_at)
|
||||
run_payload = {**payload, 'dry_run': False, 'confirm': True}
|
||||
run = self.call('post', path, run_payload, HTTP_IDEMPOTENCY_KEY='bulk-delete')
|
||||
self.assertEqual(run.json()['counts']['changed'], 1)
|
||||
self.assertEqual(self.call('post', path, run_payload, HTTP_IDEMPOTENCY_KEY='bulk-delete').json(), run.json())
|
||||
item.refresh_from_db(); self.assertIsNotNone(item.archived_at)
|
||||
self.assertTrue(FoodProduct.objects.filter(pk=self.product_a.pk).exists())
|
||||
self.assertIsNone(KitchenInventoryItem.objects.get(pk=self.item_b.pk).archived_at)
|
||||
|
||||
def test_bulk_inventory_update_remaining_amount_uses_consumption_and_preview_rolls_back(self):
|
||||
product = FoodProduct.objects.create(owner=self.a, product_name='Flour', package_amount=Decimal('500'), package_unit='g')
|
||||
location = InventoryLocation.objects.create(user=self.a, name='Pantry')
|
||||
item = KitchenInventoryItem.objects.create(user=self.a, product=product, location=location,
|
||||
original_amount=Decimal('500'), remaining_amount=Decimal('500'), inventory_unit='g')
|
||||
payload = {'items': [{'item_id': str(item.api_id), 'changes': {'remaining_amount': '400', 'opened': True}}]}
|
||||
preview = self.call('post', '/api/v1/kitchen/bulk-update/', {**payload, 'dry_run': True})
|
||||
self.assertEqual(preview.json()['counts']['validated'], 1)
|
||||
self.assertEqual(InventoryConsumption.objects.filter(user=self.a).count(), 0)
|
||||
item.refresh_from_db(); self.assertEqual(item.remaining_amount, 500)
|
||||
applied = self.call('post', '/api/v1/kitchen/bulk-update/', {**payload, 'dry_run': False},
|
||||
HTTP_IDEMPOTENCY_KEY='bulk-stock-1')
|
||||
self.assertEqual(applied.json()['counts']['changed'], 1)
|
||||
self.assertEqual(InventoryConsumption.objects.filter(user=self.a).count(), 1)
|
||||
item.refresh_from_db(); self.assertEqual(item.remaining_amount, 400)
|
||||
|
||||
def test_authentication_scopes_and_token_storage(self):
|
||||
self.assertNotIn(self.secret, self.token.token_hash)
|
||||
self.assertEqual(self.token.token_hash, hashlib.sha256(self.secret.encode()).hexdigest())
|
||||
|
||||
Reference in New Issue
Block a user