Skip to content

Commit 1654179

Browse files
committed
merge~2
1 parent 3bea6dd commit 1654179

File tree

35 files changed

+1567
-0
lines changed

35 files changed

+1567
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class BankAccount:
2+
def __init__(self, owner, balance=0):
3+
self.owner = owner
4+
if balance < 0:
5+
raise ValueError("Balance cannot be negative")
6+
self.balance = balance
7+
8+
def deposit(self, amount):
9+
if amount <= 0:
10+
raise ValueError("Deposit amount must be positive")
11+
self.balance += amount
12+
13+
def withdraw(self, amount):
14+
if amount > self.balance:
15+
raise ValueError("Insufficient funds")
16+
if amount <= 0:
17+
raise ValueError("Withdrawal amount must be positive")
18+
self.balance -= amount
19+
20+
def get_balance(self):
21+
return self.balance
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
from Banking.Banking import BankAccount
3+
4+
@pytest.fixture
5+
def bank_account():
6+
return BankAccount("John Doe", 1000)
7+
8+
def test_initial_balance(bank_account):
9+
assert bank_account.get_balance() == 1000
10+
11+
def test_deposit(bank_account):
12+
bank_account.deposit(500)
13+
assert bank_account.get_balance() == 1500
14+
15+
def test_withdraw_success(bank_account):
16+
bank_account.withdraw(300)
17+
assert bank_account.get_balance() == 700
18+
19+
def test_withdraw_insufficient_funds(bank_account):
20+
with pytest.raises(ValueError, match="Insufficient funds"):
21+
bank_account.withdraw(2000)
22+
23+
def test_negative_deposit(bank_account):
24+
with pytest.raises(ValueError, match="Deposit amount must be positive"):
25+
bank_account.deposit(-50)
26+
27+
def test_negative_withdraw(bank_account):
28+
with pytest.raises(ValueError, match="Withdrawal amount must be positive"):
29+
bank_account.withdraw(-100)
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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")

Banking System/Banking System with Mongo/Banking/banktesting.py

Whitespace-only changes.

0 commit comments

Comments
 (0)