def inventory_put(request, item_id=None):
    machine = VendingMachine.load()
    if request.method == 'PUT' and item_id is not None:
        if machine.beverageitem_set.filter(id=item_id).exists():
            item = machine.beverageitem_set.get(id=item_id)
            if item.price > machine.coins:
                response = HttpResponse(status=400)
                response['X-Coins'] = machine.coins
                return response

            elif item.quantity > 0:
                machine_factory = MachineFactory()
                machine_factory.buy_item('BeverageVendingMachineManager', item_id=item.pk)
                response = HttpResponse(json.dumps({"quantity": 1}),
                                        status=200,
                                        content_type='application/json'
                                        )
                item.refresh_from_db()
                machine.refresh_from_db()
                response['X-Coins'] = machine.coins
                response['X-Inventory-Remaining'] = item.quantity
                return response
        else:
            response = HttpResponse(status=404)
            response['X-Coins'] = machine.coins
            return response

    response = HttpResponse(status=404)
    return response
Ejemplo n.º 2
0
 def test_delete_zero_coin(self):
     machine_factory = MachineFactory()
     coins_returned = machine_factory.delete_coins(
         'BeverageVendingMachineManager')
     machine = Machine.load()
     self.assertEqual(machine.coins, 0)
     self.assertEqual(coins_returned, 0)
Ejemplo n.º 3
0
 def test_buy_item_insufficient_founds(self):
     machine = Machine.load()
     machine.coins = 4
     machine.save()
     beer = Item(name='Beer', vending_machine=machine, price=5)
     beer.save()
     machine_factory = MachineFactory()
     machine_factory.buy_item('BeverageVendingMachineManager',
                              item_id=beer.id)
     beer.refresh_from_db()
     machine = Machine.load()
     self.assertEqual(beer.quantity, 5)
     self.assertEqual(machine.coins, 4)
Ejemplo n.º 4
0
 def test_buy_item_no_stock(self):
     machine = Machine.load()
     machine.coins = 4
     machine.save()
     beer = Item(name='Beer', vending_machine=machine, quantity=0)
     beer.save()
     machine_factory = MachineFactory()
     machine_factory.buy_item('BeverageVendingMachineManager',
                              item_id=beer.id)
     beer.refresh_from_db()
     machine = Machine.load()
     self.assertEqual(beer.quantity, 0)
     self.assertEqual(machine.coins, 4)
Ejemplo n.º 5
0
 def test_buy_one_item_success_one_remaining_coin(self):
     machine = Machine.load()
     machine.coins = 3
     machine.save()
     beer = Item(name='Beer', vending_machine=machine)
     beer.save()
     machine_factory = MachineFactory()
     machine_factory.buy_item('BeverageVendingMachineManager',
                              item_id=beer.id)
     beer.refresh_from_db()
     machine = Machine.load()
     self.assertEqual(beer.quantity, 4)
     self.assertEqual(machine.coins, 1)
Ejemplo n.º 6
0
 def test_buy_two_different_items_success(self):
     machine = Machine.load()
     machine.coins = 4
     machine.save()
     beer = Item(name='Beer', vending_machine=machine)
     beer.save()
     lemonade = Item(name='Lemonade', vending_machine=machine)
     lemonade.save()
     machine_factory = MachineFactory()
     machine_factory.buy_item('BeverageVendingMachineManager',
                              item_id=beer.id)
     machine_factory.buy_item('BeverageVendingMachineManager',
                              item_id=lemonade.id)
     lemonade.refresh_from_db()
     machine = Machine.load()
     self.assertEqual(lemonade.quantity, 4)
     self.assertEqual(machine.coins, 0)
Ejemplo n.º 7
0
 def test_add_two_coins(self):
     machine_factory = MachineFactory()
     machine_factory.add_one_coin('BeverageVendingMachineManager')
     machine_factory.add_one_coin('BeverageVendingMachineManager')
     machine = Machine.load()
     self.assertEqual(machine.coins, 2)
Ejemplo n.º 8
0
def add_one_coin():
    machine_factory = MachineFactory()
    return machine_factory.add_one_coin('BeverageVendingMachineManager')
Ejemplo n.º 9
0
def delete_coins():
    machine_factory = MachineFactory()
    coins_returned = machine_factory.delete_coins(
        'BeverageVendingMachineManager')
    return coins_returned