Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
path = app.config["BEANCOUNT_FILES"][0]
payload = open(path, encoding="utf-8").read()
with app.test_request_context():
app.preprocess_request()
url = flask.url_for("json_api.format_source")
old_currency_column = flask.g.ledger.fava_options["currency-column"]
flask.g.ledger.fava_options["currency-column"] = 90
result = test_client.put(
url,
data=flask.json.dumps({"source": payload}),
content_type="application/json",
)
data = flask.json.loads(result.get_data(True))
assert data == {
"data": align(payload, {"currency-column": 90}),
"success": True,
}
flask.g.ledger.fava_options["currency-column"] = old_currency_column
def test_api_format_source(app, test_client):
with app.test_request_context():
app.preprocess_request()
url = flask.url_for("json_api.format_source")
path = app.config["BEANCOUNT_FILES"][0]
payload = open(path, encoding="utf-8").read()
result = test_client.put(
url,
data=flask.json.dumps({"source": payload}),
content_type="application/json",
)
data = flask.json.loads(result.get_data(True))
assert data == {"data": align(payload, {}), "success": True}
def _format_entry(entry, fava_options):
"""Wrapper that strips unnecessary whitespace from format_entry."""
meta = {
key: entry.meta[key] for key in entry.meta if not key.startswith("_")
}
entry = entry._replace(meta=meta)
string = align(format_entry(entry), fava_options)
string = string.replace("", "")
return "\n".join((line.rstrip() for line in string.split("\n")))
def format_source(request_data):
"""Format beancount file."""
return align(request_data["source"], g.ledger.fava_options)