Automated Teller Machine: tests and solutions


makePythonTdd ATM

The code in makePythonTdd.sh from Automated Teller Machine

 1#!/bin/bash
 2uv init atm
 3cd atm
 4mkdir tests
 5touch tests/__init__.py
 6
 7echo "import unittest
 8
 9
10class TestATM(unittest.TestCase):
11
12    def test_failure(self):
13        self.assertFalse(True)
14
15
16# Exceptions seen
17# AssertionError
18" > tests/test_atm.py
19
20echo "pytest" > requirements.txt
21echo "pytest-watcher" >> requirements.txt
22echo "watchdog==6.0.0" >> requirements.txt
23uv add --requirement requirements.txt
24uv run pytest-watcher . --now

The code in makePythonTdd.ps1 from Automated Teller Machine

 1uv init atm
 2cd atm
 3mkdir tests
 4New-Item tests/__init__.py
 5
 6"import unittest
 7
 8
 9class TestATM(unittest.TestCase):
10
11    def test_failure(self):
12        self.assertFalse(True)
13
14
15# Exceptions seen
16# AssertionError
17" | Out-File "tests/test_atm.py" -Encoding UTF8
18
19"pytest" | Out-File requirements.txt -Encoding UTF8
20"pytest-watcher" >> requirements.txt
21"watchdog==6.0.0" >> requirements.txt
22uv add --requirement requirements.txt
23uv run pytest-watcher . --now

Automated Teller Machine: tests

The code in atm/tests/test_atm.py from Automated Teller Machine

atm/tests/test_atm.py
 1import src.atm
 2import unittest
 3
 4
 5DENIED = 'DENIED'
 6
 7
 8class TestATM(unittest.TestCase):
 9
10    def test_card_w_below_limit_right_pin_enough_cash(self):
11        self.assertEqual(
12            src.atm.withdraw(
13                right_pin=True,
14                enough_cash=True,
15                above_daily_limit=False,
16                card_expired=False,
17            ),
18            'CASH'
19        )
20        self.assertEqual(
21            src.atm.withdraw(
22                right_pin=True,
23                enough_cash=True,
24                above_daily_limit=False,
25                card_expired=True,
26            ),
27            DENIED
28        )
atm/tests/test_atm.py
30    def test_card_w_above_limit_right_pin_enough_cash(self):
31        self.assertEqual(
32            src.atm.withdraw(
33                right_pin=True,
34                enough_cash=True,
35                above_daily_limit=True,
36                card_expired=True,
37            ),
38            DENIED
39        )
40        self.assertEqual(
41            src.atm.withdraw(
42                right_pin=True,
43                enough_cash=True,
44                above_daily_limit=True,
45                card_expired=False,
46            ),
47            DENIED
48        )
atm/tests/test_atm.py
50    def test_card_w_above_limit_right_pin_not_enough_cash(self):
51        self.assertEqual(
52            src.atm.withdraw(
53                right_pin=True,
54                enough_cash=False,
55                above_daily_limit=True,
56                card_expired=True,
57            ),
58            DENIED
59        )
60        self.assertEqual(
61            src.atm.withdraw(
62                right_pin=True,
63                enough_cash=False,
64                above_daily_limit=True,
65                card_expired=False,
66            ),
67            DENIED
68        )
atm/tests/test_atm.py
70    def test_card_w_below_limit_right_pin_not_enough_cash(self):
71        self.assertEqual(
72            src.atm.withdraw(
73                right_pin=True,
74                enough_cash=False,
75                above_daily_limit=False,
76                card_expired=True,
77            ),
78            DENIED
79        )
80        self.assertEqual(
81            src.atm.withdraw(
82                right_pin=True,
83                enough_cash=False,
84                above_daily_limit=False,
85                card_expired=False,
86            ),
87            DENIED
88        )
atm/tests/test_atm.py
 90    def test_card_w_above_limit_wrong_pin_enough_cash(self):
 91        self.assertEqual(
 92            src.atm.withdraw(
 93                right_pin=False,
 94                enough_cash=True,
 95                above_daily_limit=True,
 96                card_expired=True,
 97            ),
 98            DENIED
 99        )
100        self.assertEqual(
101            src.atm.withdraw(
102                right_pin=False,
103                enough_cash=True,
104                above_daily_limit=True,
105                card_expired=False,
106            ),
107            DENIED
108        )
atm/tests/test_atm.py
110    def test_card_w_below_limit_wrong_pin_enough_cash(self):
111        self.assertEqual(
112            src.atm.withdraw(
113                right_pin=False,
114                enough_cash=True,
115                above_daily_limit=False,
116                card_expired=True,
117            ),
118            DENIED
119        )
120        self.assertEqual(
121            src.atm.withdraw(
122                right_pin=False,
123                enough_cash=True,
124                above_daily_limit=False,
125                card_expired=False,
126            ),
127            DENIED
128        )
atm/tests/test_atm.py
130    def test_card_w_above_limit_wrong_pin_not_enough_cash(self):
131        self.assertEqual(
132            src.atm.withdraw(
133                right_pin=False,
134                enough_cash=False,
135                above_daily_limit=True,
136                card_expired=True,
137            ),
138            DENIED
139        )
140        self.assertEqual(
141            src.atm.withdraw(
142                right_pin=False,
143                enough_cash=False,
144                above_daily_limit=True,
145                card_expired=False,
146            ),
147            DENIED
148        )
atm/tests/test_atm.py
150    def test_card_w_below_limit_wrong_pin_not_enough_cash(self):
151        self.assertEqual(
152            src.atm.withdraw(
153                right_pin=False,
154                enough_cash=False,
155                above_daily_limit=False,
156                card_expired=True,
157            ),
158            DENIED
159        )
160        self.assertEqual(
161            src.atm.withdraw(
162                right_pin=False,
163                enough_cash=False,
164                above_daily_limit=False,
165                card_expired=False,
166            ),
167            DENIED
168        )
169
170
171# Exceptions seen
172# AssertionError
173# NameError
174# AttributeError
175# TypeError
176# SyntaxError

Automated Teller Machine: solution

The code in atm/src/atm/__init__.py from Automated Teller Machine

atm/src/__init__.py
 1def withdraw(
 2        right_pin, enough_cash,
 3        above_daily_limit=False, card_expired=False,
 4    ):
 5    denied = 'DENIED'
 6    if card_expired:
 7        return denied
 8    if above_daily_limit:
 9        return denied
10    if not right_pin:
11        return denied
12    if not enough_cash:
13        return denied
14    return 'CASH'

test atm with loops: test and solution

test atm with loops: test


The code in atm/tests/test_atm.py from test atm with loops

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

test atm with loops: solution


The code in atm/src/__init__.py from test atm with loops

atm/src/__init__.py
 1def withdraw(
 2        right_pin, enough_cash,
 3        above_daily_limit=False, card_expired=False,
 4    ):
 5    if card_expired:
 6        return 'DENIED: Card Expired'
 7    if above_daily_limit:
 8        return 'DENIED: Exceeded daily withdrawal limit'
 9    if not right_pin:
10        return 'DENIED: Wrong PIN. Try again...'
11    if not enough_cash:
12        return 'DENIED: Not enough CASH in Account'
13    return 'CASH'

test atm with generators: test

The code in atm/tests/test_atm.py from test atm with generators

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        for (
10            right_pin, enough_cash,
11            above_daily_limit, card_expired,
12        ) in itertools.product((False, True), repeat=4):
13              with self.subTest(
14                  right_pin=right_pin,
15                  enough_cash=enough_cash,
16                  above_daily_limit=above_daily_limit,
17                  card_expired=card_expired,
18              ):
19                  result = src.atm.withdraw(
20                      right_pin=right_pin,
21                      enough_cash=enough_cash,
22                      above_daily_limit=above_daily_limit,
23                      card_expired=card_expired,
24                  )
25                  if card_expired:
26                      self.assertEqual(
27                          result,
28                          'DENIED: Card Expired'
29                      )
30                  elif above_daily_limit:
31                      self.assertEqual(
32                          result,
33                          'DENIED: Exceeded daily withdrawal limit'
34                      )
35                  elif not right_pin:
36                      self.assertEqual(
37                          result,
38                          'DENIED: Wrong PIN. Try again...'
39                      )
40                  elif not enough_cash:
41                      self.assertEqual(
42                          result,
43                          'DENIED: '
44                          'Not enough CASH in Account'
45                      )
46                  else:
47                      self.assertEqual(
48                          result,
49                          'CASH'
50                      )
51
52
53# Exceptions seen
54# AssertionError
55# NameError
56# AttributeError
57# TypeError
58# SyntaxError

test atm with dictionaries: test

The code in atm/tests/test_atm.py from test atm with dictionaries

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(
34                          result, denials['card_expired']
35                      )
36                  elif above_daily_limit:
37                      self.assertEqual(
38                          result, denials['above_daily_limit']
39                      )
40                  elif not right_pin:
41                      self.assertEqual(
42                          result, denials['not_right_pin']
43                      )
44                  elif not enough_cash:
45                      self.assertEqual(
46                          result, denials['not_enough_cash']
47                      )
48                  else:
49                      self.assertEqual(result, 'CASH')
50
51
52# Exceptions seen
53# AssertionError
54# NameError
55# AttributeError
56# TypeError
57# SyntaxError