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)
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)
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)
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)
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
def test_add_two_items_to_a_machine(self): first_machine = Machine.load() first_machine.save() beer = Item(name='Beer', vending_machine=first_machine) beer.save() chocolate = Item(name='Hot chocolate', vending_machine=first_machine) chocolate.save() self.assertEqual(first_machine.beverageitem_set.count(), 2)
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)
def test_add_three_items_to_a_machine(self): machine = Machine.load() machine.save() beer = Item(name='Beer', vending_machine=machine) beer.save() chocolate = Item(name='Hot chocolate', vending_machine=machine) chocolate.save() coffee = Item(name='Coffee', vending_machine=machine) coffee.save() self.assertEqual(machine.beverageitem_set.count(), 3)
def general_inventory_get(request): if request.method == 'GET': machine = VendingMachine.load() item_dicts = [ {'id': item.id, 'name': item.name, 'quantity': item.quantity} for item in machine.beverageitem_set.all() ] response = HttpResponse(json.dumps(item_dicts), status=200, content_type='application/json' ) return response response = HttpResponse(status=404) return response
def inventory_get(request, item_id=None): machine = VendingMachine.load() if request.method == 'GET' 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 is not None: item_dicts = { 'name': item.name, 'quantity': item.quantity} response = HttpResponse(json.dumps(item_dicts), status=200, content_type='application/json' ) return response response = HttpResponse(status=404) return response
def test_saving_and_zero_coins_first(self): machine = Machine.load() machine.save() self.assertEqual(machine.coins, 0)
def test_add_item_to_a_machine(self): machine = Machine.load() machine.save() beer = Item(name='Beer', vending_machine=machine) beer.save() self.assertEqual(beer.vending_machine.id, machine.id)
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)
def test_post_refill(self): response = self.client.post('/refill') self.assertEqual(response.status_code, 200) self.assertEqual(response['content-type'], 'application/json') first_machine = Machine.load() self.assertEqual(first_machine.beverageitem_set.count(), 3)