From daf95181028a402f22792462e33643cbbad8dabf Mon Sep 17 00:00:00 2001 From: Billy O'Connell Date: Tue, 19 Sep 2023 14:14:22 +0100 Subject: [PATCH] add in changes to unit and integration tests --- bank_api/bank.py | 10 +++++++--- tests/test_app.py | 16 +++++++++++++++- tests/test_bank.py | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/bank_api/bank.py b/bank_api/bank.py index afe6353..05055a5 100644 --- a/bank_api/bank.py +++ b/bank_api/bank.py @@ -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)) diff --git a/tests/test_app.py b/tests/test_app.py index e05b062..3c184af 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -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 \ No newline at end of file diff --git a/tests/test_bank.py b/tests/test_bank.py index 0f106f0..dd131b9 100644 --- a/tests/test_bank.py +++ b/tests/test_bank.py @@ -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