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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
from itertools import product as iproduct
from random import randint
from uuid import uuid4
import pytest
from generalresearch.currency import LedgerCurrency
from generalresearch.managers.base import Permission
from generalresearch.managers.thl.ledger_manager.exceptions import (
LedgerAccountDoesntExistError,
)
from generalresearch.managers.thl.ledger_manager.ledger import LedgerManager
from generalresearch.models.thl.ledger import LedgerAccount, AccountType, Direction
from generalresearch.models.thl.ledger import (
LedgerEntry,
)
from test_utils.managers.ledger.conftest import ledger_account
@pytest.mark.parametrize(
argnames="currency, kind, acct_id",
argvalues=list(
iproduct(
["USD", "test", "EUR"],
["expense", "wallet", "revenue", "cash"],
[uuid4().hex for i in range(3)],
)
),
)
class TestLedgerAccountManagerNoResults:
def test_get_account_no_results(self, currency, kind, acct_id, lm):
"""Try to query for accounts that we know don't exist and confirm that
we either get the expected None result or it raises the correct
exception
"""
qn = ":".join([currency, kind, acct_id])
# (1) .get_account is just a wrapper for .get_account_many_ but
# call it either way
assert lm.get_account(qualified_name=qn, raise_on_error=False) is None
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
lm.get_account(qualified_name=qn, raise_on_error=True)
# (2) .get_account_if_exists is another wrapper
assert lm.get_account(qualified_name=qn, raise_on_error=False) is None
def test_get_account_no_results_many(self, currency, kind, acct_id, lm):
qn = ":".join([currency, kind, acct_id])
# (1) .get_many_
assert lm.get_account_many_(qualified_names=[qn], raise_on_error=False) == []
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
lm.get_account_many_(qualified_names=[qn], raise_on_error=True)
# (2) .get_many
assert lm.get_account_many(qualified_names=[qn], raise_on_error=False) == []
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
lm.get_account_many(qualified_names=[qn], raise_on_error=True)
# (3) .get_accounts(..)
assert lm.get_accounts_if_exists(qualified_names=[qn]) == []
with pytest.raises(expected_exception=LedgerAccountDoesntExistError):
lm.get_accounts(qualified_names=[qn])
@pytest.mark.parametrize(
argnames="currency, account_type, direction",
argvalues=list(
iproduct(
list(LedgerCurrency),
list(AccountType),
list(Direction),
)
),
)
class TestLedgerAccountManagerCreate:
def test_create_account_error_permission(
self, currency, account_type, direction, lm
):
"""Confirm that the Permission values that are set on the Ledger Manger
allow the Creation action to occur.
"""
acct_uuid = uuid4().hex
account = LedgerAccount(
display_name=f"test-{uuid4().hex}",
currency=currency,
qualified_name=f"{currency.value}:{account_type.value}:{acct_uuid}",
account_type=account_type,
normal_balance=direction,
)
# (1) With no Permissions defined
test_lm = LedgerManager(
pg_config=lm.pg_config,
permissions=[],
redis_config=lm.redis_config,
cache_prefix=lm.cache_prefix,
testing=lm.testing,
)
with pytest.raises(expected_exception=AssertionError) as excinfo:
test_lm.create_account(account=account)
assert (
str(excinfo.value) == "LedgerManager does not have sufficient permissions"
)
# (2) With Permissions defined, but not CREATE
test_lm = LedgerManager(
pg_config=lm.pg_config,
permissions=[Permission.READ, Permission.UPDATE, Permission.DELETE],
redis_config=lm.redis_config,
cache_prefix=lm.cache_prefix,
testing=lm.testing,
)
with pytest.raises(expected_exception=AssertionError) as excinfo:
test_lm.create_account(account=account)
assert (
str(excinfo.value) == "LedgerManager does not have sufficient permissions"
)
def test_create(self, currency, account_type, direction, lm):
"""Confirm that the Permission values that are set on the Ledger Manger
allow the Creation action to occur.
"""
acct_uuid = uuid4().hex
qn = f"{currency.value}:{account_type.value}:{acct_uuid}"
acct_model = LedgerAccount(
uuid=acct_uuid,
display_name=f"test-{uuid4().hex}",
currency=currency,
qualified_name=qn,
account_type=account_type,
normal_balance=direction,
)
account = lm.create_account(account=acct_model)
assert isinstance(account, LedgerAccount)
# Query for, and make sure the Account was saved in the DB
res = lm.get_account(qualified_name=qn, raise_on_error=True)
assert account.uuid == res.uuid
def test_get_or_create(self, currency, account_type, direction, lm):
"""Confirm that the Permission values that are set on the Ledger Manger
allow the Creation action to occur.
"""
acct_uuid = uuid4().hex
qn = f"{currency.value}:{account_type.value}:{acct_uuid}"
acct_model = LedgerAccount(
uuid=acct_uuid,
display_name=f"test-{uuid4().hex}",
currency=currency,
qualified_name=qn,
account_type=account_type,
normal_balance=direction,
)
account = lm.get_account_or_create(account=acct_model)
assert isinstance(account, LedgerAccount)
# Query for, and make sure the Account was saved in the DB
res = lm.get_account(qualified_name=qn, raise_on_error=True)
assert account.uuid == res.uuid
class TestLedgerAccountManagerGet:
def test_get(self, ledger_account, lm):
res = lm.get_account(qualified_name=ledger_account.qualified_name)
assert res.uuid == ledger_account.uuid
res = lm.get_account_many(qualified_names=[ledger_account.qualified_name])
assert len(res) == 1
assert res[0].uuid == ledger_account.uuid
res = lm.get_accounts(qualified_names=[ledger_account.qualified_name])
assert len(res) == 1
assert res[0].uuid == ledger_account.uuid
# TODO: I can't test the get_balance without first having Transaction
# creation working
def test_get_balance_empty(
self, ledger_account, ledger_account_credit, ledger_account_debit, ledger_tx, lm
):
res = lm.get_account_balance(account=ledger_account)
assert res == 0
res = lm.get_account_balance(account=ledger_account_credit)
assert res == 100
res = lm.get_account_balance(account=ledger_account_debit)
assert res == 100
@pytest.mark.parametrize("n_times", range(5))
def test_get_account_filtered_balance(
self,
ledger_account,
ledger_account_credit,
ledger_account_debit,
ledger_tx,
n_times,
lm,
):
"""Try searching for random metadata and confirm it's always 0 because
Tx can be found.
"""
rand_key = f"key-{uuid4().hex[:10]}"
rand_value = uuid4().hex
assert (
lm.get_account_filtered_balance(
account=ledger_account, metadata_key=rand_key, metadata_value=rand_value
)
== 0
)
# Let's create a transaction with this metadata to confirm it saves
# and that we can filter it back
rand_amount = randint(10, 1_000)
lm.create_tx(
entries=[
LedgerEntry(
direction=Direction.CREDIT,
account_uuid=ledger_account_credit.uuid,
amount=rand_amount,
),
LedgerEntry(
direction=Direction.DEBIT,
account_uuid=ledger_account_debit.uuid,
amount=rand_amount,
),
],
metadata={rand_key: rand_value},
)
assert (
lm.get_account_filtered_balance(
account=ledger_account_credit,
metadata_key=rand_key,
metadata_value=rand_value,
)
== rand_amount
)
assert (
lm.get_account_filtered_balance(
account=ledger_account_debit,
metadata_key=rand_key,
metadata_value=rand_value,
)
== rand_amount
)
def test_get_balance_timerange_empty(self, ledger_account, lm):
res = lm.get_account_balance_timerange(account=ledger_account)
assert res == 0
|