64 lines
2.3 KiB
Ruby
64 lines
2.3 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].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")
|
|
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 "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
|