SevdanurGenc's picture
Upload 60 files
63f6453 verified
Raw History Blame Contribute Delete
2.22 kB
import pytest
from solution import BankAccount, InsufficientFunds
@pytest.fixture
def acc():
return BankAccount("Ayşe", 100)
def test_init_strips_owner_and_sets_balance():
a = BankAccount(" Ali ", 50, overdraft_limit=20)
assert a.owner == "Ali" and a.balance == 50.0 and a.overdraft_limit == 20.0
@pytest.mark.parametrize("args", [("", 0), (" ", 0), ("Ali", -1), ("Ali", 0, -5)])
def test_init_rejects_invalid(args):
with pytest.raises(ValueError):
BankAccount(*args)
def test_deposit_increases_balance(acc):
assert acc.deposit(25) == 125
assert acc.history == [("deposit", 25)]
@pytest.mark.parametrize("amount", [0, -10])
def test_deposit_rejects_non_positive(acc, amount):
with pytest.raises(ValueError):
acc.deposit(amount)
def test_withdraw_within_balance(acc):
assert acc.withdraw(40) == 60
def test_withdraw_uses_overdraft_up_to_limit():
a = BankAccount("Can", 10, overdraft_limit=50)
assert a.withdraw(60) == -50
def test_withdraw_beyond_limit_raises():
a = BankAccount("Can", 10, overdraft_limit=50)
with pytest.raises(InsufficientFunds):
a.withdraw(60.01)
def test_withdraw_rejects_non_positive(acc):
with pytest.raises(ValueError):
acc.withdraw(0)
def test_transfer_moves_money(acc):
other = BankAccount("Veli")
acc.transfer(other, 30)
assert (acc.balance, other.balance) == (70, 30)
def test_transfer_to_self_rejected(acc):
with pytest.raises(ValueError):
acc.transfer(acc, 10)
def test_interest_on_positive_balance(acc):
assert acc.apply_interest(0.1) == 110
assert acc.history[-1] == ("interest", 10.0)
def test_no_interest_on_negative_balance():
a = BankAccount("Can", 0, overdraft_limit=100)
a.withdraw(50)
assert a.apply_interest(0.5) == -50
@pytest.mark.parametrize("rate", [-0.1, 1.5])
def test_interest_rate_bounds(acc, rate):
with pytest.raises(ValueError):
acc.apply_interest(rate)
def test_statement_lists_history(acc):
acc.deposit(5)
acc.withdraw(10)
text = acc.statement()
assert text.splitlines()[0] == "Hesap sahibi: Ayşe"
assert "withdraw -10.00" in text
assert text.endswith("Bakiye: 95.00")