-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.py
161 lines (129 loc) · 5.13 KB
/
server_test.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
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
import functools
import textwrap
from pydantic import FilePath
import pytest
from beancount import loader
from server import get_accounts, get_tables, run_query
# test_server.py
@functools.lru_cache(None)
def load():
entries, errors, options = loader.load_string(
textwrap.dedent("""
2022-01-01 open Assets:Checking USD
2022-01-01 open Assets:Federal:401k IRAUSD
2022-01-01 open Assets:Gold GLD
2022-01-01 open Assets:Vacation VACHR
2022-01-01 open Assets:Vanguard:RGAGX RGAGX
2022-01-01 open Expenses:Commissions USD
2022-01-01 open Expenses:Food USD
2022-01-01 open Expenses:Home:Rent USD
2022-01-01 open Expenses:Taxes:401k IRAUSD
2022-01-01 open Expenses:Taxes:Federal USD
2022-01-01 open Expenses:Tests USD
2022-01-01 open Expenses:Vacation VACHR
2022-01-01 open Income:ACME USD
2022-01-01 open Income:Gains USD
2022-01-01 open Income:Vacation VACHR
2022-01-01 * "ACME" "Salary"
Assets:Checking 10.00 USD
Income:ACME -11.00 USD
Expenses:Taxes:Federal 1.00 USD
Assets:Federal:401k -2.00 IRAUSD
Expenses:Taxes:401k 2.00 IRAUSD
Assets:Vacation 5 VACHR
Income:Vacation -5 VACHR
2022-01-01 * "Rent"
Assets:Checking 42.00 USD
Expenses:Home:Rent 42.00 USD
2022-01-02 * "Holidays"
Assets:Vacation -1 VACHR
Expenses:Vacation
2022-01-03 * "Test 01"
Assets:Checking 1.00 USD
Expenses:Tests
2022-01-04 * "My Fovorite Plase" "Eating out alone"
Assets:Checking 4.00 USD
Expenses:Food
2022-01-05 * "Invest"
Assets:Checking -359.94 USD
Assets:Vanguard:RGAGX 2.086 RGAGX {172.55 USD}
2013-10-23 * "Buy Gold"
Assets:Checking -1278.67 USD
Assets:Gold 9 GLD {141.08 USD}
Expenses:Commissions 8.95 USD
2022-01-07 * "Sell Gold"
Assets:Gold -16 GLD {147.01 USD} @ 135.50 USD
Assets:Checking 2159.05 USD
Expenses:Commissions 8.95 USD
Income:Gains 184.16 USD
2022-01-08 * "Sell Gold"
Assets:Gold -16 GLD {147.01 USD} @ 135.50 USD
Assets:Checking 2159.05 USD
Expenses:Commissions 8.95 USD
Income:Gains 184.16 USD
2022-02-01 * "ACME" "Salary"
Assets:Checking 10.00 USD
Income:ACME -11.00 USD
Expenses:Taxes:Federal 1.00 USD
Assets:Federal:401k -2.00 IRAUSD
Expenses:Taxes:401k 2.00 IRAUSD
Assets:Vacation 5 VACHR
Income:Vacation -5 VACHR
2022-02-01 * "Rent"
Assets:Checking 43.00 USD
Expenses:Home:Rent 43.00 USD
2022-02-02 * "Test 02"
Assets:Checking 2.00 USD
Expenses:Tests
2030-01-01 query "taxes" "
SELECT
date, description, position, balance
WHERE
account ~ 'Taxes'
ORDER BY date DESC
LIMIT 20"
2015-01-01 query "home" "
SELECT
last(date) as latest,
account,
sum(position) as total
WHERE
account ~ ':Home:'
GROUP BY account"
""")
)
return entries, errors, options
@pytest.fixture
def mock_ledger(monkeypatch: pytest.MonkeyPatch):
"""Fixture to mock the _entries global variable."""
monkeypatch.setattr("server.settings.ledger", FilePath("sample.bean"))
@pytest.fixture
def clear_ledger(monkeypatch: pytest.MonkeyPatch):
"""Fixture to clear the _entries global variable."""
monkeypatch.setattr("server.settings.ledger", None)
def test_run_query_no_ledger_loaded(clear_ledger: None):
"""Test run_query when no ledger file is loaded."""
with pytest.raises(Exception) as excinfo:
run_query("SELECT account WHERE account ~ 'Assets'")
excinfo.match(
"No ledger file given or file could not be found. Use set_ledger_file to set a path."
)
def test_run_query_valid_query(mock_ledger: None):
"""Test run_query with a valid query."""
query = "SELECT account, sum(position) WHERE account ~ 'Assets' GROUP BY account"
result = run_query(query)
assert r"Assets:US:Hooli:Vacation 39 VACHR" in result
def test_get_tables(mock_ledger: None):
"""Test the get_tables resource."""
result = get_tables()
assert (
"accounts\nbalances\ncommodities\ndocuments\nentries\nevents\nnotes\npostings\nprices\ntransactions\n"
in result
) # Ensure the query for tables is executed
def test_get_accounts(mock_ledger: None):
"""Test the get_accounts resource."""
result = get_accounts()
assert (
"Assets:US:BofA:Checking" in result
) # Ensure an account from the ledger is present
assert "Expenses:Food" in result # Ensure another account is present