-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_payments.py
80 lines (64 loc) · 2.64 KB
/
test_payments.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from functools import partial
from unittest.mock import patch, call, MagicMock, ANY
from pytest import yield_fixture, raises
import payments
from payments import main as all_accounts, account_transactions, pay as do_payment
from model import db, record_payment_transaction as pay, IntegrityError
# in these tests we rely on the builtin 25 fake users created in the payments module
@yield_fixture
def connection():
conn = db.connect()
with conn.begin() as transaction:
patched_all_accounts = partial(payments.get_all_accounts, conn)
patched_account_transactions = partial(payments.get_account_transactions, connection=conn)
with patch.object(payments, 'get_all_accounts', patched_all_accounts),\
patch.object(payments, 'get_account_transactions', patched_account_transactions):
yield conn
transaction.rollback()
def test_successful_payment(connection):
pay(source=1, recipient=2, amount=30, connection=connection)
accts = all_accounts()
assert accts[1]['balance'] == 170
assert accts[2]['balance'] == 230
def test_successful_payment2(connection):
"""This test verifies that tests changes are properly rollbacked"""
accts = all_accounts()
assert accts[1]['balance'] == 200
pay(1, 2, 30, connection)
accts = all_accounts()
assert accts[1]['balance'] == 170
assert accts[2]['balance'] == 230
def test_balance_can_go_to_zero(connection):
pay(1, 2, 200, connection)
assert all_accounts()[1]['balance'] == 0
def test_transaction_log(connection):
pay(1, 2, 30, connection)
pay(2, 3, 30, connection)
assert len(account_transactions(2)) == 2
def test_give_back_money(connection):
before = all_accounts()
pay(1, 2, 30, connection)
pay(2, 1, 30, connection)
after = all_accounts()
assert before == after
def test_negative_balance(connection):
with raises(IntegrityError):
pay(1, 2, 200.01, connection)
def test_nonexisting_account_id(connection):
with raises(IntegrityError):
pay(101, 2, 10, connection)
def test_negative_amount(connection):
with raises(IntegrityError):
pay(1, 2, -1, connection)
def test_all_accounts():
with patch.object(payments, 'get_all_accounts') as mocked_model:
all_accounts()
assert mocked_model.call_args == ()
def test_account_transactions():
with patch.object(payments, 'get_account_transactions') as mocked_model:
account_transactions(2)
assert mocked_model.call_args == call(2)
def test_pay():
with patch.object(payments, 'record_payment_transaction') as mocked_model:
do_payment(1, 2, 30)
assert mocked_model.call_args == call(1, 2, 30)