Skip to content

Commit

Permalink
Rework tests that mocked historical_balances_changes to now mock hist…
Browse files Browse the repository at this point in the history
…orical_balances
  • Loading branch information
IvanIvanoff committed Jan 28, 2025
1 parent 44f636f commit 5bfdf8d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
6 changes: 5 additions & 1 deletion lib/sanbase/balances/balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ defmodule Sanbase.Balance do
balances
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [%{balance: previous}, %{balance: current, datetime: dt}] ->
%{datetime: dt, balance: current - previous}
%{
datetime: dt,
balance_change_amount: current - previous,
balance_change_percent: Sanbase.Math.percent_change(previous, current)
}
end)

{:ok, changes}
Expand Down
2 changes: 2 additions & 0 deletions lib/sanbase/balances/balance_sql_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ defmodule Sanbase.Balance.SqlQuery do
"bitcoin" -> "btc_balances"
"litecoin" -> "ltc_balances"
"dogecoin" -> "doge_balances"
"bitcoin-cash" -> "bch_balances"
"binance" -> "bep20_balances"
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ defmodule SanbaseWeb.Graphql.ProjectApiEthSpentOverTimeTest do
end

test "project eth spent over time", context do
[dt1, dt2, dt3, dt4, dt5, dt6] =
generate_datetimes(~U[2017-05-13T00:00:00Z], "1d", 6) |> Enum.map(&DateTime.to_unix/1)
[dt0, dt1, dt2, dt3, dt4, dt5, dt6] =
generate_datetimes(~U[2017-05-12T00:00:00Z], "1d", 7) |> Enum.map(&DateTime.to_unix/1)

# Historical Balances Changes uses internally the historical balances query,
# so that's what needs to be mocked
rows = [
[dt1, -500],
[dt2, -1500],
[dt3, -6000],
[dt4, 0],
[dt5, 0],
[dt6, -6500]
[dt0, 20000, 1],
[dt1, 19500, 1],
[dt2, 18000, 1],
[dt3, 12000, 1],
[dt4, 0, 0],
[dt5, 0, 0],
[dt6, 5500, 1]
]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
Expand Down
17 changes: 10 additions & 7 deletions test/sanbase_web/graphql/projects/project_api_eth_spent_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,19 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do
end

test "eth spent over time by erc20 projects", context do
[dt1, dt2, dt3, dt4, dt5] =
generate_datetimes(Timex.shift(Timex.now(), days: -4), "1d", 5)
[dt0, dt1, dt2, dt3, dt4, dt5] =
generate_datetimes(Timex.shift(Timex.now(), days: -5), "1d", 6)
|> Enum.map(&DateTime.to_unix/1)

# Historical Balances Changes uses internally the historical balances query,
# so that's what needs to be mocked
rows = [
[dt1, -16_500],
[dt2, -5500],
[dt3, -3500],
[dt4, -2500],
[dt5, -500]
[dt0, 50_000, 1],
[dt1, 33_500, 1],
[dt2, 28_000, 1],
[dt3, 24_500, 1],
[dt4, 22_000, 1],
[dt5, 21_500, 1]
]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
Expand Down

0 comments on commit 5bfdf8d

Please sign in to comment.