test atm with dictionaries

I want to use a dictionary in the atm project.


preview

This is the test I have at the end of the chapter

atm/tests/test_atm.py
 1import itertools
 2import src.atm
 3import unittest
 4
 5
 6class TestATM(unittest.TestCase):
 7
 8    def test_atm(self):
 9        denials = {
10            'card_expired': 'DENIED: Card Expired',
11            'above_daily_limit': 'DENIED: Exceeded daily withdrawal limit',
12            'not_right_pin': 'DENIED: Wrong PIN. Try again...',
13            'not_enough_cash': 'DENIED: Not enough CASH in Account',
14        }
15
16        for (
17            right_pin, enough_cash,
18            above_daily_limit, card_expired
19        ) in itertools.product((False, True), repeat=4):
20              with self.subTest(
21                  right_pin=right_pin,
22                  enough_cash=enough_cash,
23                  above_daily_limit=above_daily_limit,
24                  card_expired=card_expired,
25              ):
26                  result = src.atm.withdraw(
27                      right_pin=right_pin,
28                      enough_cash=enough_cash,
29                      above_daily_limit=above_daily_limit,
30                      card_expired=card_expired,
31                  )
32                  if card_expired:
33                      self.assertEqual(denials['card_expired'])
34                  elif above_daily_limit:
35                      expectation = denials['above_daily_limit']
36                  elif not right_pin:
37                      expectation = denials['not_right_pin']
38                  elif not enough_cash:
39                      expectation = denials['not_enough_cash']
40                  else:
41                      expectation = 'CASH'
42
43                  self.assertEqual(result, expectation)
44
45
46# Exceptions seen
47# AssertionError
48# NameError
49# AttributeError
50# TypeError
51# SyntaxError

open the project


test_atm with dictionaries

RED: make it fail


  • I add a dictionary for the denials to test_atm

    atm/tests/test_atm.py
     8    def test_atm(self):
     9        denials = {
    10            'card_expired': 'DENIED: Card Expired',
    11            'above_daily_limit': 'DENIED: Exceeded daily withdrawal limit',
    12            'not_right_pin': 'DENIED: Wrong PIN. Try again...',
    13            'not_enough_cash': 'DENIED: Not enough CASH in Account',
    14        }
    15
    16        for (
    17            right_pin, enough_cash,
    18            above_daily_limit, card_expired
    19        ) in itertools.product((False, True), repeat=4):
    
  • I add variables for the expectations in the if statements then make one assertion for the variable

    atm/tests/test_atm.py
    32                  if card_expired:
    33                      expectation = denials['card_expired']
    34                      self.assertEqual(
    35                          result,
    36                          'DENIED: Card Expired'
    37                      )
    38                  elif above_daily_limit:
    39                      expectation = denials['above_daily_limit']
    40                      self.assertEqual(
    41                          result,
    42                          'DENIED: Exceeded daily withdrawal limit'
    43                      )
    
    atm/tests/test_atm.py
    44                  elif not right_pin:
    45                      expectation = denials['not_right_pin']
    46                      self.assertEqual(
    47                          result,
    48                          'DENIED: Wrong PIN. Try again...'
    49                      )
    50                  elif not enough_cash:
    51                      expectation = denials['not_enough_cash']
    52                      self.assertEqual(
    53                          result,
    54                          'DENIED: '
    55                          'Not enough CASH in Account'
    56                      )
    
    atm/tests/test_atm.py
    57                  else:
    58                      expectation = 'CASH'
    59                      self.assertEqual(
    60                          result,
    61                          'CASH'
    62                      )
    63                  self.assertEqual(result, 'BOOM!!!')
    64
    65
    66# Exceptions seen
    

    the terminal is my friend, and shows AssertionError for sixteen sub tests.


GREEN: make it pass


I change the expectation of the new assertion to the new variable

atm/tests/test_atm.py
57                  else:
58                      expectation = 'CASH'
59                      self.assertEqual(
60                          result,
61                          'CASH'
62                      )
63                  self.assertEqual(result, expectation)
64
65
66# Exceptions seen

the test passes.


REFACTOR: make it better


  • I remove the other assertions because they are now covered by the new variable

    atm/tests/test_atm.py
    32                  if card_expired:
    33                      expectation = denials['card_expired']
    34                  elif above_daily_limit:
    35                      expectation = denials['above_daily_limit']
    36                  elif not right_pin:
    37                      expectation = denials['not_right_pin']
    38                  elif not enough_cash:
    39                      expectation = denials['not_enough_cash']
    40                  else:
    41                      expectation = 'CASH'
    42
    43                  self.assertEqual(result, expectation)
    44
    45
    46# Exceptions seen
    

    the test passes and is slower than when I did not have a dictionary to check for the values of the denial message.

  • I add a git commit message

    terminal
    git commit -am \
    'test_atm with dictionaries'
    

close the project


review

In this case adding a dictionary makes the test slower than what I had before because it adds an extra step to the process - it has to look up the value of a denial.


code from the chapter

Do you want to see all the CODE I typed in this chapter?


what is next?

Would you like to use a dictionary in the person project?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.