-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_bank.py
More file actions
64 lines (43 loc) · 1.71 KB
/
test_bank.py
File metadata and controls
64 lines (43 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Unit tests for bank.py"""
import pytest
from bank_api.bank import Bank
@pytest.fixture
def bank() -> Bank:
return Bank()
def test_create_account_raises_error_if_name_blank(bank: Bank):
# This means: assert an exception is raised during the following block
with pytest.raises(Exception):
bank.create_account('')
def test_bank_creates_empty(bank: Bank):
assert len(bank.accounts) == 0
assert len(bank.transactions) == 0
def test_can_create_and_get_account(bank: Bank):
bank.create_account('Test')
account = bank.get_account('Test')
assert len(bank.accounts) == 1
assert account.name == 'Test'
def test_get_account_raises_error_if_no_account_matches(bank: Bank):
bank.create_account('Name 1')
# This means: assert an exception is raised during the following block
with pytest.raises(ValueError):
bank.get_account('Name 2')
# --- add_funds() tests ---
def test_add_funds_creates_transaction(bank: Bank):
bank.create_account('Alice')
bank.add_funds('Alice', 1000)
assert len(bank.transactions) == 1
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_raises_error_if_account_not_found(bank: Bank):
with pytest.raises(ValueError):
bank.add_funds('Nobody', 100)
def test_add_funds_raises_error_if_amount_is_negative(bank: Bank):
bank.create_account('Alice')
with pytest.raises(ValueError):
bank.add_funds('Alice', -50)
def test_add_funds_raises_error_if_amount_is_zero(bank: Bank):
bank.create_account('Alice')
with pytest.raises(ValueError):
bank.add_funds('Alice', 0)