Files
discourse-hledger/spec/lib/hledger/integration_spec.rb
T

88 lines
3.2 KiB
Ruby

# frozen_string_literal: true
# rubocop:disable RSpec/DescribeClass
RSpec.describe "Hledger integration" do
before { skip "hledger is not installed" if !Hledger::Runner.new.available? }
let(:journal) { File.read(Rails.root.join("plugins/hledger/spec/fixtures/journal.txt")) }
let(:builder) { Hledger::ReportBuilder.new }
it "builds every supported report" do
%w[accounts balance_sheet income_statement equity transactions].each do |type|
expect(builder.build(type, journal: journal)["report_type"]).to eq(type)
end
end
it "treats the end date as inclusive" do
report =
builder.build(
"income_statement",
journal: journal,
begin_date: Date.new(2024, 1, 1),
end_date: Date.new(2024, 3, 15),
)
expect(report["net"].first["display"]).to eq("29.50 EUR")
end
it "computes the equity distribution" do
rows = builder.build("equity", journal: journal)["groups"].first["rows"]
expect(rows.map { |row| [row["label"], row["percentage"]] }).to eq(
[%w[Alice 60.00], %w[Bob 40.00]],
)
end
it "renders the accounts report as a tree" do
rows = builder.build("accounts", journal: journal)["sections"].first["rows"]
by_account = rows.index_by { |row| row["account"] }
expect(by_account.keys.first(3)).to eq(%w[assets assets:bank assets:bank:checking])
expect(by_account["assets:bank"]["label"]).to eq("Bank")
expect(by_account["assets:bank"]["amounts"].first["display"]).to eq("1029.50 EUR")
expect(by_account["equity:alice"]["amounts"].first["display"]).to eq("(600.00 EUR)")
end
it "renders the balance sheet and income statement as trees" do
assets = builder.build("balance_sheet", journal: journal)["sections"].first
expect(assets["title"]).to eq("Assets")
expect(assets["rows"].map { |row| row["label"] }).to eq(%w[Bank Checking])
expect(assets["rows"].map { |row| row["depth"] }).to eq([0, 1])
expenses = builder.build("income_statement", journal: journal)["sections"].last
expect(expenses["title"]).to eq("Expenses")
expect(expenses["rows"].map { |row| row["label"] }).to eq(%w[Hosting Server])
expect(expenses["rows"].map { |row| row["depth"] }).to eq([0, 1])
end
it "returns transactions with their postings" do
transactions = builder.build("transactions", journal: journal)["transactions"]
expect(transactions.map { |txn| txn["description"] }).to eq(
["Opening balances", "Hosting", "Donation"],
)
expect(transactions.first["postings"].map { |posting| posting["account"] }).to eq(
%w[assets:bank:checking equity:alice equity:bob],
)
end
it "includes hledger's inferred amount for elided postings" do
report = builder.build("transactions", journal: <<~JOURNAL)
2024-01-01 Test
expenses:food 10.00 EUR
assets:bank
JOURNAL
postings = report["transactions"].first["postings"]
expect(postings.last["account"]).to eq("assets:bank")
expect(postings.last["amounts"].first["display"]).to eq("-10.00 EUR")
end
it "rejects journals with include directives" do
expect(Hledger::Journal.extract("```hledger\ninclude /etc/hostname\n```").error).to eq(
:include_not_allowed,
)
end
end
# rubocop:enable RSpec/DescribeClass