File: test_ledger.py

package info (click to toggle)
ledger-autosync 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: python: 2,711; makefile: 5
file content (125 lines) | stat: -rw-r--r-- 3,926 bytes parent folder | download | duplicates (2)
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
# Copyright (c) 2013-2021 Erik Hetzner
#
# This file is part of ledger-autosync
#
# ledger-autosync is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# ledger-autosync is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ledger-autosync. If not, see
# <http://www.gnu.org/licenses/>.


import os
import os.path
import tempfile

import pytest

from ledgerautosync.ledgerwrap import Ledger, LedgerPython


@pytest.mark.lgr_file("checking.lgr")
def test_check_transaction(ledger):
    assert ledger.check_transaction_by_id("ofxid", "1101.1452687~7.0000486")


@pytest.mark.lgr_file("checking.lgr")
def test_nonexistent_transaction(ledger):
    assert not ledger.check_transaction_by_id("ofxid", "FOO")


@pytest.mark.lgr_file("checking.lgr")
def test_empty_transaction(ledger):
    assert ledger.check_transaction_by_id("ofxid", "empty")


@pytest.mark.lgr_file("checking.lgr")
def test_get_account_by_payee(ledger):
    account = ledger.get_account_by_payee(
        "AUTOMATIC WITHDRAWAL, ELECTRIC BILL WEB(S )", exclude="Assets:Foo"
    )
    assert account == "Expenses:Bar"


@pytest.mark.lgr_file("checking-dynamic-account.lgr")
def test_get_ambiguous_account_by_payee(ledger):
    account = ledger.get_account_by_payee("Generic", exclude="Assets:Foo")
    # shoud use the latest
    assert account == "Expenses:Bar"


@pytest.mark.lgr_file("checking.lgr")
def test_ofx_payee_quoting(ledger):
    payees = [
        "PAYEE TEST/SLASH",
        "PAYEE TEST,COMMA",
        "PAYEE TEST:COLON",
        "PAYEE TEST*STAR",
        "PAYEE TEST#HASH",
        "PAYEE TEST.PERIOD",
    ]
    for payee in payees:
        assert (
            ledger.get_account_by_payee(payee, ["Assets:Foo"]) is not None
        ), "Did not find %s in %s" % (payee, ledger)


# TODO Broken on current hledger
@pytest.mark.lgr_file("checking.lgr")
@pytest.mark.ledger_impls([Ledger, LedgerPython])
def test_ofx_payee_quote_quote(ledger):
    payees = [
        'PAYEE TEST"QUOTE',
    ]
    for payee in payees:
        assert (
            ledger.get_account_by_payee(payee, ["Assets:Foo"]) is not None
        ), "Did not find %s in %s" % (payee, ledger)


@pytest.mark.lgr_file("checking.lgr")
def test_ofx_id_quoting(ledger):
    assert (
        ledger.check_transaction_by_id("ofxid", "1/2") is True
    ), "Did not find 1/2 in %s" % (ledger)


@pytest.mark.lgr_file("checking.lgr")
def test_load_payees(ledger):
    ledger.load_payees()
    assert ledger.payees["PAYEE TEST:COLON"] == ["Assets:Foo", "Income:Bar"]


@pytest.mark.lgr_file("empty.lgr")
def test_load_payees_with_empty_ledger(ledger):
    ledger.load_payees()


# class TestLedger(LedgerTest, TestCase):
#     def setUp(ledger):
#         self.empty_lgr = Ledger(os.path.join("fixtures", "empty.lgr"), no_pipe=True)
#         ledger = Ledger(self.ledger_path, no_pipe=True)
#         self.dynamic_lgr = Ledger(self.dynamic_ledger_path, no_pipe=True)


@pytest.mark.lgr_file("checking.lgr")
@pytest.mark.ledger_impls([Ledger])
def test_args_only(ledger):
    (f, tmprcpath) = tempfile.mkstemp(".ledgerrc")
    os.close(f)  # Who wants to deal with low-level file descriptors?
    # Create an init file that will narrow the test data to a period that
    # contains no trasnactions
    with open(tmprcpath, "w") as f:
        f.write("--period 2012")
    # If the command returns no trasnactions, as we would expect if we
    # parsed the init file, then this will throw an exception.
    next(ledger.run([""]))
    os.unlink(tmprcpath)