Skip to content

Commit b4aa0ec

Browse files
committed
adding time tests example
1 parent e380b99 commit b4aa0ec

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

src/bank_account.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from src.exceptions import InsufficientFundsError
1+
from datetime import datetime
2+
from src.exceptions import InsufficientFundsError, WithdrawalTimeRestrictionError
23

34

45
class BankAccount:
56
def __init__(self, balance=0, log_file=None):
67
self.balance = balance
78
self.log_file = log_file
8-
self._log_transaction('Cuenta creada')
9+
self._log_transaction("Cuenta creada")
910

1011
def _log_transaction(self, message):
1112
if self.log_file:
@@ -19,8 +20,14 @@ def deposit(self, amount):
1920
return self.balance
2021

2122
def withdraw(self, amount):
23+
now = datetime.now()
24+
if now.hour < 8 or now.hour > 17:
25+
raise WithdrawalTimeRestrictionError("Withdrawals are only allowed from 8am to 5pm")
26+
2227
if amount > self.balance:
23-
raise InsufficientFundsError(f"Withdrawal of {amount} exceeds balance {self.balance}")
28+
raise InsufficientFundsError(
29+
f"Withdrawal of {amount} exceeds balance {self.balance}"
30+
)
2431
if amount > 0:
2532
self.balance -= amount
2633
self._log_transaction(f"Withdrew {amount}. New balance: {self.balance}")

src/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
class InsufficientFundsError(Exception):
2+
pass
3+
4+
class WithdrawalTimeRestrictionError(Exception):
25
pass

tests/test_bank_account.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,4 @@ def test_withdraw_disallow_before_bussines_hours(self, mock_datetime):
5757
def test_withdraw_disallow_after_bussines_hours(self, mock_datetime):
5858
mock_datetime.now.return_value.hour = 18
5959
with self.assertRaises(WithdrawalTimeRestrictionError):
60-
self.account.withdraw(100)
61-
62-
63-
def test_deposit_multiple_ammounts(self):
64-
65-
test_cases = [
66-
{"ammount": 100, "expected": 1100},
67-
{"ammount": 3000, "expected": 4000},
68-
{"ammount": 4500, "expected": 5500},
69-
]
70-
for case in test_cases:
71-
with self.subTest(case=case):
72-
self.account = BankAccount(balance=1000, log_file="transactions.txt")
73-
new_balance = self.account.deposit(case["ammount"])
74-
self.assertEqual(new_balance, case["expected"])
60+
self.account.withdraw(100)

0 commit comments

Comments
 (0)