Budget App Project Failed Transfer method in Unittests

I need help please in this transfer method this is my whole code but I get weird outputs I don’t know why

class Category:

   def __init__(self,budget_category):

       self.ledger = []

       self.budget_category = budget_category

       self.call = False

   def deposit( self, amount, description = ''):

       self.amount = amount

       self.description = description

       if self.call:

           Category(self.anthr_category).ledger.append({"amount": self.amount, "description": self.description})

           return None

       self.ledger.append({"amount": self.amount, "description": self.description})

   def withdraw(self , withdraw_amount , description = ''):

       self.withdraw_amount = withdraw_amount

       self.description = description

       if self.withdraw_amount <= self.get_balance(): 

           self.ledger.append({"amount": -self.withdraw_amount, "description": self.description})    

       return self.check_funds(self.withdraw_amount)

   def get_balance(self):

       balance = 0

       for item in range(len(self.ledger)):

           balance += self.ledger[item]["amount"]

       return balance

   def transfer(self, trans_amount , anthr_category):

       self.anthr_category = anthr_category

       self.trans_amount = trans_amount

       self.call = self.check_funds(self.trans_amount)

       if self.call:

           self.withdraw(self.trans_amount , f"Transfer to {self.anthr_category}")

           self.deposit(self.trans_amount , f"Transfer from {self.budget_category}")

       return self.call

   def check_funds(self , amount):

       self.amount = amount

       if self.amount > self.get_balance():

           return False

       return True

   def _string(self):

       self.header = self.budget_category.center(30 , '*')

       self.lst = ''

       for item in range(len(self.ledger)):

           self.short_descrptn = self.ledger[item]["description"][:23]

           self.cash = f'{self.ledger[item]["amount"]:.2f}'.rjust(30-(len(self.short_descrptn)))

           self.lst += f'{self.short_descrptn}{self.cash}' + '\n'

       self.lst = self.lst + f'Total: {self.get_balance()}'

       return self.header + '\n'+ self.lst

   def __str__(self):

       return self._string()

when I run the test module I get weird astericks on the test_transfer method in class unittests as follows `======================================================================
FAIL: test_to_string (main.UnitTests)

Traceback (most recent call last):
File “c:\Users\Bradleys\Desktop\python projects\boilerplate-budget-app\test_module.py”, line 83, in test_to_string
self.assertEqual(actual, expected, ‘Expected different string representation of object.’)
AssertionError: ‘[75 chars]ggs, bac -45.67\nTransfer to ********Ent -20.00\nTotal: 834.33’ != '[75 chars]ggs, bac -45.67\nTransfer to Entertainme -20.00\nTotal: 834.33’
Food
deposit 900.00
milk, cereal, eggs, bac -45.67

  • Transfer to ********Ent -20.00
  • Transfer to Entertainme -20.00
    Total: 834.33 : Expected different string representation of object.

======================================================================
FAIL: test_transfer (main.UnitTests)

Traceback (most recent call last):
File “c:\Users\Bradleys\Desktop\python projects\boilerplate-budget-app\test_module.py”, line 52, in test_transfer
self.assertEqual(actual, expected, ‘Expected transfer method to create a specific ledger item in food object.’)
AssertionError: {‘amo[18 chars]ption’: ‘Transfer to Entertainment*\nTotal: 0’} != {‘amo[18 chars]ption’: ‘Transfer to Entertainment’}

  • {‘amount’: -20, ‘description’: ‘Transfer to Entertainment’}
  • {‘amount’: -20,
  • ‘description’: ‘Transfer to Entertainment*\nTotal: 0’} : Expected transfer method to create a specific ledger item in food object.

Ran 11 tests in 0.008s

FAILED (failures=3)`

Remember that using class instance in a string requires it to be written as a string, this makes use of the string representation of that instance. Does this make clearer what is happening?

Create a __repr__ method in the class so that when you want to have the assigned representation of your anthr_category argument, you just call on it with anthr_category.__repr__.

def __repr__(self):
    return self.budget_category

def transfer(self, trans_amount , anthr_category):

       self.anthr_category = anthr_category

       self.trans_amount = trans_amount

       self.call = self.check_funds(self.trans_amount)

       if self.call:

           self.withdraw(self.trans_amount , f"Transfer to {anthr_category.__repr__()}")

           self.deposit(self.trans_amount , f"Transfer from {self.__repr__()}")

       return self.call

I think this will do it, tell me if there are any more additional problems.

I did not understand what you meant can you please elaborate through examples if possible

great idea but won’t this idea go against the general use of repr() method

In other words - f"Transfer to {self.anthr_category}" are calling __str__ method of the class.

Thanks let me read more about this

Thanks it got the job done

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.