예제 #1
0
def unbook_postings(postings: List[Posting]) -> Posting:
    """Unbooks a list of postings back into a single posting.

    The combined units are computed, the cost and price are left unspecified.
    """
    if len(postings) == 1:
        return postings[0]
    number = sum((posting.units.number for posting in postings), ZERO)
    return postings[0]._replace(units=Amount(
        number=number, currency=postings[0].units.currency),
                                cost=CostSpec(number_per=None,
                                              number_total=None,
                                              currency=None,
                                              date=None,
                                              label=None,
                                              merge=None))
예제 #2
0
        serialised = loads(dumps(serialise(txn)))
        assert serialised == json_txn

        txn = txn._replace(payee=None)
        serialised = loads(dumps(serialise(txn)))
        assert serialised == json_txn


@pytest.mark.parametrize(
    "amount_cost_price,amount_string",
    [
        ((A("100 USD"), None, None), "100 USD"),
        (
            (
                A("100 USD"),
                CostSpec(D("10"), None, "EUR", None, None, False),
                None,
            ),
            "100 USD {10 EUR}",
        ),
        (
            (
                A("100 USD"),
                CostSpec(D("10"), None, "EUR", None, None, False),
                A("11 EUR"),
            ),
            "100 USD {10 EUR} @ 11 EUR",
        ),
        ((A("100 USD"), None, A("11 EUR")), "100 USD @ 11 EUR"),
        (
            (
예제 #3
0
        ),
        (
            (
                A("100 USD"),
                Cost(D("10"), "EUR", None, None),
                A("11 EUR"),
                None,
                None,
            ),
            "100 USD {10 EUR} @ 11 EUR",
        ),
        ((A("100 USD"), None, A("11 EUR"), None, None), "100 USD @ 11 EUR"),
        (
            (
                A("100 USD"),
                CostSpec(MISSING, None, MISSING, None, None, False),
                None,
                None,
                None,
            ),
            "100 USD {}",
        ),
    ],
)
def test_serialise_posting(pos, amount):
    pos = Posting("Assets:ETrade:Cash", *pos)
    json = {"account": "Assets:ETrade:Cash", "amount": amount}
    assert loads(dumps(serialise(pos))) == json
    assert deserialise_posting(json) == pos

예제 #4
0
        ],
    }

    with app.test_request_context():
        serialised = loads(dumps(serialise(txn)))
    assert serialised == json_txn


@pytest.mark.parametrize('pos,amount', [
    ((A('100 USD'), None, None, None, None), '100 USD'),
    ((A('100 USD'), Cost(D('10'), 'EUR', None,
                         None), None, None, None), '100 USD {10 EUR}'),
    ((A('100 USD'), Cost(D('10'), 'EUR', None, None), A('11 EUR'), None, None),
     '100 USD {10 EUR} @ 11 EUR'),
    ((A('100 USD'), None, A('11 EUR'), None, None), '100 USD @ 11 EUR'),
    ((A('100 USD'), CostSpec(MISSING, None, MISSING, None, None,
                             False), None, None, None), '100 USD {}'),
])
def test_serialise_posting(pos, amount):
    pos = Posting('Assets:ETrade:Cash', *pos)
    json = {
        'account': 'Assets:ETrade:Cash',
        'amount': amount,
    }
    assert loads(dumps(serialise(pos))) == json
    assert deserialise_posting(json) == pos


def test_deserialise():
    postings = [
        {
            'account': 'Assets:ETrade:Cash',