def balance(self): """ Property responsible for displaying money nicely instead of just a Decimal object that could have up to 28 points of precision """ return money.to_money(self._balance)
def test_deposit_always_logs_a_message(self, deposit: Decimal, caplog): deposit = money.to_money(deposit) machine = VendingMachine() machine.deposit(deposit) self.logs_one_message(caplog) # Resetting the log capturing utility because hypothesis won't reset it caplog.clear()
def test_deposit_0_or_less_logs_error_to_user(self, caplog): deposit = money.to_money(Decimal("-5")) machine = VendingMachine() machine.deposit(deposit) self.logs_one_message(caplog) assert "ERROR" in caplog.text assert "negative or 0" in caplog.text
def dispense_change(self) -> Decimal: """ Returns the remaining amount to the user """ change = money.to_money(self._balance) self._balance = Decimal(0) fancy_print(LOG_SUCCESS, f"Dispensed {change}. Have a good day!") return change
def test_deposit_money_logs_success_to_user(self, caplog): deposit = money.to_money(Decimal("30")) machine = VendingMachine() machine.deposit(deposit) self.logs_one_message(caplog) assert "SUCCESS" in caplog.text assert "Successfully deposited" in caplog.text
def test_deposit_money_accepts_positive_amounts(self, deposit: Decimal): """ Test case ensures that any positive amount rounded to the nearest cent can be accepted by the vending machine. """ deposit = money.to_money(deposit) machine = VendingMachine() machine.deposit(deposit) machine.deposit(deposit) assert machine.balance == money.add(deposit, deposit)
def to_json(self) -> Dict[str, Any]: """ Dumps the core vending machine object to a json serializable object to be able to save to disk to persist state across CLI commands. """ return { "balance": str(money.to_money(self._balance)), "items": { location: item.to_json() for location, item in self.items.items() }, "purchases": [purchase.to_json() for purchase in self.purchases], }
def test_purchase_returns_proper_change(self, item: Item, balance: Decimal): # pylint: disable=no-self-use,invalid-name,redefined-outer-name """ Testing to ensure that any valid purchase returns proper change by the formula ouput = input - price of item. Capping largest value at 100k because when a decimal gets generated by hypothesis that is larger than decimal's size precision, an error gets thrown. """ balance = money.to_money(balance) expected_change = money.subtract(balance, item.price) # Hypothesis doesn't reset the fixture, so we're going to manually # reset the remaining_stock instance variable to make sure # we don't hit OutOfStockErrors after a couple tests item.remaining_stock = 5 change = item.purchase(balance) assert change == expected_change