Budget App Spend chart unittest error

Hey everyone,
So I am done with the Budget App’s Spend Chart but I keep getting this unittest fail and its the only failed test:

FAIL: test_create_spend_chart (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/runner/boilerplate-budget-app-1/test_module.py", line 94, in test_create_spend_chart
    self.assertEqual(actual, expected, 'Expected different chart representation. Check that all spacing is exact.')
AssertionError: 'Perc[34 chars]     \n 90|         \n 80|         \n 70|    o[336 chars]    ' != 'Perc[34 chars]      \n 90|          \n 80|          \n 70|  [340 chars] t  '
Diff is 861 characters long. Set self.maxDiff to None to see it. : Expected different chart representation. Check that all spacing is exact.

Here is the chart:
image

I know it has to do with something in the spacing at the end of the returned string so I tried to strip it but it didn’t work.

This is my create_spend_chart function:

def create_spend_chart(categories):
    percentages = []
    count = 0

    # find out percentage of withdraws of all categories
    total_percentage = categories[count].total_withdraws
    for i in range(2):
        count += 1
        total_percentage += categories[count].total_withdraws

    for category in categories:
        percentages.append(int(round((category.total_withdraws / total_percentage) * 100)))

    # set some variables
    chart = []
    num = 100
    final_chart = ["Percentage spent by category\n"]
    bottom_line = "-"
    names = []
    vertical_names = ["     "]
    categories_str = []
    index = 0
    int1 = 4

    for category in categories:
        categories_str.append(str(category.name))

    # create bottom line and split names into a list of letters
    for c in categories_str:
        bottom_line += "---"
        name = str(c).ljust(len(max(categories_str, key=len)))
        names.append(list(name))

    # create vertical names 
    for i in range(len(names[0])):
        for lis in names:
            vertical_names.append(lis[index] + "  ")
            if len(vertical_names) == int1:
                vertical_names.append("\n" + "     ")
                int1 += 4
        index += 1

    names_str = "".join(vertical_names)

    # create "blueprint" of the chart with decreasing percentages from 100 to 0
    for i in range(11):
        chart.append(f"{str(num).rjust(3)}|")
        num -= 10

    # add the "o's" to mark the percetage spent by each category
    for ele in chart:
        els = ele.split("|")

        num2 = int(els[0])
        for percentage in percentages:
            if percentage >= num2:
                els.append(" o ")
            else:
                els.append("   ")

        els.insert(1, "|")
        line = "".join(els)
        final_chart.append(line + "\n")

    # convert the final_chart var to a string
    final_chart.append("    " + bottom_line + "\n" + names_str)
    final_chart = "".join(final_chart)

    return final_chart

I think the problem is somewhere in here:

# add the "o's" to mark the percetage spent by each category
    for ele in chart:
        els = ele.split("|")

        num2 = int(els[0])
        for percentage in percentages:
            if percentage >= num2:
                els.append(" o ")
            else:
                els.append("   ")

        els.insert(1, "|")
        line = "".join(els)
        final_chart.append(line + "\n")

    # convert the final_chart var to a string
    final_chart.append("    " + bottom_line + "\n" + names_str)
    final_chart = "".join(final_chart)

    return final_chart

Again, I tried using strip, lstrip and it didn’t work.

Thank you a lot in advance!

First, do this and check your spacing and output. You can set self.maxDiff = None in the class for this test so it will print the entire difference between your output and the expected output. The chart you posted is not the one being tested. Without the complete diff, it’s much harder to find the problem.

You may also want to post a link to your project code so that others can run it and check the output. I tried running your function with my code, but it was expecting a method on the category class that I did not have, so I could not generate the output myself.

1 Like
AssertionError: 'Perc[34 chars]     \n 90|' != 'Perc[34 chars]      \n 90|'

Left is correct, right is wrong. Left has 5 spaces, right has 6 spaces.
So you are adding to many spaces somewhere.

1 Like

Hey Jeremy, thank you for responding.
I did what you told me and this is what i get:

Percentage spent by category
- 100|         
+ 100|          
?              +
-  90|         
+  90|          
?              +
-  80|         
+  80|          
?              +
-  70|    o    
+  70|    o     
?              +
-  60|    o    
+  60|    o     
?              +
-  50|    o    
+  50|    o     
?              +
-  40|    o    
+  40|    o     
?              +
-  30|    o    
+  30|    o     
?              +
-  20|    o  o 
+  20|    o  o  
?              +
-  10|    o  o 
+  10|    o  o  
?              +
-   0| o  o  o 
+   0| o  o  o  
?              +
      ----------
       B  F  E  
       u  o  n  
       s  o  t  
       i  d  e  
       n     r  
       e     t  
       s     a  
       s     i  
             n  
             m  
             e  
             n  
-            t+            t  ?             ++
 : Expected different chart representation. Check that all spacing is exact.

I think I am missing spaces for some reason but i really don’t now how to fix it.
Here is the repl.it of my project:
https://repl.it/@SolGeller/boilerplate-budget-app-1

Thanks a lot again!

The red (-) is your output, the green (+) is the expected output and the ? line is marking the difference. So it appears you are missing a space at the end of each line. The bottom line is the same problem, it’s just all mashed together since there was no newline:

So this is missing two spaces.

1 Like

Thank you a lot!!!