def test_vending_machine_errors_on_bad_arguments(): """ Negative tests for better developer feedback """ with pytest.raises(expected_exception=Exception): vendmach.VendingMachine(itemrequest='AAA') with pytest.raises(expected_exception=Exception): vendmach.VendingMachine(itemrequest='AAA', coins=1) with pytest.raises(expected_exception=vendmach.BadCoinError): vendmach.VendingMachine(itemrequest='AAA', coins=';lkj')
def test_vendingmachine_knows_if_coins_are_enough(): choice = list(vendmach.config.VALID_ITEMS)[0] machine = vendmach.VendingMachine(itemrequest=choice, coins=[10]) assert not machine.is_paid machine.coins = [50, 50, 50, 50] assert machine.is_paid
def test_can_not_update_coins_with_not_configured_coins(): machine = vendmach.VendingMachine( itemrequest='nuke license', coins=[], ) with pytest.raises(expected_exception=vendmach.BadCoinError): machine.coins = [123451]
def test_if_item_is_not_paid_for_item_attr_is_none(): choice = list(vendmach.config.VALID_ITEMS)[0] mchine = vendmach.VendingMachine(itemrequest=choice, coins=[10]) assert not mchine.is_paid assert mchine.item is None mchine.coins = [50, 50, 50, 50] assert mchine.is_paid assert mchine.item == choice
def test_functionality_example(): # The item's name we want itemname = 'renderfarm time' # The budget budget = [] machine = vendmach.VendingMachine( itemrequest=itemname, coins=budget, ) assert machine.item is None # budget is not enough assert not machine.is_paid budget += [-1] # The budget contains coins not configured # for the vending machine with pytest.raises(expected_exception=vendmach.BadCoinError): machine.coins = budget assert machine.item is None # budget is not enough assert not machine.is_paid budget = [10] machine.coins = budget # budget is still not enough assert machine.item is None assert not machine.is_paid budget = [10, 10, 50] machine.coins = budget # budget is just enough, we get the item. The machine's state # is paid. There's no change. assert machine.item == itemname assert machine.is_paid assert not machine.change # adding more to the budget, maybe we get another item (not implemented...) machine.coins += [20, 50] # we get the item(s), the machine is paid and we get change back. # we get back what we added as extra, the item was precisely paid # for the last time. assert machine.item == itemname assert machine.is_paid assert machine.change == [50, 20] # we have a bigger budget this time machine.coins = [50, 50] # we get the item and get change back assert machine.item == itemname assert machine.is_paid assert machine.change == [20, 10] # we can get an empty machine and ask it for stuff later # when we made up our mind. machine = vendmach.VendingMachine() # we don't get our item of course assert not machine.item assert not machine.is_paid # if we set the bidget, but don't choose yet we don't get an item machine.coins = [50, 50, 50, 50, 50] assert machine.item is None assert machine.is_paid is None
def test_can_instantiate_machine_without_arguments_for_later_use(): machine = vendmach.VendingMachine() assert isinstance(machine, vendmach.VendingMachine)
def test_vendingmachine_returns_change_if_overpaid(budget, expected): machine = vendmach.VendingMachine(itemrequest='nuke license', coins=budget) assert machine.change == expected
def test_can_init_vendingmachine_w_item_and_coins(): choice = list(vendmach.config.VALID_ITEMS)[0] vendmach.VendingMachine(itemrequest=choice, coins=[10])