1+ import pytest
2+ from decimal import Decimal
3+
4+ # Assuming these are the classes we're testing
5+ # You'll need to replace these imports with your actual implementation
6+ class BankAccount :
7+ def __init__ (self , account_id , owner_name , balance = 0.0 ):
8+ self .account_id = account_id
9+ self .owner_name = owner_name
10+ self .balance = Decimal (str (balance ))
11+
12+ def deposit (self , amount ):
13+ if amount <= 0 :
14+ raise ValueError ("Deposit amount must be positive" )
15+ self .balance += Decimal (str (amount ))
16+ return self .balance
17+
18+ def withdraw (self , amount ):
19+ if amount <= 0 :
20+ raise ValueError ("Withdrawal amount must be positive" )
21+ if amount > self .balance :
22+ raise ValueError ("Insufficient funds" )
23+ self .balance -= Decimal (str (amount ))
24+ return self .balance
25+
26+ def get_balance (self ):
27+ return self .balance
28+
29+ class Bank :
30+ def __init__ (self , name ):
31+ self .name = name
32+ self .accounts = {}
33+
34+ def create_account (self , account_id , owner_name , initial_balance = 0.0 ):
35+ if account_id in self .accounts :
36+ raise ValueError (f"Account with ID { account_id } already exists" )
37+ account = BankAccount (account_id , owner_name , initial_balance )
38+ self .accounts [account_id ] = account
39+ return account
40+
41+ def get_account (self , account_id ):
42+ if account_id not in self .accounts :
43+ raise ValueError (f"Account with ID { account_id } does not exist" )
44+ return self .accounts [account_id ]
45+
46+ def transfer (self , from_account_id , to_account_id , amount ):
47+ if amount <= 0 :
48+ raise ValueError ("Transfer amount must be positive" )
49+
50+ from_account = self .get_account (from_account_id )
51+ to_account = self .get_account (to_account_id )
52+
53+ from_account .withdraw (amount )
54+ to_account .deposit (amount )
55+
56+ return from_account .get_balance (), to_account .get_balance ()
57+
58+
59+ # Tests for the BankAccount class
60+ class TestBankAccount :
61+ def test_init (self ):
62+ account = BankAccount ("123" , "John Doe" , 100.0 )
63+ assert account .account_id == "123"
64+ assert account .owner_name == "John Doe"
65+ assert account .balance == Decimal ("100.0" )
66+
67+ def test_deposit (self ):
68+ account = BankAccount ("123" , "John Doe" , 100.0 )
69+ new_balance = account .deposit (50.0 )
70+ assert new_balance == Decimal ("150.0" )
71+ assert account .balance == Decimal ("150.0" )
72+
73+ def test_deposit_negative_amount (self ):
74+ account = BankAccount ("123" , "John Doe" , 100.0 )
75+ with pytest .raises (ValueError , match = "Deposit amount must be positive" ):
76+ account .deposit (- 50.0 )
77+
78+ def test_withdraw (self ):
79+ account = BankAccount ("123" , "John Doe" , 100.0 )
80+ new_balance = account .withdraw (50.0 )
81+ assert new_balance == Decimal ("50.0" )
82+ assert account .balance == Decimal ("50.0" )
83+
84+ def test_withdraw_negative_amount (self ):
85+ account = BankAccount ("123" , "John Doe" , 100.0 )
86+ with pytest .raises (ValueError , match = "Withdrawal amount must be positive" ):
87+ account .withdraw (- 50.0 )
88+
89+ def test_withdraw_insufficient_funds (self ):
90+ account = BankAccount ("123" , "John Doe" , 100.0 )
91+ with pytest .raises (ValueError , match = "Insufficient funds" ):
92+ account .withdraw (150.0 )
93+
94+ def test_get_balance (self ):
95+ account = BankAccount ("123" , "John Doe" , 100.0 )
96+ assert account .get_balance () == Decimal ("100.0" )
97+
98+
99+ # Tests for the Bank class
100+ class TestBank :
101+ def test_init (self ):
102+ bank = Bank ("Test Bank" )
103+ assert bank .name == "Test Bank"
104+ assert bank .accounts == {}
105+
106+ def test_create_account (self ):
107+ bank = Bank ("Test Bank" )
108+ account = bank .create_account ("123" , "John Doe" , 100.0 )
109+ assert account .account_id == "123"
110+ assert account .owner_name == "John Doe"
111+ assert account .balance == Decimal ("100.0" )
112+ assert "123" in bank .accounts
113+
114+ def test_create_duplicate_account (self ):
115+ bank = Bank ("Test Bank" )
116+ bank .create_account ("123" , "John Doe" , 100.0 )
117+ with pytest .raises (ValueError , match = "Account with ID 123 already exists" ):
118+ bank .create_account ("123" , "Jane Smith" , 200.0 )
119+
120+ def test_get_account (self ):
121+ bank = Bank ("Test Bank" )
122+ bank .create_account ("123" , "John Doe" , 100.0 )
123+ account = bank .get_account ("123" )
124+ assert account .account_id == "123"
125+ assert account .owner_name == "John Doe"
126+ assert account .balance == Decimal ("100.0" )
127+
128+ def test_get_nonexistent_account (self ):
129+ bank = Bank ("Test Bank" )
130+ with pytest .raises (ValueError , match = "Account with ID 123 does not exist" ):
131+ bank .get_account ("123" )
132+
133+ def test_transfer (self ):
134+ bank = Bank ("Test Bank" )
135+ bank .create_account ("123" , "John Doe" , 100.0 )
136+ bank .create_account ("456" , "Jane Smith" , 50.0 )
137+ from_balance , to_balance = bank .transfer ("123" , "456" , 30.0 )
138+ assert from_balance == Decimal ("70.0" )
139+ assert to_balance == Decimal ("80.0" )
140+ assert bank .get_account ("123" ).balance == Decimal ("70.0" )
141+ assert bank .get_account ("456" ).balance == Decimal ("80.0" )
142+
143+ def test_transfer_negative_amount (self ):
144+ bank = Bank ("Test Bank" )
145+ bank .create_account ("123" , "John Doe" , 100.0 )
146+ bank .create_account ("456" , "Jane Smith" , 50.0 )
147+ with pytest .raises (ValueError , match = "Transfer amount must be positive" ):
148+ bank .transfer ("123" , "456" , - 30.0 )
149+
150+ def test_transfer_insufficient_funds (self ):
151+ bank = Bank ("Test Bank" )
152+ bank .create_account ("123" , "John Doe" , 100.0 )
153+ bank .create_account ("456" , "Jane Smith" , 50.0 )
154+ with pytest .raises (ValueError , match = "Insufficient funds" ):
155+ bank .transfer ("123" , "456" , 150.0 )
156+
157+ def test_transfer_nonexistent_account (self ):
158+ bank = Bank ("Test Bank" )
159+ bank .create_account ("123" , "John Doe" , 100.0 )
160+ with pytest .raises (ValueError , match = "Account with ID 456 does not exist" ):
161+ bank .transfer ("123" , "456" , 50.0 )
162+
163+
164+ # Using pytest fixtures for more complex scenarios
165+ @pytest .fixture
166+ def populated_bank ():
167+ bank = Bank ("Test Bank" )
168+ bank .create_account ("123" , "John Doe" , 1000.0 )
169+ bank .create_account ("456" , "Jane Smith" , 2000.0 )
170+ bank .create_account ("789" , "Bob Johnson" , 500.0 )
171+ return bank
172+
173+ class TestBankOperations :
174+ def test_multiple_transfers (self , populated_bank ):
175+ bank = populated_bank
176+ # Transfer from account 123 to 456
177+ bank .transfer ("123" , "456" , 200.0 )
178+ # Transfer from account 456 to 789
179+ bank .transfer ("456" , "789" , 300.0 )
180+
181+ assert bank .get_account ("123" ).balance == Decimal ("800.0" )
182+ assert bank .get_account ("456" ).balance == Decimal ("1900.0" )
183+ assert bank .get_account ("789" ).balance == Decimal ("800.0" )
184+
185+ def test_deposit_and_withdraw_sequence (self , populated_bank ):
186+ bank = populated_bank
187+ account = bank .get_account ("123" )
188+
189+ account .deposit (500.0 )
190+ assert account .balance == Decimal ("1500.0" )
191+
192+ account .withdraw (300.0 )
193+ assert account .balance == Decimal ("1200.0" )
194+
195+ account .deposit (100.0 )
196+ account .withdraw (50.0 )
197+ assert account .balance == Decimal ("1250.0" )
0 commit comments