Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit d35d608

Browse files
Add unit tests for BankAccount class
1 parent 142a003 commit d35d608

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

billing/tests/test_accounts.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import unittest
2+
from billing.accounts import BankAccount
3+
4+
class TestBankAccount(unittest.TestCase):
5+
def setUp(self):
6+
self.account = BankAccount("John Doe", 1000.0)
7+
8+
def test_initialization(self):
9+
self.assertEqual(self.account.owner, "John Doe")
10+
self.assertEqual(self.account.balance, 1000.0)
11+
12+
def test_initialization_with_negative_balance(self):
13+
with self.assertRaises(ValueError):
14+
BankAccount("Jane Doe", -100.0)
15+
16+
def test_deposit(self):
17+
new_balance = self.account.deposit(500.0)
18+
self.assertEqual(new_balance, 1500.0)
19+
self.assertEqual(self.account.balance, 1500.0)
20+
21+
def test_deposit_negative_amount(self):
22+
with self.assertRaises(ValueError):
23+
self.account.deposit(-100.0)
24+
25+
def test_withdraw(self):
26+
new_balance = self.account.withdraw(300.0)
27+
self.assertEqual(new_balance, 700.0)
28+
self.assertEqual(self.account.balance, 700.0)
29+
30+
def test_withdraw_negative_amount(self):
31+
with self.assertRaises(ValueError):
32+
self.account.withdraw(-100.0)
33+
34+
def test_withdraw_insufficient_funds(self):
35+
with self.assertRaises(ValueError):
36+
self.account.withdraw(1500.0)
37+
38+
def test_transfer(self):
39+
other_account = BankAccount("Jane Doe", 500.0)
40+
sender_balance, recipient_balance = self.account.transfer(other_account, 300.0)
41+
self.assertEqual(sender_balance, 700.0)
42+
self.assertEqual(recipient_balance, 800.0)
43+
self.assertEqual(self.account.balance, 700.0)
44+
self.assertEqual(other_account.balance, 800.0)
45+
46+
def test_transfer_invalid_recipient(self):
47+
with self.assertRaises(ValueError):
48+
self.account.transfer("not a bank account", 100.0)
49+
50+
def test_transfer_insufficient_funds(self):
51+
other_account = BankAccount("Jane Doe", 500.0)
52+
with self.assertRaises(ValueError):
53+
self.account.transfer(other_account, 1500.0)
54+
55+
def test_get_balance(self):
56+
self.assertEqual(self.account.get_balance(), 1000.0)
57+
58+
if __name__ == '__main__':
59+
unittest.main()

0 commit comments

Comments
 (0)