Skip to content

Code Review for the Bank API Exercise#3

Open
Jonesey13 wants to merge 5 commits intocorndeladmin:masterfrom
atuldharne:Stretch2
Open

Code Review for the Bank API Exercise#3
Jonesey13 wants to merge 5 commits intocorndeladmin:masterfrom
atuldharne:Stretch2

Conversation

@Jonesey13
Copy link
Contributor

No description provided.

- Added 5 unit tests covering add_funds() happy path, error on missing
  account, and rejection of zero/negative amounts
- Fixed bug in Bank.add_funds(): amount <= 0 now raises ValueError
- test_account_creation: POST creates account, GET retrieves it,
  both return 200 and correct JSON body
- test_get_account_returns_404_if_not_found: verifies 404 on missing account
- New BankReport class in bank_api/bank_report.py with get_balance(name)
  that sums transactions for the named account
- 3 unit tests: zero balance, summing multiple transactions, account isolation
- Tests use real Bank instance (to be mocked in StretchC)
- Import BankReport into app.py and wire it to the bank instance
- GET /accounts/<name> now returns {"name": ..., "balance": ...}
- Added integration test asserting balance reflects added funds
- Updated existing GET assertion to include balance: 0
- Replaced naive tests (using real Bank) with monkeypatched equivalents
- get_account mocked via lambda; transactions replaced with controlled list
- Isolation verified: breaking Bank.get_account fails test_bank.py but
  leaves test_bank_report.py fully green
Copy link
Contributor Author

@Jonesey13 Jonesey13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! All seems to work in my testing. My comments are mostly code quality comments rather than functional ones.

Comment on lines +48 to +49
if amount <= 0:
raise ValueError("Amount must be a positive integer")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently surfaces itself as a 500 error in the API but it's probably better suited as a 4XX error (i.e. a client error rather than a server error).

Comment on lines +21 to +22
assert post_response.status_code == 200
assert post_response.get_json() == {"name": "Alice"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth extracting "Alice" as a test constant here.

Comment on lines +26 to +27
assert get_response.status_code == 200
assert get_response.get_json() == {"name": "Alice", "balance": 0}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth discussing whether testing the precise structure of the JSON response is in scope for this test. For example you could instead write this as:

Suggested change
assert get_response.status_code == 200
assert get_response.get_json() == {"name": "Alice", "balance": 0}
assert get_response.status_code == 200
body_json = get_response.get_json()
assert body_json["name"] == "Alice"
assert body_json["balance"] == 0

This has 2 (potential) benefits:

  • If the shape of the response changes/extends to more properties this test will not break (and arguably shouldn't break as an account is still created
  • The test will fail on a specific attribute (rather than on the entire object)

Comment on lines +43 to +47
def test_add_funds_transaction_has_correct_amount(bank: Bank):
bank.create_account('Alice')
bank.add_funds('Alice', 500)

assert bank.transactions[0].amount == 500
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests could be written in a more Arrange/Act/Assert style e.g.

Suggested change
def test_add_funds_transaction_has_correct_amount(bank: Bank):
bank.create_account('Alice')
bank.add_funds('Alice', 500)
assert bank.transactions[0].amount == 500
def test_add_funds_transaction_has_correct_amount(bank: Bank):
# Arrange
test_account = 'Alice'
test_amount = 500
bank.create_account(test_account)
# Act
bank.add_funds(test_account , test_amount)
# Assert
assert len(bank.transactions) == 1
created_transaction = bank.transactions[0]
assert created_transaction.amount == test_amount
assert created_transaction.account.name == test_account

It's more verbose but arguably easier to understand (following the principle of self-documenting code)

Comment on lines +19 to +20
monkeypatch.setattr(bank, 'get_account', lambda name: account)
monkeypatch.setattr(bank, 'transactions', [])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(More the exercise's problem than yours but) Be aware that monkeypatch is fairly redundant here (vs editing the object properties directly) as a new bank is created for each test. If we were calling static methods on the Bank class then monkeypatch would be perfect for this task (as using monkeypatch in a test ensures any changes do not persist between tests).

@Jonesey13 Jonesey13 marked this pull request as ready for review February 26, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants