Skip to content

Commit

Permalink
chore: Reduce number of warnings in web tests (#9851)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbaranov committed Apr 10, 2024
1 parent 577254f commit a2079c4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule BlockScoutWeb.API.RPC.BlockControllerTest do
use BlockScoutWeb.ConnCase

import EthereumJSONRPC, only: [integer_to_quantity: 1]

alias BlockScoutWeb.Chain
alias Explorer.Chain.{Hash, Wei}
alias Explorer.Counters.AverageBlockTime
Expand Down Expand Up @@ -58,8 +56,6 @@ defmodule BlockScoutWeb.API.RPC.BlockControllerTest do
|> insert(gas_price: 1)
|> with_block(block, gas_used: 1)

block_quantity = integer_to_quantity(block.number)

expected_reward =
emission_reward.reward
|> Wei.to(:wei)
Expand Down Expand Up @@ -101,8 +97,6 @@ defmodule BlockScoutWeb.API.RPC.BlockControllerTest do
|> insert(gas_price: 1)
|> with_block(block, gas_used: 1)

block_quantity = integer_to_quantity(block.number)

decimal_emission_reward = Wei.to(emission_reward.reward, :wei)

uncle1_reward =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ defmodule BlockScoutWeb.API.V2.TokenControllerTest do
)

request = get(conn, "/api/v2/tokens/#{token.contract_address.hash}/counters")
assert response = json_response(request, 200)
assert json_response(request, 200)

Process.sleep(500)
request = get(conn, "/api/v2/tokens/#{token.contract_address.hash}/counters")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ defmodule BlockScoutWeb.API.V2.TransactionControllerTest do
alias Explorer.Chain.{Address, InternalTransaction, Log, Token, TokenTransfer, Transaction}
alias Explorer.Repo

@first_topic_hex_string_1 "0x99e7b0ba56da2819c37c047f0511fd2bf6c9b4e27b4a979a19d6da0f74be8155"

defp topic(topic_hex_string) do
{:ok, topic} = Explorer.Chain.Hash.Full.cast(topic_hex_string)
topic
end

setup do
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.TransactionsApiV2.child_id())
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.TransactionsApiV2.child_id())
Expand Down Expand Up @@ -975,6 +968,13 @@ defmodule BlockScoutWeb.API.V2.TransactionControllerTest do
end

if Application.compile_env(:explorer, :chain_type) == "stability" do
@first_topic_hex_string_1 "0x99e7b0ba56da2819c37c047f0511fd2bf6c9b4e27b4a979a19d6da0f74be8155"

defp topic(topic_hex_string) do
{:ok, topic} = Explorer.Chain.Hash.Full.cast(topic_hex_string)
topic
end

describe "stability fees" do
test "check stability fees", %{conn: conn} do
tx = insert(:transaction) |> with_block()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,72 @@
defmodule BlockScoutWeb.API.V2.ValidatorControllerTest do
use BlockScoutWeb.ConnCase

alias Explorer.Chain.Address
alias Explorer.Chain.Stability.Validator, as: ValidatorStability
alias Explorer.Chain.Cache.StabilityValidatorsCounters

if Application.compile_env(:explorer, :chain_type) == "stability" do
alias Explorer.Chain.Address
alias Explorer.Chain.Cache.StabilityValidatorsCounters
alias Explorer.Chain.Stability.Validator, as: ValidatorStability

defp check_paginated_response(first_page_resp, second_page_resp, list) do
assert Enum.count(first_page_resp["items"]) == 50
assert first_page_resp["next_page_params"] != nil
compare_item(Enum.at(list, 50), Enum.at(first_page_resp["items"], 0))
compare_item(Enum.at(list, 1), Enum.at(first_page_resp["items"], 49))

assert Enum.count(second_page_resp["items"]) == 1
assert second_page_resp["next_page_params"] == nil
compare_item(Enum.at(list, 0), Enum.at(second_page_resp["items"], 0))
end

defp compare_default_sorting_for_asc({validator_1, blocks_count_1}, {validator_2, blocks_count_2}) do
case {
compare(blocks_count_1, blocks_count_2),
compare(
Keyword.fetch!(ValidatorStability.state_enum(), validator_1.state),
Keyword.fetch!(ValidatorStability.state_enum(), validator_2.state)
),
compare(validator_1.address_hash.bytes, validator_2.address_hash.bytes)
} do
{:lt, _, _} -> false
{:eq, :lt, _} -> false
{:eq, :eq, :lt} -> false
_ -> true
end
end

defp compare_default_sorting_for_desc({validator_1, blocks_count_1}, {validator_2, blocks_count_2}) do
case {
compare(blocks_count_1, blocks_count_2),
compare(
Keyword.fetch!(ValidatorStability.state_enum(), validator_1.state),
Keyword.fetch!(ValidatorStability.state_enum(), validator_2.state)
),
compare(validator_1.address_hash.bytes, validator_2.address_hash.bytes)
} do
{:gt, _, _} -> false
{:eq, :lt, _} -> false
{:eq, :eq, :lt} -> false
_ -> true
end
end

defp compare_item(%ValidatorStability{} = validator, json) do
assert Address.checksum(validator.address_hash) == json["address"]["hash"]
assert to_string(validator.state) == json["state"]
end

defp compare_item({%ValidatorStability{} = validator, count}, json) do
assert json["blocks_validated_count"] == count + 1
assert compare_item(validator, json)
end

defp compare(a, b) do
cond do
a < b -> :lt
a > b -> :gt
true -> :eq
end
end

describe "/validators/stability" do
test "get paginated list of the validators", %{conn: conn} do
validators =
Expand Down Expand Up @@ -141,65 +202,4 @@ defmodule BlockScoutWeb.API.V2.ValidatorControllerTest do
end
end
end

defp compare_item(%ValidatorStability{} = validator, json) do
assert Address.checksum(validator.address_hash) == json["address"]["hash"]
assert to_string(validator.state) == json["state"]
end

defp compare_item({%ValidatorStability{} = validator, count}, json) do
assert json["blocks_validated_count"] == count + 1
assert compare_item(validator, json)
end

defp check_paginated_response(first_page_resp, second_page_resp, list) do
assert Enum.count(first_page_resp["items"]) == 50
assert first_page_resp["next_page_params"] != nil
compare_item(Enum.at(list, 50), Enum.at(first_page_resp["items"], 0))
compare_item(Enum.at(list, 1), Enum.at(first_page_resp["items"], 49))

assert Enum.count(second_page_resp["items"]) == 1
assert second_page_resp["next_page_params"] == nil
compare_item(Enum.at(list, 0), Enum.at(second_page_resp["items"], 0))
end

defp compare_default_sorting_for_asc({validator_1, blocks_count_1}, {validator_2, blocks_count_2}) do
case {
compare(blocks_count_1, blocks_count_2),
compare(
Keyword.fetch!(ValidatorStability.state_enum(), validator_1.state),
Keyword.fetch!(ValidatorStability.state_enum(), validator_2.state)
),
compare(validator_1.address_hash.bytes, validator_2.address_hash.bytes)
} do
{:lt, _, _} -> false
{:eq, :lt, _} -> false
{:eq, :eq, :lt} -> false
_ -> true
end
end

defp compare_default_sorting_for_desc({validator_1, blocks_count_1}, {validator_2, blocks_count_2}) do
case {
compare(blocks_count_1, blocks_count_2),
compare(
Keyword.fetch!(ValidatorStability.state_enum(), validator_1.state),
Keyword.fetch!(ValidatorStability.state_enum(), validator_2.state)
),
compare(validator_1.address_hash.bytes, validator_2.address_hash.bytes)
} do
{:gt, _, _} -> false
{:eq, :lt, _} -> false
{:eq, :eq, :lt} -> false
_ -> true
end
end

defp compare(a, b) do
cond do
a < b -> :lt
a > b -> :gt
true -> :eq
end
end
end
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
defmodule BlockScoutWeb.API.V2.TransactionViewTest do
use BlockScoutWeb.ConnCase, async: true

import Mox

alias BlockScoutWeb.API.V2.TransactionView
alias Explorer.Repo

describe "decode_logs/2" do
test "doesn't use decoding candidate event with different 2nd, 3d or 4th topic" do
Expand Down

0 comments on commit a2079c4

Please sign in to comment.