diff options
Diffstat (limited to 'beancount_extras_kris7t/plugins/templates_test.py')
-rw-r--r-- | beancount_extras_kris7t/plugins/templates_test.py | 326 |
1 files changed, 326 insertions, 0 deletions
diff --git a/beancount_extras_kris7t/plugins/templates_test.py b/beancount_extras_kris7t/plugins/templates_test.py new file mode 100644 index 0000000..5f63e6c --- /dev/null +++ b/beancount_extras_kris7t/plugins/templates_test.py | |||
@@ -0,0 +1,326 @@ | |||
1 | __copyright__ = 'Copyright (c) 2020 Kristóf Marussy <kristof@marussy.com>' | ||
2 | __license__ = 'GNU GPLv2' | ||
3 | |||
4 | import unittest | ||
5 | |||
6 | from beancount import loader | ||
7 | from beancount.parser import cmptest | ||
8 | import pytest | ||
9 | |||
10 | |||
11 | class TestClosingBalance(cmptest.TestCase): | ||
12 | |||
13 | @loader.load_doc() | ||
14 | def test_use_template_simple(self, entries, errors, options_map): | ||
15 | ''' | ||
16 | plugin "beancount_extras_kris7t.plugins.templates" | ||
17 | |||
18 | 2020-01-01 open Assets:Checking | ||
19 | 2020-01-01 open Expenses:Food | ||
20 | |||
21 | 2020-01-01 * "Eating out" #tag ^link | ||
22 | template: "eating-out" | ||
23 | Assets:Checking 25 USD | ||
24 | Expenses:Food -25 USD | ||
25 | |||
26 | 2020-03-15 custom "template-use" "eating-out" | ||
27 | |||
28 | 2020-04-12 custom "template-use" "eating-out" | ||
29 | ''' | ||
30 | self.assertEqualEntries(''' | ||
31 | plugin "beancount_extras_kris7t.plugins.closing_balance" | ||
32 | |||
33 | 2020-01-01 open Assets:Checking | ||
34 | 2020-01-01 open Expenses:Food | ||
35 | |||
36 | 2020-03-15 * "Eating out" #tag ^link ^template_eating-out | ||
37 | template: "eating-out" | ||
38 | Assets:Checking 25 USD | ||
39 | Expenses:Food -25 USD | ||
40 | |||
41 | 2020-04-12 * "Eating out" #tag ^link ^template_eating-out | ||
42 | template: "eating-out" | ||
43 | Assets:Checking 25 USD | ||
44 | Expenses:Food -25 USD | ||
45 | ''', entries) | ||
46 | |||
47 | @loader.load_doc() | ||
48 | def test_use_template_metadata(self, entries, errors, options_map): | ||
49 | ''' | ||
50 | plugin "beancount_extras_kris7t.plugins.templates" | ||
51 | |||
52 | 2020-01-01 open Assets:Checking | ||
53 | 2020-01-01 open Expenses:Food | ||
54 | |||
55 | 2020-01-01 * "Eating out" #tag ^link | ||
56 | template: "eating-out" | ||
57 | meta1: "data" | ||
58 | meta2: TRUE | ||
59 | Assets:Checking 25 USD | ||
60 | Expenses:Food -25 USD | ||
61 | |||
62 | 2020-03-15 custom "template-use" "eating-out" | ||
63 | meta1: "foo" | ||
64 | meta3: 3.14 | ||
65 | ''' | ||
66 | self.assertEqualEntries(''' | ||
67 | plugin "beancount_extras_kris7t.plugins.closing_balance" | ||
68 | |||
69 | 2020-01-01 open Assets:Checking | ||
70 | 2020-01-01 open Expenses:Food | ||
71 | |||
72 | 2020-03-15 * "Eating out" #tag ^link ^template_eating-out | ||
73 | template: "eating-out" | ||
74 | meta1: "foo" | ||
75 | meta2: TRUE | ||
76 | meta3: 2.14 | ||
77 | Assets:Checking 25 USD | ||
78 | Expenses:Food -25 USD | ||
79 | ''', entries) | ||
80 | |||
81 | @loader.load_doc() | ||
82 | def test_use_template_scaled(self, entries, errors, options_map): | ||
83 | ''' | ||
84 | plugin "beancount_extras_kris7t.plugins.templates" | ||
85 | |||
86 | 2020-01-01 open Assets:Checking | ||
87 | 2020-01-01 open Expenses:Food | ||
88 | |||
89 | 2020-01-01 * "Eating out" #tag ^link | ||
90 | template: "eating-out" | ||
91 | Assets:Checking 1 USD | ||
92 | Expenses:Food -1 USD | ||
93 | |||
94 | 2020-03-15 custom "template-use" "eating-out" 25 | ||
95 | |||
96 | 2020-04-12 custom "template-use" "eating-out" 27 | ||
97 | ''' | ||
98 | self.assertEqualEntries(''' | ||
99 | plugin "beancount_extras_kris7t.plugins.closing_balance" | ||
100 | |||
101 | 2020-01-01 open Assets:Checking | ||
102 | 2020-01-01 open Expenses:Food | ||
103 | |||
104 | 2020-03-15 * "Eating out" #tag ^link ^template_eating-out | ||
105 | template: "eating-out" | ||
106 | Assets:Checking 25 USD | ||
107 | Expenses:Food -25 USD | ||
108 | |||
109 | 2020-04-12 * "Eating out" #tag ^link ^template_eating-out | ||
110 | template: "eating-out" | ||
111 | Assets:Checking 27 USD | ||
112 | Expenses:Food -27 USD | ||
113 | ''', entries) | ||
114 | |||
115 | @loader.load_doc() | ||
116 | def test_use_template_overwritten(self, entries, errors, options_map): | ||
117 | ''' | ||
118 | plugin "beancount_extras_kris7t.plugins.templates" | ||
119 | |||
120 | 2020-01-01 open Assets:Checking | ||
121 | 2020-01-01 open Expenses:Food | ||
122 | 2020-01-01 open Expenses:Tax | ||
123 | |||
124 | 2020-04-01 * "Eating out" #tag ^link | ||
125 | template: "eating-out" | ||
126 | Assets:Checking 1.10 USD | ||
127 | Expenses:Food -1 USD | ||
128 | Expenses:Tax -0.10 USD | ||
129 | |||
130 | 2020-01-01 * "Eating out" #tag ^link | ||
131 | template: "eating-out" | ||
132 | Assets:Checking 1 USD | ||
133 | Expenses:Food -1 USD | ||
134 | |||
135 | 2020-03-15 custom "template-use" "eating-out" 25 | ||
136 | |||
137 | 2020-04-12 custom "template-use" "eating-out" 27 | ||
138 | ''' | ||
139 | self.assertEqualEntries(''' | ||
140 | plugin "beancount_extras_kris7t.plugins.closing_balance" | ||
141 | |||
142 | 2020-01-01 open Assets:Checking | ||
143 | 2020-01-01 open Expenses:Food | ||
144 | 2020-01-01 open Expenses:Tax | ||
145 | |||
146 | 2020-03-15 * "Eating out" #tag ^link ^template_eating-out | ||
147 | template: "eating-out" | ||
148 | Assets:Checking 25 USD | ||
149 | Expenses:Food -25 USD | ||
150 | |||
151 | 2020-04-12 * "Eating out" #tag ^link ^template_eating-out | ||
152 | template: "eating-out" | ||
153 | Assets:Checking 29.70 USD | ||
154 | Expenses:Food -27 USD | ||
155 | Expenses:Tax -2.70 USD | ||
156 | ''', entries) | ||
157 | |||
158 | @loader.load_doc(expect_errors=True) | ||
159 | def test_invalid_name(self, entries, errors, options_map): | ||
160 | ''' | ||
161 | plugin "beancount_extras_kris7t.plugins.templates" | ||
162 | |||
163 | 2020-01-01 open Assets:Checking | ||
164 | 2020-01-01 open Expenses:Food | ||
165 | |||
166 | 2020-01-01 * "Eating out" | ||
167 | template: TRUE | ||
168 | Assets:Checking 25 USD | ||
169 | Expenses:Food -25 USD | ||
170 | ''' | ||
171 | self.assertRegex(errors[0].message, "^template must be a string") | ||
172 | |||
173 | @pytest.mark.xfail(reason="Empty custom directive fails in beancount.ops.pad") | ||
174 | @loader.load_doc(expect_errors=True) | ||
175 | def test_use_missing_name(self, entries, errors, options_map): | ||
176 | ''' | ||
177 | plugin "beancount_extras_kris7t.plugins.templates" | ||
178 | |||
179 | 2020-01-01 open Assets:Checking | ||
180 | 2020-01-01 open Expenses:Food | ||
181 | |||
182 | 2020-01-01 * "Eating out" | ||
183 | template: "eating-out" | ||
184 | Assets:Checking 25 USD | ||
185 | Expenses:Food -25 USD | ||
186 | |||
187 | 2020-03-15 custom "template-use" | ||
188 | ''' | ||
189 | self.assertRegex(errors[0].message, "^Template name missing") | ||
190 | |||
191 | @loader.load_doc(expect_errors=True) | ||
192 | def test_use_too_many_arguments(self, entries, errors, options_map): | ||
193 | ''' | ||
194 | plugin "beancount_extras_kris7t.plugins.templates" | ||
195 | |||
196 | 2020-01-01 open Assets:Checking | ||
197 | 2020-01-01 open Expenses:Food | ||
198 | |||
199 | 2020-01-01 * "Eating out" | ||
200 | template: "eating-out" | ||
201 | Assets:Checking 25 USD | ||
202 | Expenses:Food -25 USD | ||
203 | |||
204 | 2020-03-15 custom "template-use" "eating-out" 2 "another" | ||
205 | ''' | ||
206 | self.assertRegex(errors[0].message, "^Too many template-use arguments") | ||
207 | |||
208 | @loader.load_doc(expect_errors=True) | ||
209 | def test_use_invalid_name(self, entries, errors, options_map): | ||
210 | ''' | ||
211 | plugin "beancount_extras_kris7t.plugins.templates" | ||
212 | |||
213 | 2020-03-15 custom "template-use" TRUE | ||
214 | ''' | ||
215 | self.assertRegex(errors[0].message, "^Template name must be a string") | ||
216 | |||
217 | @loader.load_doc(expect_errors=True) | ||
218 | def test_use_unknown(self, entries, errors, options_map): | ||
219 | ''' | ||
220 | plugin "beancount_extras_kris7t.plugins.templates" | ||
221 | |||
222 | 2020-03-15 custom "template-use" "taxi" | ||
223 | ''' | ||
224 | self.assertRegex(errors[0].message, "^Unknown template") | ||
225 | |||
226 | @loader.load_doc(expect_errors=True) | ||
227 | def test_use_invalid_scale_factor(self, entries, errors, options_map): | ||
228 | ''' | ||
229 | plugin "beancount_extras_kris7t.plugins.templates" | ||
230 | |||
231 | 2020-01-01 open Assets:Checking | ||
232 | 2020-01-01 open Expenses:Food | ||
233 | |||
234 | 2020-01-01 * "Eating out" | ||
235 | template: "eating-out" | ||
236 | Assets:Checking 25 USD | ||
237 | Expenses:Food -25 USD | ||
238 | |||
239 | 2020-03-15 custom "template-use" "eating-out" TRUE | ||
240 | ''' | ||
241 | self.assertRegex(errors[0].message, "^Invalid scale factor") | ||
242 | |||
243 | @loader.load_doc(expect_errors=True) | ||
244 | def test_use_zero_scale_factor(self, entries, errors, options_map): | ||
245 | ''' | ||
246 | plugin "beancount_extras_kris7t.plugins.templates" | ||
247 | |||
248 | 2020-01-01 open Assets:Checking | ||
249 | 2020-01-01 open Expenses:Food | ||
250 | |||
251 | 2020-01-01 * "Eating out" | ||
252 | template: "eating-out" | ||
253 | Assets:Checking 25 USD | ||
254 | Expenses:Food -25 USD | ||
255 | |||
256 | 2020-03-15 custom "template-use" "eating-out" 0.0 | ||
257 | ''' | ||
258 | self.assertRegex(errors[0].message, "^Scale factor must not be 0") | ||
259 | |||
260 | @loader.load_doc(expect_errors=True) | ||
261 | def test_template_delete(self, entries, errors, options_map): | ||
262 | ''' | ||
263 | plugin "beancount_extras_kris7t.plugins.templates" | ||
264 | |||
265 | 2020-01-01 open Assets:Checking | ||
266 | 2020-01-01 open Expenses:Food | ||
267 | |||
268 | 2020-01-01 * "Eating out" | ||
269 | template: "eating-out" | ||
270 | Assets:Checking 25 USD | ||
271 | Expenses:Food -25 USD | ||
272 | |||
273 | 2020-03-01 custom "template-delete" "eating-out" | ||
274 | |||
275 | 2020-03-15 custom "template-use" "eating-out" | ||
276 | ''' | ||
277 | self.assertRegex(errors[0].message, "^Unknown template") | ||
278 | |||
279 | @pytest.mark.xfail(reason="Empty custom directive fails in beancount.ops.pad") | ||
280 | @loader.load_doc(expect_errors=True) | ||
281 | def test_template_delete_too_few_arguments(self, entries, errors, options_map): | ||
282 | ''' | ||
283 | plugin "beancount_extras_kris7t.plugins.templates" | ||
284 | |||
285 | 2020-03-01 custom "template-delete" | ||
286 | ''' | ||
287 | self.assertRegex(errors[0].message, "^template-delete takes a single argument") | ||
288 | |||
289 | @loader.load_doc(expect_errors=True) | ||
290 | def test_template_delete_too_many_arguments(self, entries, errors, options_map): | ||
291 | ''' | ||
292 | plugin "beancount_extras_kris7t.plugins.templates" | ||
293 | |||
294 | 2020-01-01 open Assets:Checking | ||
295 | 2020-01-01 open Expenses:Food | ||
296 | |||
297 | 2020-01-01 * "Eating out" | ||
298 | template: "eating-out" | ||
299 | Assets:Checking 25 USD | ||
300 | Expenses:Food -25 USD | ||
301 | |||
302 | 2020-03-01 custom "template-delete" "eating-out" TRUE | ||
303 | ''' | ||
304 | self.assertRegex(errors[0].message, "^template-delete takes a single argument") | ||
305 | |||
306 | @loader.load_doc(expect_errors=True) | ||
307 | def test_template_delete_invalid_argument(self, entries, errors, options_map): | ||
308 | ''' | ||
309 | plugin "beancount_extras_kris7t.plugins.templates" | ||
310 | |||
311 | 2020-03-01 custom "template-delete" TRUE | ||
312 | ''' | ||
313 | self.assertRegex(errors[0].message, "^Template name must be a string") | ||
314 | |||
315 | @loader.load_doc(expect_errors=True) | ||
316 | def test_template_delete_unknown(self, entries, errors, options_map): | ||
317 | ''' | ||
318 | plugin "beancount_extras_kris7t.plugins.templates" | ||
319 | |||
320 | 2020-03-01 custom "template-delete" "taxi" | ||
321 | ''' | ||
322 | self.assertRegex(errors[0].message, "^Unknown template") | ||
323 | |||
324 | |||
325 | if __name__ == '__main__': | ||
326 | unittest.main() | ||