示例#1
0
    def test_custom_cost(self):

        # Unbooked capital gains.
        lgr = ledger_str(self.def_accounts + """

2008-01-10 * Bought some shares.
  Assets:Broker              10 AAPL @ 120.00 USD
  Assets:Bank

2008-01-10 * Sold some shares.
  Assets:Broker              -10 AAPL @ 125.00 USD
  Assets:Bank

""", 'unbooked-gains')
        assert len(lgr.transactions) == 2
        assert lgr.transactions[0].postings[1].amount == Wallet('-1200 USD')
        assert lgr.transactions[1].postings[1].amount == Wallet('1250 USD')


        # Forgotten capital gains.
        lgr = ledger_str(self.def_accounts + """

2008-01-10 * Bought some shares.
  Assets:Broker              10 AAPL @ 120.00 USD
  Assets:Bank

2008-01-11 * Sold some shares.
  Assets:Broker              -10 AAPL {120.00 USD} @ 125.00 USD
  Assets:Bank

""", 'forgot-gains')
        assert len(lgr.transactions) == 2
        assert lgr.transactions[0].postings[1].amount == Wallet('-1200 USD')
        assert lgr.transactions[1].postings[1].amount == Wallet('1200 USD')
示例#2
0
    def test_with_commissions(self):

        # Booked capital gains.
        lgr = ledger_str(self.def_accounts + """

2008-01-10 * Bought some shares.
  Assets:Broker              10 AAPL @ 120.00 USD
  Expenses:Commissions      9.95 USD
  Assets:Bank          -1209.95 USD

2008-01-11 * Sold some shares.
  Assets:Broker              -10 AAPL {120.00 USD} @ 125.00 USD
  Assets:Bank            1240.05 USD      ;; actual amount deposited (easy to find on statement)
  Expenses:Commissions      9.95 USD      ;; actual commission for closing the trade
  Income:CapitalGains                     ;; automatically computed gain (from share cost above)
  [Income:CapitalGains]       19.90 USD   ;; offset for commissions to open and close this trade, manually entered
  [Expenses:Deductible-Costs]             ;; an account that track costs for closed trades

""", 'booked-gains')
        assert len(lgr.transactions) == 2

        commisions = 2*Decimal('9.95')
        for accname, amount in (
            ('Assets:Bank', Decimal('50')-commisions),
            ('Expenses:Commissions', commisions),
            ('Income:CapitalGains', -(Decimal('50')-commisions)),
            ):
            assert (lgr.get_account(accname).balances['total'] ==
                    Wallet('USD', amount))
示例#3
0
    def test_price(self):

        # Normal price.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Broker               10 AAPL @ 121.00 USD
  Assert:Checking

""",
            "normal-price",
        )

        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert txn.postings[0].amount == Wallet("10 AAPL")
        assert txn.postings[0].price == Wallet("121 USD")
        assert txn.postings[0].cost == Wallet("1210 USD")
        assert lgr.get_account("Assets:Broker").total == Wallet("10 AAPL")
        assert lgr.get_account("Assert:Checking").total == Wallet("-1210 USD")

        # Total price.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Broker               10 AAPL @@ 1210.00 USD
  Assert:Checking

""",
            "total-price",
        )

        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert txn.postings[0].amount == Wallet("10 AAPL")
        assert txn.postings[0].price == Wallet("121 USD")
        assert txn.postings[0].cost == Wallet("1210 USD")
        assert lgr.get_account("Assets:Broker").total == Wallet("10 AAPL")
        assert lgr.get_account("Assert:Checking").total == Wallet("-1210 USD")
示例#4
0
    def test_explicit_cost(self):

        # Cost-per-share.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Broker               10 AAPL {111.00 USD} @ 121.00 USD
  Assert:Checking

""",
            "cost-per-share",
        )

        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert txn.postings[0].amount == Wallet("10 AAPL")
        assert txn.postings[0].cost == Wallet("1110 USD")
        assert lgr.get_account("Assets:Broker").total == Wallet("10 AAPL")
        assert lgr.get_account("Assert:Checking").total == Wallet("-1110 USD")

        # Total cost.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Broker               10 AAPL {{1110.00 USD}} @ 121.00 USD
  Assert:Checking

""",
            "cost-per-share",
        )

        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert txn.postings[0].amount == Wallet("10 AAPL")
        assert txn.postings[0].cost == Wallet("1110 USD")
        assert lgr.get_account("Assets:Broker").total == Wallet("10 AAPL")
        assert lgr.get_account("Assert:Checking").total == Wallet("-1110 USD")
示例#5
0
    def test_manycomm(self):
        "Test padding in the presence of many commodities."

        lgr = ledger_str(self.def_accounts + """
@pad 2008-01-01  Assets:Bag  Equity:Opening-Balances

@check 2008-04-02  Assets:Bag    1 CAD
@check 2008-04-02  Assets:Bag    2 USD
@check 2008-04-02  Assets:Bag    3 AAPL
""", 'manycomm')
        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        w = Wallet('1 CAD, 2 USD, 3 AAPL')
        assert txn.postings[0].amount == w
        assert txn.postings[1].amount == -w
示例#6
0
    def test_custom_cost2(self):

        # Booked capital gains.
        lgr = ledger_str(self.def_accounts + """

2008-01-10 * Bought some shares.
  Assets:Broker              10 AAPL @ 120.00 USD
  Assets:Bank          -1200 USD

2008-01-11 * Sold some shares.
  Assets:Broker              -10 AAPL {120.00 USD} @ 125.00 USD
  Assets:Bank           1250 USD
  Income:CapitalGains      -50 USD

""", 'booked-gains')
        assert len(lgr.transactions) == 2
        assert lgr.transactions[1].postings[0].cost == Wallet('-1200 USD')
        assert lgr.transactions[1].postings[1].amount == Wallet('1250 USD')
        assert lgr.transactions[1].postings[2].amount == Wallet('-50 USD')
示例#7
0
    def test_truestory(self):

        # Some other examples.
        lgr = ledger_str(
            self.def_accounts
            + """

2007-12-19 * Redemption
  Assets:Broker                 -29.4650 "AIM681" {{-326.62 CAD}} @ 11.197 CAD
  Assets:Checking                    329.92 CAD   ; adjusted cost base
  Income:CapGains  

""",
            "true-story-1",
        )

        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert lgr.get_account("Income:CapGains").total == Wallet("-3.30 CAD")
示例#8
0
    def test_padding(self):

        # Testing padding before the first check.
        lgr = ledger_str(self.def_accounts + """
@pad 2008-04-10  Assets:Bag  Equity:Opening-Balances

2008-04-15 * Misc
  Assets:Bag             10.00 USD
  Equity:Opening-Balances

@check 2008-04-20  Assets:Bag  1203.23 USD
        """, 'before')
        assert len(lgr.transactions) == 2
        txn = filter(lambda x: x.flag == 'A', lgr.transactions)[0]
        assert txn.actual_date == date(2008, 4, 10)
        assert txn.postings[0].amount == Wallet('1193.23 USD')


        # Testing padding between checks before a transaction.
        lgr = ledger_str(self.def_accounts + """
2008-04-01 * Misc
  Assets:Bag             43.00 USD
  Equity:Opening-Balances

@check 2008-04-20  Assets:Bag  43.00 USD

@pad 2008-04-22  Assets:Bag  Equity:Opening-Balances

2008-04-23 * Misc
  Assets:Bag             10.00 USD
  Equity:Opening-Balances

@check 2008-04-25  Assets:Bag  64.00 USD
""", 'between1')
        assert len(lgr.transactions) == 3
        txn = filter(lambda x: x.flag == 'A', lgr.transactions)[0]
        assert txn.actual_date == date(2008, 4, 22)
        ## print txn
        assert txn.postings[0].amount == Wallet('11.00 USD')



        # Test padding between checks between transactions.
        lgr = ledger_str(self.def_accounts + """
2008-04-01 * Misc
  Assets:Bag             21.00 USD
  Equity:Opening-Balances

@check 2008-04-20  Assets:Bag  21.00 USD

2008-04-21 * Misc
  Assets:Bag             10.00 USD
  Equity:Opening-Balances

@pad 2008-04-22  Assets:Bag  Equity:Opening-Balances

2008-04-23 * Misc
  Assets:Bag             10.00 USD
  Equity:Opening-Balances

@check 2008-04-24  Assets:Bag  53.01 USD
""", 'between2')
        assert len(lgr.transactions) == 4
        txn = filter(lambda x: x.flag == 'A', lgr.transactions)[0]
        assert txn.actual_date == date(2008, 4, 22)
        assert txn.postings[0].amount == Wallet('12.01 USD')


        # Test padding between checks after transactions.
        lgr = ledger_str(self.def_accounts + """

2008-04-01 * Misc
  Assets:Bag             43.00 USD
  Equity:Opening-Balances

@check 2008-04-20  Assets:Bag  43.00 USD

2008-04-23 * Misc
  Assets:Bag             10.00 USD
  Equity:Opening-Balances

@pad 2008-04-22  Assets:Bag  Equity:Opening-Balances

@check 2008-04-25  Assets:Bag  64.02 USD
""", 'between3')
        assert len(lgr.transactions) == 3
        txn = filter(lambda x: x.flag == 'A', lgr.transactions)[0]
        assert txn.actual_date == date(2008, 4, 22)
        assert txn.postings[0].amount == Wallet('11.02 USD')


        # Test padding between after checks (should fail).
        lgr = ledger_str(self.def_accounts + """
2008-01-01 * Misc
  Assets:Bag             17.00 USD
  Equity:Opening-Balances

@check 2008-04-20  Assets:Bag  17.00 USD

@pad 2008-04-21  Assets:Bag  Equity:Opening-Balances
""", 'after')
        assert len(lgr.transactions) == 1


        # Test padding for an empty amount: should be no padding at all.
        lgr = ledger_str(self.def_accounts + """
2008-01-01 * Misc
  Assets:Bag             43.00 USD
  Equity:Opening-Balances

@check 2008-04-20  Assets:Bag  43.00 USD

@pad 2008-04-22  Assets:Bag  Equity:Opening-Balances

2008-04-23 * Misc
  Assets:Bag             10.00 USD
  Equity:Opening-Balances

@check 2008-04-25  Assets:Bag  53.00 USD
""", 'empty1')
        assert len(lgr.transactions) == 2
        assert not filter(lambda x: x.flag == 'A', lgr.transactions)


        lgr = ledger_str(self.def_accounts + """
@pad 2008-04-12  Assets:Bag  Equity:Opening-Balances
@check 2008-04-20  Assets:Bag  0.00 USD
""", 'empty2')
        assert len(lgr.transactions) == 0
示例#9
0
    def test_simple(self):

        # Simple transaction.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Bank               100.00 USD
  Income:Salary

""",
            "simple",
        )

        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert txn.postings[0].amount == Wallet("100 USD")
        assert lgr.get_account("Assets:Bank").balances["total"] == Wallet("100 USD")
        assert lgr.get_account("Income:Salary").balance["total"] == Wallet("-100 USD")

        # Empty unbalanced virtual posting should fail.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Bank               100.00 USD
  Income:Salary            -100.00 USD
  (Expenses:TripPlan)

""",
            "virtempty",
        )
        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert len(txn.postings) == 3
        assert txn.postings[2].amount == Wallet()
        ## FIXME: how do we assert that there was a warning here?

        # Normal and virtual postings should balance independently.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * Bought some shares
  Assets:Bank              170.00 USD
  Income:Salary
  [Income:Planning]         42.00 USD
  [Expenses:TripPlan]

""",
            "twoempty",
        )
        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]
        assert len(txn.postings) == 4
        for i, amt in enumerate(("170 USD", "-170 USD", "42 USD", "-42 USD")):
            assert txn.postings[i].amount == Wallet(amt)

        # Normal and virtual postings should balance independently.
        lgr = ledger_str(
            self.def_accounts
            + """

2008-01-10 * 
  Assets:Bank              170.00 USD
  Income:Salary            169.00 USD

""",
            "twoempty",
        )
        assert len(lgr.transactions) == 1
        txn = lgr.transactions[0]