class TestMoneyManager(unittest.TestCase): def setUp(self): # Create a test BankAccount object self.user = MoneyManager() # Provide it with some initial balance values self.user.balance = 1000.0 def test_legal_deposit_works(self): # Your code here to test that depsositing money using the account's # 'deposit_funds' function adds the amount to the balance. self.user.deposit_funds(100) self.assertEqual(self.user.balance, 1100) def test_illegal_deposit_raises_exception(self): # Your code here to test that depositing an illegal value (like 'bananas' # or such - something which is NOT a float) results in an exception being # raised. self.assertRaises((TypeError, ValueError), self.user.deposit_funds('bananas')) self.assertRaises((TypeError, ValueError), self.user.deposit_funds('1000')) def test_legal_entry(self): # Your code here to test that adding a new entry with a a legal amount subtracts the # funds from the balance. self.user.add_entry(500, 'food') self.assertEqual(self.user.balance, 500) def test_illegal_entry_amount(self): # Your code here to test that withdrawing an illegal amount (like 'bananas' # or such - something which is NOT a float) raises a suitable exception. self.assertRaises((TypeError, ValueError), self.user.add_entry('bananas', 'food')) self.assertRaises((TypeError, ValueError), self.user.add_entry('500', 'food')) def test_illegal_entry_type(self): # Your code here to test that adding an illegal entry type (like 'bananas' # or such - something which is NOT a float) raises a suitable exception. self.assertRaises((TypeError, ValueError), self.user.add_entry(500, 'bananas')) self.assertRaises((TypeError, ValueError), self.user.add_entry(500, 1200)) def test_insufficient_funds_entry(self): # Your code here to test that you can only spend funds which are available. # For example, if you have a balance of 500.00 dollars then that is the maximum # that can be spent. If you tried to spend 600.00 then a suitable exception # should be raised and the withdrawal should NOT be applied to the user balance # or the user's transaction list. self.assertRaises((TypeError, ValueError), self.user.add_entry(2000, 'food'))
def perform_deposit(): '''Function to add a deposit for the amount in the amount entry to the user's transaction list.''' global user global amount_entry global balance_label global balance_var try: bal = balance_var.get() bal = float(bal) except: c = Tk() c.title('FedUni Money Manager') l = Label(c, text='Enter Valid Data', font=("Helvetica", 16)) l.pack() c.mainloop() return # Try to increase the account balance and append the deposit to the account file try: # Get the cash amount to deposit. Note: We check legality inside account's deposit method balance = balance_var.get() balance = balance.strip() balance = balance.replace(" ", "") file = str(user_number_var.get()) MoneyManager.deposit_funds(file, balance) fo = open(file + ".txt", "rb") fo.readline() fo.readline() fo.seek(12, 1) bal = fo.readline() bal = str(bal) bal = bal.replace("b", "") bal = bal.replace("r", "") bal = bal.replace("n", "") bal = bal.replace("\\", "") bal = bal.replace("'", "") bal = float(bal) fo.close() # Deposit funds bal = float(bal) + float(balance) # Update the transaction widget with the new transaction by calling account.get_transaction_string() # Note: Configure the text widget to be state='normal' first, then delete contents, then instert new # contents, and finally configure back to state='disabled' so it cannot be user edited. MoneyManager.get_transaction_string(file, bal) # Change the balance label to reflect the new balance global y, variable y.set("Balance : $ " + str(bal)) v = "Deposit " MoneyManager.save_to_file(file, v, balance) fo = open(file + ".txt", "r") fo.readline().replace("\n", "") fo.readline().replace("\n", "") fo.readline().replace("\n", "") fo.readline().replace("\n", "") x = fo.readline() transaction_list = x while x: x = fo.readline() transaction_list = transaction_list + x fo.close() Textbox.delete(1.0, END) Textbox.insert(END, transaction_list) # Clear the amount entry global balance_entry balance_entry.delete(0, END) # Update the interest graph with our new balance plot_spending_graph() # Catch and display exception as a 'showerror' messagebox with a title of 'Transaction Error' and the text of the exception except: c = Tk() c.title('FedUni Money Manager') l = Label(c, text='Something Went Wrong', font=("Helvetica", 16)) l.pack() c.mainloop()