61 lines
1.9 KiB
Ruby
61 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "bigdecimal"
|
|
|
|
RSpec.describe Hledger::Equity do
|
|
def part(commodity, quantity)
|
|
{ "commodity" => commodity, "quantity" => BigDecimal(quantity), "style" => {} }
|
|
end
|
|
|
|
it "builds a per-commodity tree with each row's share" do
|
|
groups =
|
|
described_class.groups(
|
|
[["equity:alice", [part("EUR", "600")]], ["equity:bob", [part("EUR", "400")]]],
|
|
)
|
|
|
|
group = groups.first
|
|
expect(group["commodity"]).to eq("EUR")
|
|
expect(group["total"]["quantity"].to_s("F")).to eq("1000.0")
|
|
expect(group["rows"].map { |row| row["label"] }).to eq(%w[Alice Bob])
|
|
expect(group["rows"].map { |row| row["depth"] }).to eq([0, 0])
|
|
expect(group["rows"].map { |row| row["percentage"] }).to eq(%w[60.00 40.00])
|
|
end
|
|
|
|
it "nests subaccounts and shares the commodity total" do
|
|
groups =
|
|
described_class.groups(
|
|
[
|
|
["equity:alice:initial", [part("EUR", "600")]],
|
|
["equity:alice:extra", [part("EUR", "100")]],
|
|
["equity:bob", [part("EUR", "300")]],
|
|
],
|
|
)
|
|
|
|
rows = groups.first["rows"]
|
|
expect(rows.map { |row| [row["label"], row["depth"]] }).to eq(
|
|
[["Alice", 0], ["Initial", 1], ["Extra", 1], ["Bob", 0]],
|
|
)
|
|
expect(rows.map { |row| row["percentage"] }).to eq(%w[70.00 60.00 10.00 30.00])
|
|
end
|
|
|
|
it "keeps commodities separate" do
|
|
groups =
|
|
described_class.groups(
|
|
[
|
|
["equity:alice", [part("EUR", "100"), part("USD", "50")]],
|
|
["equity:bob", [part("USD", "50")]],
|
|
],
|
|
)
|
|
|
|
expect(groups.map { |group| group["commodity"] }).to eq(%w[EUR USD])
|
|
usd = groups.find { |group| group["commodity"] == "USD" }
|
|
expect(usd["rows"].map { |row| row["percentage"] }).to eq(%w[50.00 50.00])
|
|
end
|
|
|
|
it "ignores accounts outside of equity" do
|
|
groups = described_class.groups([["assets:bank", [part("EUR", "100")]]])
|
|
|
|
expect(groups).to eq([])
|
|
end
|
|
end
|