diff --git a/lib/ledger.ex b/lib/ledger.ex index 24459f6..c41a3bb 100644 --- a/lib/ledger.ex +++ b/lib/ledger.ex @@ -11,17 +11,11 @@ defmodule ExLedger.Ledger do struct!(%__MODULE__{}, attrs) end - @spec balance(__MODULE__.t()) :: integer() - def balance(%{transactions: transactions} = _ledger) do - transactions - |> Enum.reduce(0, fn totals, acc -> - totals = - totals - |> Transaction.balance() - |> Keyword.values() - |> Enum.sum() - - acc + totals + @spec is_balance?(__MODULE__.t()) :: bool() + def is_balance?(%{transactions: transactions} = _ledger) do + Enum.flat_map(transactions, fn txn -> + Keyword.values(txn.balances) end) + |> Enum.all?(&(&1 == 0)) end end diff --git a/test/exledger_test.exs b/test/exledger_test.exs index e9d5b38..79d4214 100644 --- a/test/exledger_test.exs +++ b/test/exledger_test.exs @@ -2,9 +2,9 @@ defmodule ExledgerTest do use ExUnit.Case use ExLedger.LedgerBuilder - test "Ledger.balance/1" do + test "Ledger.is_balance?/1" do transaction = - Transaction.new(%{description: "Test"}) + Transaction.new("test transaction") |> Transaction.add_entry("assets", {1, :USD}) |> Transaction.add_entry("expensese", {-1, :USD}) |> Transaction.add_entry("assets", {1, :PHP}) @@ -12,6 +12,18 @@ defmodule ExledgerTest do ledger = Ledger.new(transactions: [transaction]) - assert Ledger.balance(ledger) == 0 + assert Ledger.is_balance?(ledger) + end + + test "Ledger.is_balance?/1 imbalance" do + transaction = + Transaction.new("test transaction") + |> Transaction.add_entry("assets", {1, :USD}) + |> Transaction.add_entry("expensese", {-1, :USD}) + |> Transaction.add_entry("assets", {1, :PHP}) + + ledger = Ledger.new(transactions: [transaction]) + + assert false == Ledger.is_balance?(ledger) end end diff --git a/test/transaction_test.exs b/test/transaction_test.exs index 4d0fc29..257a73c 100644 --- a/test/transaction_test.exs +++ b/test/transaction_test.exs @@ -2,15 +2,15 @@ defmodule Exledger.TransactionTest do use ExUnit.Case use ExLedger.LedgerBuilder - test "add_entry" do + test "add_entry/3" do assert %{balances: [usd: 1]} = - Transaction.new(%{description: "Test"}) + Transaction.new("test transaction") |> Transaction.add_entry("assets", {1, :usd}) end - test "add_entry sum 2 common currency" do + test "add_entry/3 sum 2 common currency" do assert %{balances: [usd: 6]} = - Transaction.new(%{description: "Test"}) + Transaction.new("test transaction") |> Transaction.add_entry("assets", {1, :usd}) |> Transaction.add_entry("assets", {5, :usd}) end @@ -18,15 +18,12 @@ defmodule Exledger.TransactionTest do test "compute balances" do end - test "Transaction.balance/1" do - transaction = - Transaction.new(%{description: "Test"}) - |> Transaction.add_entry("assets", {1, :USD}) - |> Transaction.add_entry("expenses", {-1, :USD}) - |> Transaction.add_entry("assets", {1, :PHP}) - |> Transaction.add_entry("expenses", {-1, :PHP}) - |> Transaction.balance() - - assert [USD: 0, PHP: 0] == transaction + test "add_entry/3 with multiple currency" do + assert %{balances: [usd: 0, php: 0]} = + Transaction.new("test transaction") + |> Transaction.add_entry("assets", {1, :usd}) + |> Transaction.add_entry("expenses", {-1, :usd}) + |> Transaction.add_entry("assets", {1, :php}) + |> Transaction.add_entry("expenses", {-1, :php}) end end