Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_add_inventory():
inv = CounterInventory()
inv2 = CounterInventory()
inv3 = CounterInventory()
inv.add_amount(A("10 USD"))
inv2.add_amount(A("30 USD"))
inv3.add_amount(A("-40 USD"))
inv.add_inventory(inv2)
assert len(inv) == 1
inv.add_inventory(inv3)
assert inv.is_empty()
inv = CounterInventory()
inv.add_inventory(inv2)
assert len(inv) == 1
def test_add_inventory():
inv = CounterInventory()
inv2 = CounterInventory()
inv3 = CounterInventory()
inv.add_amount(A("10 USD"))
inv2.add_amount(A("30 USD"))
inv3.add_amount(A("-40 USD"))
inv.add_inventory(inv2)
assert len(inv) == 1
inv.add_inventory(inv3)
assert inv.is_empty()
inv = CounterInventory()
inv.add_inventory(inv2)
assert len(inv) == 1
def test_add_amount():
inv = CounterInventory()
inv.add_amount(A("10 USD"))
inv.add_amount(A("30 USD"))
assert len(inv) == 1
inv.add_amount(A("-40 USD"))
assert inv.is_empty()
inv.add_amount(A("10 USD"))
inv.add_amount(A("20 CAD"))
inv.add_amount(A("10 USD"))
assert len(inv) == 2
inv.add_amount(A("-20 CAD"))
assert len(inv) == 1
def test_should_show(app):
with app.test_request_context("/"):
app.preprocess_request()
assert should_show(g.ledger.root_tree.get("")) is True
assert should_show(g.ledger.root_tree.get("Expenses")) is True
account = TreeNode("name")
assert should_show(account) is False
account.balance_children = CounterInventory({("USD", None): 9})
assert should_show(account) is True
with app.test_request_context("/?time=2100"):
app.preprocess_request()
assert not g.ledger.fava_options["show-accounts-with-zero-balance"]
assert should_show(g.ledger.root_tree.get("")) is True
assert should_show(g.ledger.root_tree.get("Expenses")) is False
def __init__(self, name):
#: str: Account name.
self.name = name
#: A list of :class:`.TreeNode`, its children.
self.children = []
#: :class:`.CounterInventory`: The cumulative account balance.
self.balance_children = CounterInventory()
#: :class:`.CounterInventory`: The account balance.
self.balance = CounterInventory()
#: bool: True if the account has any transactions.
self.has_txns = False
transactions = (
entry
for entry in self.ledger.entries
if (
isinstance(entry, Transaction)
and entry.flag != flags.FLAG_UNREALIZED
)
)
types = (
self.ledger.options["name_assets"],
self.ledger.options["name_liabilities"],
)
txn = next(transactions, None)
inventory = CounterInventory()
for end_date_exclusive in self.ledger.interval_ends(interval):
end_date_inclusive = end_date_exclusive - datetime.timedelta(
days=1
)
while txn and txn.date < end_date_exclusive:
for posting in filter(
lambda p: p.account.startswith(types), txn.postings
):
inventory.add_position(posting)
txn = next(transactions, None)
yield {
"date": end_date_exclusive,
"balance": cost_or_value(inventory, end_date_inclusive),
}
def cap(self, options, unrealized_account):
"""Transfer Income and Expenses, add conversions and unrealized gains.
Args:
options: The Beancount options.
unrealized_account: The name of the account to post unrealized
gains to (as a subaccount of Equity).
"""
equity = options["name_equity"]
conversions = CounterInventory(
{
(currency, None): -number
for currency, number in self.get("")
.balance_children.reduce(convert.get_cost)
.items()
}
)
# Add conversions
self.insert(
equity + ":" + options["account_current_conversions"], conversions
)
# Insert unrealized gains.
self.insert(
equity + ":" + unrealized_account, -self.get("").balance_children
def __add__(self, other):
counter = CounterInventory(self)
counter.add_inventory(other)
return counter
def __init__(self, name):
#: str: Account name.
self.name = name
#: A list of :class:`.TreeNode`, its children.
self.children = []
#: :class:`.CounterInventory`: The cumulative account balance.
self.balance_children = CounterInventory()
#: :class:`.CounterInventory`: The account balance.
self.balance = CounterInventory()
#: bool: True if the account has any transactions.
self.has_txns = False
def __neg__(self):
return CounterInventory({key: -num for key, num in self.items()})