4
0
Fork 0
Fork of the exw3 library. With our own additions
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exw3/test/exw3_test.exs

122 lines
3.0 KiB

7 years ago
defmodule EXW3Test do
use ExUnit.Case
doctest ExW3
7 years ago
setup_all do
%{
simple_storage_abi: ExW3.load_abi("test/examples/build/SimpleStorage.abi"),
array_tester_abi: ExW3.load_abi("test/examples/build/ArrayTester.abi"),
event_tester_abi: ExW3.load_abi("test/examples/build/EventTester.abi"),
accounts: ExW3.accounts
}
7 years ago
end
test "gets accounts" do
assert ExW3.accounts |> is_list
end
test "gets balance", context do
assert ExW3.balance(Enum.at context[:accounts], 0 ) |> is_integer
end
test "gets block number" do
assert ExW3.block_number |> is_integer
end
7 years ago
test "loads abi", context do
assert context[:simple_storage_abi] |> is_map
7 years ago
end
test "mines a block" do
block_number = ExW3.block_number
ExW3.mine
assert ExW3.block_number == block_number + 1
end
test "mines multiple blocks" do
block_number = ExW3.block_number
ExW3.mine 5
assert ExW3.block_number == block_number + 5
end
test "deploys simple storage and uses it", context do
contract_address = ExW3.Contract.deploy(
"test/examples/build/SimpleStorage.bin",
%{
from: Enum.at(context[:accounts], 0),
gas: 150000
}
)
7 years ago
storage = ExW3.Contract.at context[:simple_storage_abi], contract_address
7 years ago
{:ok, result} = ExW3.Contract.method(storage, "get")
assert result == 0
{:ok, tx_hash} = ExW3.Contract.method(storage, "set", [1], %{from: Enum.at(context[:accounts], 0)})
receipt = ExW3.tx_receipt tx_hash
#IO.inspect ExW3.block receipt["blockNumber"]
{:ok, result} = ExW3.Contract.method(storage, "get")
assert result == 1
7 years ago
end
test "deploys event tester and uses it", context do
contract_address = ExW3.Contract.deploy(
"test/examples/build/EventTester.bin",
%{
from: Enum.at(context[:accounts], 0),
gas: 300000
}
)
event_tester = ExW3.Contract.at context[:event_tester_abi], contract_address
{:ok, event_pub} = ExW3.EventPublisher.start_link
ExW3.subscribe("Simple(uint256,bytes32)", fn event -> IO.inspect event end)
{:ok, tx_hash} = ExW3.Contract.method(event_tester, "simple", ["hello, there!"], %{from: Enum.at(context[:accounts], 0)})
receipt = ExW3.tx_receipt tx_hash
logs = receipt["logs"]
topic = Map.get(Enum.at(logs, 0), "topics")
assert String.slice(Enum.at(topic, 0), 2..-1) == ExW3.encode_event("Simple(uint256,bytes32)")
:timer.sleep(5000)
end
test "deploys array tester and uses it", context do
contract_address = ExW3.Contract.deploy(
"test/examples/build/ArrayTester.bin",
%{
from: Enum.at(context[:accounts], 0),
gas: 300000
}
)
array_tester = ExW3.Contract.at context[:array_tester_abi], contract_address
arr = [1, 2, 3, 4, 5]
{:ok, result} = ExW3.Contract.method(array_tester, "staticUint", [arr])
assert result == arr
#0x5d4e0342
{:ok, result} = ExW3.Contract.method(array_tester, "dynamicUint", [arr])
assert result == arr
7 years ago
end
end