Beispiel #1
0
def test_serialise() -> None:
    assert serialise(None) is None
    txn = Transaction(
        {},
        datetime.date(2017, 12, 12),
        "*",
        "Test3",
        "asdfasd",
        frozenset(["tag"]),
        frozenset(["link"]),
        [],
    )
    create_simple_posting(txn, "Assets:ETrade:Cash", "100", "USD")
    create_simple_posting(txn, "Assets:ETrade:GLD", None, None)

    json_txn = {
        "date":
        "2017-12-12",
        "flag":
        "*",
        "meta": {},
        "narration":
        "asdfasd #tag ^link",
        "payee":
        "Test3",
        "type":
        "Transaction",
        "postings": [
            {
                "account": "Assets:ETrade:Cash",
                "amount": "100 USD"
            },
            {
                "account": "Assets:ETrade:GLD",
                "amount": ""
            },
        ],
    }

    serialised = loads(dumps(serialise(txn)))
    assert serialised == json_txn

    txn = txn._replace(payee="")
    json_txn["payee"] = ""
    serialised = loads(dumps(serialise(txn)))
    assert serialised == json_txn

    txn = txn._replace(payee=None)
    serialised = loads(dumps(serialise(txn)))
    assert serialised == json_txn
Beispiel #2
0
def test_serialise_balance() -> None:
    bal = Balance(
        {},
        datetime.date(2019, 9, 17),
        "Assets:ETrade:Cash",
        A("0.1234567891011121314151617 CHF"),
        None,
        None,
    )

    json = {
        "date": "2019-09-17",
        "amount": {
            "currency": "CHF",
            "number": "0.1234567891011121314151617"
        },
        "diff_amount": None,
        "meta": {},
        "tolerance": None,
        "account": "Assets:ETrade:Cash",
        "type": "Balance",
    }

    serialised = loads(dumps(serialise(bal)))

    assert serialised == json
Beispiel #3
0
def test_api_source_put(app, test_client) -> None:
    with app.test_request_context("/long-example/"):
        app.preprocess_request()
        url = url_for("json_api.source")
        path = g.ledger.beancount_file_path

    # test bad request
    response = test_client.put(url)
    assert_api_error(response, "Invalid JSON request.")

    with open(path, encoding="utf-8") as file_handle:
        payload = file_handle.read()
    with open(path, mode="rb") as bfile_handle:
        sha256sum = hashlib.sha256(bfile_handle.read()).hexdigest()

    # change source
    response = test_client.put(
        url,
        data=dumps(
            {
                "source": "asdf" + payload,
                "sha256sum": sha256sum,
                "file_path": path,
            }
        ),
        content_type="application/json",
    )
    with open(path, mode="rb") as bfile_handle:
        sha256sum = hashlib.sha256(bfile_handle.read()).hexdigest()
    assert_api_success(response, sha256sum)

    # check if the file has been written
    with open(path, encoding="utf-8") as file_handle:
        assert file_handle.read() == "asdf" + payload

    # write original source file
    result = test_client.put(
        url,
        data=dumps(
            {"source": payload, "sha256sum": sha256sum, "file_path": path}
        ),
        content_type="application/json",
    )
    assert result.status_code == 200
    with open(path, encoding="utf-8") as file_handle:
        assert file_handle.read() == payload
Beispiel #4
0
def test_serialise_posting(
    amount_cost_price: Tuple[Amount, Optional[CostSpec], Amount],
    amount_string: str,
) -> None:
    pos = Posting("Assets", *amount_cost_price, None, None)
    json = {"account": "Assets", "amount": amount_string}
    assert loads(dumps(serialise(pos))) == json
    assert deserialise_posting(json) == pos
Beispiel #5
0
def test_interval_totals_inverted(small_example_ledger: FavaLedger,
                                  snapshot) -> None:
    for conversion in ["at_cost", "USD"]:
        data = small_example_ledger.charts.interval_totals(Interval.MONTH,
                                                           "Expenses",
                                                           conversion,
                                                           invert=True)
        snapshot(data)
        snapshot(dumps(data))
Beispiel #6
0
def test_api_format_source(app, test_client) -> None:
    with app.test_request_context("/long-example/"):
        app.preprocess_request()
        url = flask.url_for("json_api.format_source")
        path = flask.g.ledger.beancount_file_path

    payload = open(path, encoding="utf-8").read()

    response = test_client.put(
        url,
        data=dumps({"source": payload}),
        content_type="application/json",
    )
    assert_api_success(response, align(payload, 61))
Beispiel #7
0
def test_api_format_source_options(app, test_client, monkeypatch) -> None:
    with app.test_request_context("/long-example/"):
        app.preprocess_request()
        path = g.ledger.beancount_file_path
        payload = open(path, encoding="utf-8").read()

        url = url_for("json_api.format_source")

        monkeypatch.setitem(g.ledger.fava_options, "currency-column", 90)

        response = test_client.put(
            url,
            data=dumps({"source": payload}),
            content_type="application/json",
        )
        assert_api_success(response, align(payload, 90))
Beispiel #8
0
def test_api_add_entries(app, test_client, tmp_path):
    with app.test_request_context("/long-example/"):
        app.preprocess_request()
        old_beancount_file = flask.g.ledger.beancount_file_path
        test_file = tmp_path / "test_file"
        test_file.open("a")
        flask.g.ledger.beancount_file_path = str(test_file)

        data = {
            "entries": [
                {
                    "type":
                    "Transaction",
                    "date":
                    "2017-12-12",
                    "flag":
                    "*",
                    "payee":
                    "Test3",
                    "narration":
                    "",
                    "meta": {},
                    "postings": [
                        {
                            "account": "Assets:US:ETrade:Cash",
                            "amount": "100 USD",
                        },
                        {
                            "account": "Assets:US:ETrade:GLD"
                        },
                    ],
                },
                {
                    "type":
                    "Transaction",
                    "date":
                    "2017-01-12",
                    "flag":
                    "*",
                    "payee":
                    "Test1",
                    "narration":
                    "",
                    "meta": {},
                    "postings": [
                        {
                            "account": "Assets:US:ETrade:Cash",
                            "amount": "100 USD",
                        },
                        {
                            "account": "Assets:US:ETrade:GLD"
                        },
                    ],
                },
                {
                    "type":
                    "Transaction",
                    "date":
                    "2017-02-12",
                    "flag":
                    "*",
                    "payee":
                    "Test",
                    "narration":
                    "Test",
                    "meta": {},
                    "postings": [
                        {
                            "account": "Assets:US:ETrade:Cash",
                            "amount": "100 USD",
                        },
                        {
                            "account": "Assets:US:ETrade:GLD"
                        },
                    ],
                },
            ]
        }
        url = flask.url_for("json_api.add_entries")

        response = test_client.put(url,
                                   data=dumps(data),
                                   content_type="application/json")
        assert_api_success(response, "Stored 3 entries.")

        assert (test_file.read_text("utf-8") == """
2017-01-12 * "Test1" ""
  Assets:US:ETrade:Cash                                 100 USD
  Assets:US:ETrade:GLD

2017-02-12 * "Test" "Test"
  Assets:US:ETrade:Cash                                 100 USD
  Assets:US:ETrade:GLD

2017-12-12 * "Test3" ""
  Assets:US:ETrade:Cash                                 100 USD
  Assets:US:ETrade:GLD
""")

        flask.g.ledger.beancount_file_path = old_beancount_file
Beispiel #9
0
def test_net_worth(example_ledger: FavaLedger, snapshot) -> None:
    data = example_ledger.charts.net_worth(Interval.MONTH, "USD")
    snapshot(data)
    snapshot(dumps(data))
Beispiel #10
0
def test_linechart_data(example_ledger: FavaLedger, snapshot) -> None:
    for conversion in ["at_cost", "units", "at_value", "USD"]:
        data = example_ledger.charts.linechart(
            "Assets:Testing:MultipleCommodities", conversion)
        snapshot(data)
        snapshot(dumps(data))