From d14b9c8160d68ddc3a33d1ba65ed7b24a89b6d05 Mon Sep 17 00:00:00 2001 From: CJ Bryan and Matt Olenick Date: Wed, 17 Jan 2018 16:30:41 -0800 Subject: [PATCH] Pull out the last five Blocks when loading the homepage. --- .../controllers/page_controller.ex | 10 ++++++++- test/explorer/block_test.exs | 10 ++++----- .../controllers/page_controller_test.exs | 21 ++++++++++++++++--- test/support/conn_case.ex | 2 ++ test/support/factories/block_factory.ex | 20 +++++++++--------- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/lib/explorer_web/controllers/page_controller.ex b/lib/explorer_web/controllers/page_controller.ex index bed0258a71..b37e07d823 100644 --- a/lib/explorer_web/controllers/page_controller.ex +++ b/lib/explorer_web/controllers/page_controller.ex @@ -1,7 +1,15 @@ defmodule ExplorerWeb.PageController do use ExplorerWeb, :controller + import Ecto.Query + alias Explorer.Block + alias Explorer.Repo def index(conn, _params) do - render conn, "index.html" + blocks = Block + |> order_by(desc: :inserted_at) + |> limit(5) + |> Repo.all + + render(conn, "index.html", blocks: blocks) end end diff --git a/test/explorer/block_test.exs b/test/explorer/block_test.exs index 60f2414a9d..4a883810f3 100644 --- a/test/explorer/block_test.exs +++ b/test/explorer/block_test.exs @@ -5,22 +5,22 @@ defmodule Explorer.BlockTest do describe "changeset/2" do test "with invalid attributes" do - changeset = Block.changeset(%Block{}, %{racecar: "yellow ham"}) + changeset = %Block{} |> Block.changeset(%{racecar: "yellow ham"}) refute(changeset.valid?) end test "with duplicate information" do insert(:block, hash: "0x0") - {:error, changeset} = Block.changeset(%Block{}, params_for(:block, hash: "0x0")) |> Repo.insert - assert changeset.errors == [hash: {"has already been taken", []}] + {:error, changeset} = %Block{} |> Block.changeset(params_for(:block, hash: "0x0")) |> Repo.insert refute changeset.valid? + assert changeset.errors == [hash: {"has already been taken", []}] end test "rejects duplicate blocks with mixed case" do insert(:block, hash: "0xa") - {:error, changeset} = Block.changeset(%Block{}, params_for(:block, hash: "0xA")) |> Repo.insert - assert changeset.errors == [hash: {"has already been taken", []}] + {:error, changeset} = %Block{} |> Block.changeset(params_for(:block, hash: "0xA")) |> Repo.insert refute changeset.valid? + assert changeset.errors == [hash: {"has already been taken", []}] end end end diff --git a/test/explorer_web/controllers/page_controller_test.exs b/test/explorer_web/controllers/page_controller_test.exs index d446ef61a0..69bf444e10 100644 --- a/test/explorer_web/controllers/page_controller_test.exs +++ b/test/explorer_web/controllers/page_controller_test.exs @@ -1,8 +1,23 @@ defmodule ExplorerWeb.PageControllerTest do use ExplorerWeb.ConnCase - test "GET /", %{conn: conn} do - conn = get conn, "/" - assert html_response(conn, 200) =~ "Welcome" + describe "GET index/2" do + test "returns a welcome message", %{conn: conn} do + conn = get conn, "/" + assert html_response(conn, 200) =~ "Welcome" + end + + test "returns a block", %{conn: conn} do + block = insert(:block, %{number: 23}) + conn = get conn, "/" + assert(List.first(conn.assigns.blocks) == block) + end + + test "excludes all but the most recent five blocks", %{conn: conn} do + old_block = insert(:block) + insert_list(5, :block) + conn = get conn, "/" + refute(Enum.member?(conn.assigns.blocks, old_block)) + end end end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 2a5c3ac9d7..0b01988ae6 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -23,6 +23,8 @@ defmodule ExplorerWeb.ConnCase do # The default endpoint for testing @endpoint ExplorerWeb.Endpoint + + import Explorer.Factory end end diff --git a/test/support/factories/block_factory.ex b/test/support/factories/block_factory.ex index 389810aa25..39dec623c9 100644 --- a/test/support/factories/block_factory.ex +++ b/test/support/factories/block_factory.ex @@ -3,16 +3,16 @@ defmodule Explorer.BlockFactory do quote do def block_factory do %Explorer.Block{ - number: 1, - hash: "0x0", - parent_hash: "0x0", - nonce: "1", - miner: "0x0", - difficulty: 1, - total_difficulty: 1, - size: 0, - gas_limit: 0, - gas_used: 0, + number: sequence(""), + hash: sequence("0x"), + parent_hash: sequence("0x"), + nonce: sequence(""), + miner: sequence("0x"), + difficulty: Enum.random(1..100_000), + total_difficulty: Enum.random(1..100_000), + size: Enum.random(1..100_000), + gas_limit: Enum.random(1..100_000), + gas_used: Enum.random(1..100_000), timestamp: DateTime.utc_now, } end