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
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
I open a terminal
I change directory to the atm folder in the
pumping_pythonfolderI open
test_atm.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
test_atm with dictionaries
RED: make it fail
I add a dictionary for the denials to test_atm
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
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 )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 )57 else: 58 expectation = 'CASH' 59 self.assertEqual( 60 result, 61 'CASH' 62 ) 63 self.assertEqual(result, 'BOOM!!!') 64 65 66# Exceptions seenthe 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
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
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 seenthe 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
close the project
I close
tests/test_atm.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests
I change directory to the parent of Automated Teller Machine
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
what is next?
I know how to make a Python Test Driven Development environment manually.
I know how to make a Python Test Driven Development environment automatically.
I know how to make a Python Test Driven Development environment automatically with variables.
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.