1+ import unittest
2+ from billing .accounts import BankAccount
3+
4+ class TestBankAccount (unittest .TestCase ):
5+ def test_init (self ):
6+ account = BankAccount ("John Doe" , 100.0 )
7+ self .assertEqual (account .owner , "John Doe" )
8+ self .assertEqual (account .balance , 100.0 )
9+
10+ def test_init_negative_balance (self ):
11+ with self .assertRaises (ValueError ):
12+ BankAccount ("John Doe" , - 50.0 )
13+
14+ def test_deposit (self ):
15+ account = BankAccount ("John Doe" )
16+ self .assertEqual (account .deposit (50.0 ), 50.0 )
17+ self .assertEqual (account .balance , 50.0 )
18+
19+ def test_deposit_negative_amount (self ):
20+ account = BankAccount ("John Doe" )
21+ with self .assertRaises (ValueError ):
22+ account .deposit (- 50.0 )
23+
24+ def test_withdraw (self ):
25+ account = BankAccount ("John Doe" , 100.0 )
26+ self .assertEqual (account .withdraw (50.0 ), 50.0 )
27+ self .assertEqual (account .balance , 50.0 )
28+
29+ def test_withdraw_negative_amount (self ):
30+ account = BankAccount ("John Doe" , 100.0 )
31+ with self .assertRaises (ValueError ):
32+ account .withdraw (- 50.0 )
33+
34+ def test_withdraw_insufficient_funds (self ):
35+ account = BankAccount ("John Doe" , 50.0 )
36+ with self .assertRaises (ValueError ):
37+ account .withdraw (100.0 )
38+
39+ def test_transfer (self ):
40+ account1 = BankAccount ("John Doe" , 100.0 )
41+ account2 = BankAccount ("Jane Doe" , 50.0 )
42+ balance1 , balance2 = account1 .transfer (account2 , 30.0 )
43+ self .assertEqual (balance1 , 70.0 )
44+ self .assertEqual (balance2 , 80.0 )
45+ self .assertEqual (account1 .balance , 70.0 )
46+ self .assertEqual (account2 .balance , 80.0 )
47+
48+ def test_transfer_invalid_recipient (self ):
49+ account = BankAccount ("John Doe" , 100.0 )
50+ with self .assertRaises (ValueError ):
51+ account .transfer ("not an account" , 50.0 )
52+
53+ def test_transfer_insufficient_funds (self ):
54+ account1 = BankAccount ("John Doe" , 50.0 )
55+ account2 = BankAccount ("Jane Doe" , 50.0 )
56+ with self .assertRaises (ValueError ):
57+ account1 .transfer (account2 , 100.0 )
58+
59+ def test_get_balance (self ):
60+ account = BankAccount ("John Doe" , 100.0 )
61+ self .assertEqual (account .get_balance (), 100.0 )
62+
63+ if __name__ == '__main__' :
64+ unittest .main ()
0 commit comments