From 36949da03b6194bc137f19ecb78df4759e226b2d Mon Sep 17 00:00:00 2001 From: Stamates Date: Mon, 6 Aug 2018 17:31:06 -0400 Subject: [PATCH] Add FakeAdapter to replace reltuple count query during testing --- apps/explorer_web/config/test.exs | 2 ++ .../lib/explorer_web/controllers/chain_controller.ex | 5 +++-- apps/explorer_web/lib/explorer_web/notifier.ex | 4 +++- .../test/explorer_web/features/viewing_chain_test.exs | 3 +-- apps/explorer_web/test/support/fake_adapter.ex | 8 ++++++++ 5 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 apps/explorer_web/test/support/fake_adapter.ex diff --git a/apps/explorer_web/config/test.exs b/apps/explorer_web/config/test.exs index dc2523f19b..70bf70dcda 100644 --- a/apps/explorer_web/config/test.exs +++ b/apps/explorer_web/config/test.exs @@ -11,3 +11,5 @@ config :explorer_web, ExplorerWeb.Endpoint, # Configure wallaby config :wallaby, screenshot_on_failure: true + +config :explorer_web, :fake_adapter, ExplorerWeb.FakeAdapter diff --git a/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex b/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex index ffdc2bd1c4..51ccb85e30 100644 --- a/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex +++ b/apps/explorer_web/lib/explorer_web/controllers/chain_controller.ex @@ -7,9 +7,10 @@ defmodule ExplorerWeb.ChainController do alias Explorer.ExchangeRates.Token alias Explorer.Market + @address_count_module Application.get_env(:explorer_web, :fake_adapter) || Chain + def show(conn, _params) do transaction_estimated_count = Chain.transaction_estimated_count() - address_estimated_count = Chain.address_estimated_count() transactions = Chain.recent_collated_transactions( @@ -29,7 +30,7 @@ defmodule ExplorerWeb.ChainController do render( conn, "show.html", - address_estimated_count: address_estimated_count, + address_estimated_count: @address_count_module.address_estimated_count(), average_block_time: Chain.average_block_time(), blocks: blocks, exchange_rate: Market.get_exchange_rate(Explorer.coin()) || Token.null(), diff --git a/apps/explorer_web/lib/explorer_web/notifier.ex b/apps/explorer_web/lib/explorer_web/notifier.ex index 037f014aef..1bb0164619 100644 --- a/apps/explorer_web/lib/explorer_web/notifier.ex +++ b/apps/explorer_web/lib/explorer_web/notifier.ex @@ -8,8 +8,10 @@ defmodule ExplorerWeb.Notifier do alias Explorer.ExchangeRates.Token alias ExplorerWeb.Endpoint + @address_count_module Application.get_env(:explorer_web, :fake_adapter) || Chain + def handle_event({:chain_event, :addresses, addresses}) do - Endpoint.broadcast("addresses:new_address", "count", %{count: Chain.address_estimated_count()}) + Endpoint.broadcast("addresses:new_address", "count", %{count: @address_count_module.address_estimated_count()}) addresses |> Stream.reject(fn %Address{fetched_balance: fetched_balance} -> is_nil(fetched_balance) end) diff --git a/apps/explorer_web/test/explorer_web/features/viewing_chain_test.exs b/apps/explorer_web/test/explorer_web/features/viewing_chain_test.exs index 167c0e2b16..ae4abe36d9 100644 --- a/apps/explorer_web/test/explorer_web/features/viewing_chain_test.exs +++ b/apps/explorer_web/test/explorer_web/features/viewing_chain_test.exs @@ -3,7 +3,6 @@ defmodule ExplorerWeb.ViewingChainTest do use ExplorerWeb.FeatureCase, async: true - alias Explorer.Chain alias ExplorerWeb.{AddressPage, BlockPage, ChainPage, Notifier, TransactionPage} setup do @@ -52,7 +51,7 @@ defmodule ExplorerWeb.ViewingChainTest do end test "address count live updates", %{session: session} do - count = Chain.address_estimated_count() + count = ExplorerWeb.FakeAdapter.address_estimated_count() session |> ChainPage.visit_page() diff --git a/apps/explorer_web/test/support/fake_adapter.ex b/apps/explorer_web/test/support/fake_adapter.ex new file mode 100644 index 0000000000..b9d8ff0397 --- /dev/null +++ b/apps/explorer_web/test/support/fake_adapter.ex @@ -0,0 +1,8 @@ +defmodule ExplorerWeb.FakeAdapter do + alias Explorer.Chain.Address + alias Explorer.Repo + + def address_estimated_count() do + Repo.aggregate(Address, :count, :hash) + end +end