forked from reingart/exercism
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbank_account_test.py
137 lines (118 loc) · 4.72 KB
/
bank_account_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/bank-account/canonical-data.json
# File last updated on 2023-07-20
import unittest
from bank_account import (
BankAccount,
)
class BankAccountTest(unittest.TestCase):
def test_newly_opened_account_has_zero_balance(self):
account = BankAccount()
account.open()
self.assertEqual(account.get_balance(), 0)
def test_single_deposit(self):
account = BankAccount()
account.open()
account.deposit(100)
self.assertEqual(account.get_balance(), 100)
def test_multiple_deposits(self):
account = BankAccount()
account.open()
account.deposit(100)
account.deposit(50)
self.assertEqual(account.get_balance(), 150)
def test_withdraw_once(self):
account = BankAccount()
account.open()
account.deposit(100)
account.withdraw(75)
self.assertEqual(account.get_balance(), 25)
def test_withdraw_twice(self):
account = BankAccount()
account.open()
account.deposit(100)
account.withdraw(80)
account.withdraw(20)
self.assertEqual(account.get_balance(), 0)
def test_can_do_multiple_operations_sequentially(self):
account = BankAccount()
account.open()
account.deposit(100)
account.deposit(110)
account.withdraw(200)
account.deposit(60)
account.withdraw(50)
self.assertEqual(account.get_balance(), 20)
def test_cannot_check_balance_of_closed_account(self):
account = BankAccount()
account.open()
account.close()
with self.assertRaises(ValueError) as err:
account.get_balance()
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_deposit_into_closed_account(self):
account = BankAccount()
account.open()
account.close()
with self.assertRaises(ValueError) as err:
account.deposit(50)
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_deposit_into_unopened_account(self):
account = BankAccount()
with self.assertRaises(ValueError) as err:
account.deposit(50)
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_withdraw_from_closed_account(self):
account = BankAccount()
account.open()
account.close()
with self.assertRaises(ValueError) as err:
account.withdraw(50)
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_close_an_account_that_was_not_opened(self):
account = BankAccount()
with self.assertRaises(ValueError) as err:
account.close()
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "account not open")
def test_cannot_open_an_already_opened_account(self):
account = BankAccount()
account.open()
with self.assertRaises(ValueError) as err:
account.open()
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "account already open")
def test_reopened_account_does_not_retain_balance(self):
account = BankAccount()
account.open()
account.deposit(50)
account.close()
account.open()
self.assertEqual(account.get_balance(), 0)
def test_cannot_withdraw_more_than_deposited(self):
account = BankAccount()
account.open()
account.deposit(25)
with self.assertRaises(ValueError) as err:
account.withdraw(50)
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "amount must be less than balance")
def test_cannot_withdraw_negative(self):
account = BankAccount()
account.open()
account.deposit(100)
with self.assertRaises(ValueError) as err:
account.withdraw(-50)
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "amount must be greater than 0")
def test_cannot_deposit_negative(self):
account = BankAccount()
account.open()
with self.assertRaises(ValueError) as err:
account.deposit(-50)
self.assertEqual(type(err.exception), ValueError)
self.assertEqual(err.exception.args[0], "amount must be greater than 0")