Allow addresses to present zero balances when they do not have a balance

pull/42/head
Doc Ritezel 7 years ago
parent 37b7f11b79
commit fc513ccb91
  1. 1
      lib/explorer/credit.ex
  2. 1
      lib/explorer/debit.ex
  3. 7
      lib/explorer/forms/address_form.ex
  4. 10
      test/explorer/forms/address_form_test.exs

@ -23,4 +23,5 @@ defmodule Explorer.Credit do
def refresh do
SQL.query!(Repo, "REFRESH MATERIALIZED VIEW CONCURRENTLY credits;", [], timeout: 120_000)
end
def null, do: %__MODULE__{value: Decimal.new(0), count: 0}
end

@ -23,4 +23,5 @@ defmodule Explorer.Debit do
def refresh do
SQL.query!(Repo, "REFRESH MATERIALIZED VIEW CONCURRENTLY debits;", [], timeout: 120_000)
end
def null, do: %__MODULE__{value: Decimal.new(0), count: 0}
end

@ -1,8 +1,13 @@
defmodule Explorer.AddressForm do
@moduledoc false
alias Explorer.Credit
alias Explorer.Debit
def build(address) do
balance = Decimal.sub(address.credit.value, address.debit.value)
credit = address.credit || Credit.null
debit = address.debit || Debit.null
balance = Decimal.sub(credit.value, debit.value)
Map.put(address, :balance, balance)
end
end

@ -6,7 +6,7 @@ defmodule Explorer.AddressFormTest do
alias Explorer.Debit
describe "build/1" do
test "that it has a balance" do
test "returns a balance" do
address = insert(:address, %{hash: "bert"})
insert(:transaction, value: 5) |> with_addresses(%{to: "bert", from: "ernie"})
insert(:transaction, value: 5) |> with_addresses(%{to: "bert", from: "kermit"})
@ -15,5 +15,13 @@ defmodule Explorer.AddressFormTest do
Debit.refresh
assert AddressForm.build(Repo.preload(address, [:debit, :credit])).balance == Decimal.new(10)
end
test "returns a zero balance when the address does not have balances" do
address = insert(:address, %{hash: "bert"})
insert(:transaction, value: 5) |> with_addresses(%{to: "bert", from: "ernie"})
insert(:transaction, value: 5) |> with_addresses(%{to: "bert", from: "kermit"})
assert AddressForm.build(Repo.preload(address, [:debit, :credit])).balance == Decimal.new(0)
end
end
end

Loading…
Cancel
Save