Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions bank_api/bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def get_account(self, name: str) -> Account:

def add_funds(self, name: str, amount: int) -> None:
"""Add funds to the named account"""
account = self.get_account(name)
now = datetime.now()
self.transactions.append(Transaction(account, now, amount))

# check if amount is a non negative int
# greater than zero before adding funds
if isinstance(amount, int) and amount > 0:
account = self.get_account(name)
now = datetime.now()
self.transactions.append(Transaction(account, now, amount))
16 changes: 15 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@ def test_account_creation(client: FlaskClient):
# response = client.get('/example/route')
# Or use client.post to make a POST request
# https://flask.palletsprojects.com/en/1.1.x/testing/
pass
post_response = client.post("/accounts/IntegrationTest1")
created_name = post_response.json["name"]

assert post_response.status_code == 200
assert created_name == "IntegrationTest1"

get_response = client.get("/accounts/IntegrationTest1")
assert get_response.status_code == 200
assert get_response.json["name"] == "IntegrationTest1"

def test_add_money(client: FlaskClient):
post_response = client.post("/accounts/IntegrationTest1")

add_money_response = client.post("/money", data={'name': 'IntegrationTest1', 'amount': 100})
assert add_money_response.status_code == 200
40 changes: 40 additions & 0 deletions tests/test_bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,43 @@ def test_get_account_raises_error_if_no_account_matches(bank: Bank):

# TODO: Add unit tests for bank.add_funds()

def test_add_funds_successful(bank: Bank):
bank.create_account("TestName 1")

bank.add_funds("TestName 1", 100)

assert len(bank.transactions) == 1

def test_add_funds_raises_error_if_no_account_matches(bank: Bank):
bank.create_account("TestName 1")

with pytest.raises(ValueError):
bank.add_funds("TestName 2", 100)

def test_add_funds_string_amount_does_not_add_transaction(bank: Bank):
bank.create_account("TestName 1")

bank.add_funds("TestName 1", "100")

assert len(bank.transactions) == 0

def test_add_funds_decimal_value_does_not_add_transaction(bank: Bank):
bank.create_account("TestName 1")

bank.add_funds("TestName 1", 100.50)

assert len(bank.transactions) == 0

def test_add_funds_does_not_add_for_negative_amount(bank: Bank):
bank.create_account("TestName 1")

bank.add_funds("TestName 1", -1)

assert len(bank.transactions) == 0

def test_add_funds_does_not_add_for_zero_amount(bank: Bank):
bank.create_account("TestName 1")

bank.add_funds("TestName 1", 0)

assert len(bank.transactions) == 0