Pull out the last five Blocks when loading the homepage.

pull/2/head
CJ Bryan and Matt Olenick 7 years ago
parent 1237f90cba
commit d14b9c8160
  1. 10
      lib/explorer_web/controllers/page_controller.ex
  2. 10
      test/explorer/block_test.exs
  3. 17
      test/explorer_web/controllers/page_controller_test.exs
  4. 2
      test/support/conn_case.ex
  5. 20
      test/support/factories/block_factory.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

@ -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

@ -1,8 +1,23 @@
defmodule ExplorerWeb.PageControllerTest do
use ExplorerWeb.ConnCase
test "GET /", %{conn: conn} do
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

@ -23,6 +23,8 @@ defmodule ExplorerWeb.ConnCase do
# The default endpoint for testing
@endpoint ExplorerWeb.Endpoint
import Explorer.Factory
end
end

@ -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

Loading…
Cancel
Save