aboutsummaryrefslogtreecommitdiffstats
path: root/beancount_extras_kris7t/plugins/templates_test.py
blob: 5f63e6c1143515bb897a095a0073ba97ecd5ff3b (plain) (blame)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
__copyright__ = 'Copyright (c) 2020  Kristóf Marussy <kristof@marussy.com>'
__license__ = 'GNU GPLv2'

import unittest

from beancount import loader
from beancount.parser import cmptest
import pytest


class TestClosingBalance(cmptest.TestCase):

    @loader.load_doc()
    def test_use_template_simple(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out" #tag ^link
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-15 custom "template-use" "eating-out"

        2020-04-12 custom "template-use" "eating-out"
        '''
        self.assertEqualEntries('''
        plugin "beancount_extras_kris7t.plugins.closing_balance"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-03-15 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-04-12 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD
        ''', entries)

    @loader.load_doc()
    def test_use_template_metadata(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out" #tag ^link
          template: "eating-out"
          meta1: "data"
          meta2: TRUE
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-15 custom "template-use" "eating-out"
          meta1: "foo"
          meta3: 3.14
        '''
        self.assertEqualEntries('''
        plugin "beancount_extras_kris7t.plugins.closing_balance"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-03-15 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          meta1: "foo"
          meta2: TRUE
          meta3: 2.14
          Assets:Checking  25 USD
          Expenses:Food  -25 USD
        ''', entries)

    @loader.load_doc()
    def test_use_template_scaled(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out" #tag ^link
          template: "eating-out"
          Assets:Checking  1 USD
          Expenses:Food  -1 USD

        2020-03-15 custom "template-use" "eating-out" 25

        2020-04-12 custom "template-use" "eating-out" 27
        '''
        self.assertEqualEntries('''
        plugin "beancount_extras_kris7t.plugins.closing_balance"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-03-15 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-04-12 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          Assets:Checking  27 USD
          Expenses:Food  -27 USD
        ''', entries)

    @loader.load_doc()
    def test_use_template_overwritten(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food
        2020-01-01 open Expenses:Tax

        2020-04-01 * "Eating out" #tag ^link
          template: "eating-out"
          Assets:Checking  1.10 USD
          Expenses:Food  -1 USD
          Expenses:Tax  -0.10 USD

        2020-01-01 * "Eating out" #tag ^link
          template: "eating-out"
          Assets:Checking  1 USD
          Expenses:Food  -1 USD

        2020-03-15 custom "template-use" "eating-out" 25

        2020-04-12 custom "template-use" "eating-out" 27
        '''
        self.assertEqualEntries('''
        plugin "beancount_extras_kris7t.plugins.closing_balance"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food
        2020-01-01 open Expenses:Tax

        2020-03-15 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-04-12 * "Eating out" #tag ^link ^template_eating-out
          template: "eating-out"
          Assets:Checking  29.70 USD
          Expenses:Food  -27 USD
          Expenses:Tax  -2.70 USD
        ''', entries)

    @loader.load_doc(expect_errors=True)
    def test_invalid_name(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: TRUE
          Assets:Checking  25 USD
          Expenses:Food  -25 USD
        '''
        self.assertRegex(errors[0].message, "^template must be a string")

    @pytest.mark.xfail(reason="Empty custom directive fails in beancount.ops.pad")
    @loader.load_doc(expect_errors=True)
    def test_use_missing_name(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-15 custom "template-use"
        '''
        self.assertRegex(errors[0].message, "^Template name missing")

    @loader.load_doc(expect_errors=True)
    def test_use_too_many_arguments(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-15 custom "template-use" "eating-out" 2 "another"
        '''
        self.assertRegex(errors[0].message, "^Too many template-use arguments")

    @loader.load_doc(expect_errors=True)
    def test_use_invalid_name(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-03-15 custom "template-use" TRUE
        '''
        self.assertRegex(errors[0].message, "^Template name must be a string")

    @loader.load_doc(expect_errors=True)
    def test_use_unknown(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-03-15 custom "template-use" "taxi"
        '''
        self.assertRegex(errors[0].message, "^Unknown template")

    @loader.load_doc(expect_errors=True)
    def test_use_invalid_scale_factor(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-15 custom "template-use" "eating-out" TRUE
        '''
        self.assertRegex(errors[0].message, "^Invalid scale factor")

    @loader.load_doc(expect_errors=True)
    def test_use_zero_scale_factor(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-15 custom "template-use" "eating-out" 0.0
        '''
        self.assertRegex(errors[0].message, "^Scale factor must not be 0")

    @loader.load_doc(expect_errors=True)
    def test_template_delete(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-01 custom "template-delete" "eating-out"

        2020-03-15 custom "template-use" "eating-out"
        '''
        self.assertRegex(errors[0].message, "^Unknown template")

    @pytest.mark.xfail(reason="Empty custom directive fails in beancount.ops.pad")
    @loader.load_doc(expect_errors=True)
    def test_template_delete_too_few_arguments(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-03-01 custom "template-delete"
        '''
        self.assertRegex(errors[0].message, "^template-delete takes a single argument")

    @loader.load_doc(expect_errors=True)
    def test_template_delete_too_many_arguments(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-01-01 open Assets:Checking
        2020-01-01 open Expenses:Food

        2020-01-01 * "Eating out"
          template: "eating-out"
          Assets:Checking  25 USD
          Expenses:Food  -25 USD

        2020-03-01 custom "template-delete" "eating-out" TRUE
        '''
        self.assertRegex(errors[0].message, "^template-delete takes a single argument")

    @loader.load_doc(expect_errors=True)
    def test_template_delete_invalid_argument(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-03-01 custom "template-delete" TRUE
        '''
        self.assertRegex(errors[0].message, "^Template name must be a string")

    @loader.load_doc(expect_errors=True)
    def test_template_delete_unknown(self, entries, errors, options_map):
        '''
        plugin "beancount_extras_kris7t.plugins.templates"

        2020-03-01 custom "template-delete" "taxi"
        '''
        self.assertRegex(errors[0].message, "^Unknown template")


if __name__ == '__main__':
    unittest.main()