From d4aedd08f4360d305d8609cf34a88375443d9ab5 Mon Sep 17 00:00:00 2001 From: Lokraan Date: Sun, 23 Sep 2018 13:16:00 -0500 Subject: [PATCH] Fix queries. Update chain queries and created tests for mentioned queries. --- apps/explorer/lib/explorer/chain.ex | 3 + apps/explorer/test/explorer/chain_test.exs | 76 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 43be810659..a898e19d02 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -826,6 +826,7 @@ defmodule Explorer.Chain do end @doc """ +<<<<<<< HEAD Lists the top 250 `t:Explorer.Chain.Address.t/0`'s' in descending order based on coin balance. """ @@ -839,6 +840,8 @@ defmodule Explorer.Chain do end @doc """ +======= +>>>>>>> Fix queries. Finds all Blocks validated by the address given. ## Options diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 5e834724ef..4352932370 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -1033,6 +1033,82 @@ defmodule Explorer.ChainTest do end end + describe "get_blocks_validated_by_address/2" do + test "without blocks" do + address = insert(:address) + + assert [] = Chain.get_blocks_validated_by_address([], address) + end + + test "with blocks" do + address = insert(:address) + address2 = insert(:address) + + %Block{hash: hash, miner_hash: miner_hash} = + insert(:block, miner: address, miner_hash: adress.hash) + + %Block{hash: hash, miner_hash: miner_hash} = + insert(:block, miner: address2, miner_hash: adress2.hash) + + assert [%Block{hash: ^hash, miner: ^address, miner_hash: ^address.hash}] == + Chain.get_blocks_validated_by_address([], address) + + assert [%Block{hash: ^hash, miner: ^address, miner_hash: ^address.hash}] == + Chain.get_blocks_validated_by_address([], address2) + end + + test "with blocks can be paginated" do + address = insert(:address) + address2 = insert(:address) + + second_page_hashes = + 50 + |> insert_list(:block, miner: address, miner_hash: adress.hash) + |> Enum.map(& &1.hash) + + second_page_hashes2 = + 50 + |> insert_list(:block, miner: address2, miner_hash: adress.hash2) + |> Enum.map(& &1.hash) + + assert second_page_hashes == + [paging_options: %PagingOptions{key: {inserted_at, hash}, page_size: 50}] + |> Chain.get_blocks_validated_by_address([], address) + |> Enum.map(& &1.hash) + |> Enum.reverse() + + assert second_page_hashes2 == + [paging_options: %PagingOptions{key: {inserted_at, hash}, page_size: 50}] + |> Chain.get_blocks_validated_by_address([], address2) + |> Enum.map(& &1.hash) + |> Enum.reverse() + end + end + + describe "address_to_validation_count/1" do + test "without blocks" do + address = insert(:address) + + assert 0 = Chain.address_to_validation_count(address) + end + + test "with blocks" do + address = insert(:address) + address2 = insert(:address) + + %Block{hash: hash, miner_hash: miner_hash} = + insert(:block, miner: address, miner_hash: adress.hash) + + %Block{hash: hash, miner_hash: miner_hash} = + insert(:block, miner: address2, miner_hash: adress2.hash) + %Block{hash: hash, miner_hash: miner_hash} = + insert(:block, miner: address2, miner_hash: adress2.hash) + + assert 1 = Chain.address_to_validation_count(address) + assert 2 = Chain.address_to_validation_count(address) + end + end + describe "number_to_block/1" do test "without block" do assert {:error, :not_found} = Chain.number_to_block(-1)