Create tests for new dependency injection model.

pull/969/head
Lokraan 6 years ago committed by Locke
parent 2a603be370
commit 582345cfb7
  1. 1
      apps/block_scout_web/test/block_scout_web/channels/exchange_rate_channel_test.exs
  2. 1
      apps/block_scout_web/test/block_scout_web/controllers/api/rpc/stats_controller_test.exs
  3. 2
      apps/explorer/config/test.exs
  4. 4
      apps/explorer/lib/explorer/exchange_rates/exchange_rates.ex
  5. 4
      apps/explorer/lib/explorer/exchange_rates/source/transaction_and_log.ex
  6. 38
      apps/explorer/test/explorer/exchange_rates/exchange_rates_test.exs
  7. 26
      apps/explorer/test/explorer/exchange_rates/source/coin_gecko_test.exs
  8. 45
      apps/explorer/test/explorer/exchange_rates/source/coin_market_cap_test.exs
  9. 26
      apps/explorer/test/explorer/exchange_rates/source/transaction_and_log_test.exs
  10. 2
      config/test.exs

@ -15,6 +15,7 @@ defmodule BlockScoutWeb.ExchangeRateChannelTest do
# Use TestSource mock and ets table for this test set
configuration = Application.get_env(:explorer, Explorer.ExchangeRates)
Application.put_env(:explorer, Explorer.ExchangeRates, source: TestSource)
Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates)
ExchangeRates.init([])

@ -106,6 +106,7 @@ defmodule BlockScoutWeb.API.RPC.StatsControllerTest do
# Use TestSource mock for this test set
configuration = Application.get_env(:explorer, Explorer.ExchangeRates)
Application.put_env(:explorer, Explorer.ExchangeRates, source: TestSource)
Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates)
ExchangeRates.init([])

@ -15,7 +15,7 @@ config :explorer, Explorer.Repo,
pool_timeout: 10_000,
ownership_timeout: 60_000
config :explorer, Explorer.ExchangeRates, enabled: false
config :explorer, Explorer.ExchangeRates, enabled: true
config :explorer, Explorer.Market.History.Cataloger, enabled: false

@ -99,7 +99,9 @@ defmodule Explorer.ExchangeRates do
@doc false
@spec table_name() :: atom()
def table_name, do: @table_name
def table_name do
config(:table_name) || @table_name
end
@spec broadcast_event(atom()) :: :ok
defp broadcast_event(event_type) do

@ -14,8 +14,8 @@ defmodule Explorer.ExchangeRates.Source.TransactionAndLog do
@impl Source
def format_data(data) do
data = secondary_source().format_data(data)
token_data =
token_data =
data
|> Enum.find(fn token -> token.symbol == Explorer.coin() end)
|> build_struct

@ -3,6 +3,7 @@ defmodule Explorer.ExchangeRatesTest do
import Mox
alias Plug.Conn
alias Explorer.ExchangeRates
alias Explorer.ExchangeRates.Token
alias Explorer.ExchangeRates.Source.TestSource
@ -13,21 +14,18 @@ defmodule Explorer.ExchangeRatesTest do
setup do
# Use TestSource mock and ets table for this test set
configuration = Application.get_env(:explorer, Explorer.ExchangeRates)
Application.put_env(:explorer, Explorer.ExchangeRates, source: TestSource)
source_configuration = Application.get_env(:explorer, Explorer.ExchangeRates.Source)
rates_configuration = Application.get_env(:explorer, Explorer.ExchangeRates)
Application.put_env(:explorer, Explorer.ExchangeRates.Source, source: TestSource)
Application.put_env(:explorer, Explorer.ExchangeRates, table_name: :rates)
on_exit(fn ->
Application.put_env(:explorer, Explorer.ExchangeRates, configuration)
Application.put_env(:explorer, Explorer.ExchangeRates.Source, source_configuration)
Application.put_env(:explorer, Explorer.ExchangeRates, rates_configuration)
end)
end
test "start_link" do
stub(TestSource, :fetch_exchange_rates, fn -> {:ok, [Token.null()]} end)
set_mox_global()
assert {:ok, _} = ExchangeRates.start_link([])
end
test "init" do
assert :ets.info(ExchangeRates.table_name()) == :undefined
@ -43,10 +41,18 @@ defmodule Explorer.ExchangeRatesTest do
end
test "handle_info with :update" do
bypass = Bypass.open()
Bypass.expect(bypass, "GET", "/", fn conn ->
Conn.resp(conn, 200, "{}")
end)
stub(TestSource, :source_url, fn -> "http://localhost:#{bypass.port}" end)
ExchangeRates.init([])
state = %{}
expect(TestSource, :fetch_exchange_rates, fn -> {:ok, [Token.null()]} end)
expect(TestSource, :format_data, fn _ -> [Token.null()] end)
set_mox_global()
assert {:noreply, ^state} = ExchangeRates.handle_info(:update, state)
@ -83,9 +89,17 @@ defmodule Explorer.ExchangeRatesTest do
end
test "with failed fetch" do
bypass = Bypass.open()
Bypass.expect(bypass, "GET", "/", fn conn ->
Conn.resp(conn, 200, "{}")
end)
stub(TestSource, :source_url, fn -> "http://localhost:#{bypass.port}" end)
state = %{}
expect(TestSource, :fetch_exchange_rates, fn -> {:ok, [Token.null()]} end)
expect(TestSource, :format_data, fn _ -> [Token.null()] end)
set_mox_global()
assert {:noreply, ^state} = ExchangeRates.handle_info({nil, {:error, "some error"}}, state)

@ -46,7 +46,7 @@ defmodule Explorer.ExchangeRates.Source.CoinGeckoTest do
]
"""
describe "fetch_exchange_rates" do
describe "format_data/1" do
setup do
bypass = Bypass.open()
Application.put_env(:explorer, CoinGecko, base_url: "http://localhost:#{bypass.port}")
@ -54,16 +54,11 @@ defmodule Explorer.ExchangeRates.Source.CoinGeckoTest do
{:ok, bypass: bypass}
end
test "with successful request", %{bypass: bypass} do
test "returns valid tokens with valid data", %{bypass: bypass} do
Bypass.expect(bypass, "GET", "/exchange_rates", fn conn ->
Conn.resp(conn, 200, @json_btc_price)
end)
Bypass.expect(
bypass,
fn conn -> Conn.resp(conn, 200, @json_mkt_data) end
)
{:ok, expected_date, 0} = "2018-10-23T01:25:31.764Z" |> DateTime.from_iso8601()
expected = [
@ -80,18 +75,19 @@ defmodule Explorer.ExchangeRates.Source.CoinGeckoTest do
}
]
assert {:ok, ^expected} = CoinGecko.fetch_exchange_rates()
assert expected == CoinGecko.format_data(@json_mkt_data)
end
test "with bad request response", %{bypass: bypass} do
error_text = ~S({"error": "bad request"})
test "returns nothing when given bad data", %{bypass: bypass} do
Bypass.expect(bypass, "GET", "/exchange_rates", fn conn ->
Conn.resp(conn, 200, @json_btc_price)
end)
Bypass.expect(
bypass,
fn conn -> Conn.resp(conn, 400, error_text) end
)
bad_data = """
[{"id": "poa-network"}]
"""
assert {:error, "bad request"} == CoinGecko.fetch_exchange_rates()
assert [] = CoinGecko.format_data(bad_data)
end
end
end

@ -3,7 +3,6 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do
alias Explorer.ExchangeRates.Token
alias Explorer.ExchangeRates.Source.CoinMarketCap
alias Plug.Conn
@json """
[
@ -27,16 +26,8 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do
]
"""
describe "fetch_exchange_rates" do
setup do
bypass = Bypass.open()
Application.put_env(:explorer, CoinMarketCap, base_url: "http://localhost:#{bypass.port}")
{:ok, bypass: bypass}
end
test "with successful request", %{bypass: bypass} do
Bypass.expect(bypass, fn conn -> Conn.resp(conn, 200, @json) end)
describe "format_data/1" do
test "returns valid tokens with valid data" do
expected_date = ~N[2018-04-11 19:00:00] |> DateTime.from_naive!("Etc/UTC")
expected = [
@ -53,33 +44,15 @@ defmodule Explorer.ExchangeRates.Source.CoinMarketCapTest do
}
]
assert {:ok, ^expected} = CoinMarketCap.fetch_exchange_rates()
end
test "with bad request response", %{bypass: bypass} do
error_text = ~S({"error": "bad request"})
Bypass.expect(bypass, fn conn -> Conn.resp(conn, 400, error_text) end)
assert {:error, "bad request"} == CoinMarketCap.fetch_exchange_rates()
assert expected == CoinMarketCap.format_data(@json)
end
end
test "format_data/1" do
expected_date = ~N[2018-04-11 19:00:00] |> DateTime.from_naive!("Etc/UTC")
test "returns nothing when given bad data" do
bad_data = """
[{"id": "poa-network"}]
"""
expected = [
%Token{
available_supply: Decimal.new("203981804.0"),
btc_value: Decimal.new("0.00007032"),
id: "poa-network",
last_updated: expected_date,
market_cap_usd: Decimal.new("98941986.0"),
name: "POA Network",
symbol: "POA",
usd_value: Decimal.new("0.485053"),
volume_24h_usd: Decimal.new("20185000.0")
}
]
assert expected == CoinMarketCap.format_data(@json)
assert [] = CoinMarketCap.format_data(bad_data)
end
end
end

@ -3,9 +3,31 @@ defmodule Explorer.ExchangeRates.Source.TransactionAndLogTest do
alias Explorer.ExchangeRates.Source.TransactionAndLog
alias Explorer.ExchangeRates.Token
describe "fetch_exchange_rates/1" do
@json """
[
{
"id": "poa-network",
"name": "POA Network",
"symbol": "POA",
"rank": "103",
"price_usd": "0.485053",
"price_btc": "0.00007032",
"24h_volume_usd": "20185000.0",
"market_cap_usd": "98941986.0",
"available_supply": "203981804.0",
"total_supply": "254473964.0",
"max_supply": null,
"percent_change_1h": "-0.66",
"percent_change_24h": "12.34",
"percent_change_7d": "49.15",
"last_updated": "1523473200"
}
]
"""
describe "format_data/1" do
test "bring a list with one %Token{}" do
assert {:ok, [%Token{}]} = TransactionAndLog.fetch_exchange_rates()
assert [%Token{}] = TransactionAndLog.format_data(@json)
end
end
end

@ -12,4 +12,4 @@ config :logger, :error, path: Path.absname("logs/test/error.log")
config :explorer, Explorer.ExchangeRates,
source: Explorer.ExchangeRates.Source.NoOpSource,
store: :none
store: :ets

Loading…
Cancel
Save