Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test #70

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions .iex.exs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
best_block_height_getter =
File.read!("examples/best_block_height_getter.ex")
endpoint_provider =
File.read!("examples/endpoint_provider.ex")
best_block_height_getter = File.read!("examples/best_block_height_getter.ex")
endpoint_provider = File.read!("examples/endpoint_provider.ex")

block_to_ascii_emoji_translator =
File.read!("examples/block_to_game_map_translator.ex")
block_to_ascii_emoji_translator = File.read!("examples/block_to_game_map_translator.ex")
alias FunctionServerBasedOnArweave.DataToChain

read_conn = Components.Ipfs.Connection.conn(:read)

dao_info_exp = %{
"description" => "it's just a cool DAO.",
"logo" => "https://tva1.sinaimg.cn/large/e6c9d24egy1h2rb87iw5ij20je0j8t9t.jpg",
"homepage" => "https://google.com",
"name" => "coolDAO",
"template_gist_id" => "xxxx",
"verified_erc721_contract" => "xxxx"
}
"description" => "it's just a cool DAO.",
"logo" => "https://tva1.sinaimg.cn/large/e6c9d24egy1h2rb87iw5ij20je0j8t9t.jpg",
"homepage" => "https://google.com",
"name" => "coolDAO",
"template_gist_id" => "xxxx",
"verified_erc721_contract" => "xxxx"
}
5 changes: 2 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,9 @@ defmodule FunctionServerBasedOnArweave.MixProject do
{:pow, "~> 1.0.26"},
# trace
{:recon, "~> 2.5"},

{:cors_plug, "~> 2.0"},

{:tentacat, "~> 2.2"}
{:tentacat, "~> 2.2"},
{:credo, "~> 1.6", only: [:dev, :test], runtime: false}
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
%{
"arweave_sdk_ex": {:hex, :arweave_sdk_ex, "0.1.10", "92cc993569ad896b6c6608bf584f1b46cf9d861870071446f7ba9d8f72265b21", [:mix], [{:ex_struct_translator, "~> 0.1.1", [hex: :ex_struct_translator, repo: "hexpm", optional: false]}, {:export, "~> 0.1.0", [hex: :export, repo: "hexpm", optional: false]}, {:httpoison, "~> 1.8", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}, {:jose, "~> 1.11", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "3ccf179eb85cdfa113b890a122ca10d2cb678ac5c70edbac7b3ccb30f581a84d"},
"binary": {:hex, :binary, "0.0.5", "20d816f7274ea34f1b673b4cff2fdb9ebec9391a7a68c349070d515c66b1b2cf", [:mix], [], "hexpm", "ee1e9ebcab703a4e24db554957fbb540642fe9327eb9e295cb3f07dd7c11ddb2"},
"bunt": {:hex, :bunt, "0.2.1", "e2d4792f7bc0ced7583ab54922808919518d0e57ee162901a16a1b6664ef3b14", [:mix], [], "hexpm", "a330bfb4245239787b15005e66ae6845c9cd524a288f0d141c148b02603777a5"},
"castore": {:hex, :castore, "0.1.17", "ba672681de4e51ed8ec1f74ed624d104c0db72742ea1a5e74edbc770c815182f", [:mix], [], "hexpm", "d9844227ed52d26e7519224525cb6868650c272d4a3d327ce3ca5570c12163f9"},
"certifi": {:hex, :certifi, "2.9.0", "6f2a475689dd47f19fb74334859d460a2dc4e3252a3324bd2111b8f0429e7e21", [:rebar3], [], "hexpm", "266da46bdb06d6c6d35fde799bcb28d36d985d424ad7c08b5bb48f5b5cdd4641"},
"connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"},
"cors_plug": {:hex, :cors_plug, "2.0.3", "316f806d10316e6d10f09473f19052d20ba0a0ce2a1d910ddf57d663dac402ae", [:mix], [{:plug, "~> 1.8", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "ee4ae1418e6ce117fc42c2ba3e6cbdca4e95ecd2fe59a05ec6884ca16d469aea"},
"cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
"credo": {:hex, :credo, "1.6.7", "323f5734350fd23a456f2688b9430e7d517afb313fbd38671b8a4449798a7854", [:mix], [{:bunt, "~> 0.2.1", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "41e110bfb007f7eda7f897c10bf019ceab9a0b269ce79f015d54b0dcf4fc7dd3"},
"db_connection": {:hex, :db_connection, "2.4.2", "f92e79aff2375299a16bcb069a14ee8615c3414863a6fef93156aee8e86c2ff3", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4fe53ca91b99f55ea249693a0229356a08f4d1a7931d8ffa79289b145fe83668"},
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
"earmark": {:hex, :earmark, "1.4.26", "f0e3c3d5c278a6d448ad8c27ab0ecdec9c57a7710553138c56af220a6330a4fd", [:mix], [{:earmark_parser, "~> 1.4.26", [hex: :earmark_parser, repo: "hexpm", optional: false]}], "hexpm", "e1231882b56bece0692af33f0959f06c9cd580c2dc2ecb1dc9f16f2750fa78c5"},
Expand Down
105 changes: 105 additions & 0 deletions test/code_on_chain/data_handler.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
defmodule CodeOnChain.SoulCard.DataHandlerTest do
use ExUnit.Case

alias CodesOnChain.SoulCard.DataHandler
alias Components.KVHandler

Ecto.Adapters.SQL.Sandbox.mode(FunctionServerBasedOnArweave.Repo, :manual)

setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(FunctionServerBasedOnArweave.Repo)
end

test "analyze github" do
DataHandler.init_github_repos_white_map()

payload = %{
WeLightProject: %{
if_in_owner: true,
repo_list: [
%{if_in: true, name: "tai_shang_micro_faas_system"},
%{if_in: true, name: "Tai-Shang-Soul-Card"}
]
}
}

assert payload == DataHandler.analyze_github("0x00000001", "leeduckgo")
# assert payload == KVHandler.get("0x00000001", "SoulCard.DataHandler")
end

def data_user() do
%{
"awesome_things" => [
%{"link" => "www.baidu.com", "title" => "Design for the transport"}
],
"basic_info" => %{
"avatar" => "test",
"location" => "California",
"name" => "Robert Fox",
"skills" => [
"Javascript",
"C++",
"Python",
"HTML",
"Node",
"C#",
"Java",
"Javascript",
"C++",
"Python",
"HTML",
"Node",
"C#",
"Java"
],
"slogan" => "Have more than 6 years of Digital Product Design experience.",
"social_links" => %{
"discord " => "hitchhacker@3691",
"github_link " => "https://github.com/WeLightProject",
"mirror_link " => "https://mirror.xyz/apecoder.eth",
"twitter" => "https://twitter.com/Web3dAppCamp",
"wechat " => "197626581"
}
},
"daos_joined" => ["0x73c7448760517E3E6e416b2c130E3c6dB2026A1d"]
}
end

def data_dao() do
%{
"awesome_things" => [%{"link" => "www.google.com", "title" => "Ho"}],
"basic_info" => %{
"avatar" => "https://leeduckgo.com/assets/images/ava.jpeg",
"contract_addresses" => [%{"addr" => "0x0", "alias" => "BYAC NFT"}],
"homepage" => "https://noncegeek.com",
"location" => "California",
"name" => "NonceGeekDAO",
"slogan" => "sth_sth",
"social_links" => %{
"discord" => "hitchhacker@3691",
"github_link" => "https://github.com/WeLightProject",
"mirror_link" => "https://mirror.xyz/apecoder.eth",
"twitter" => "https://twitter.com/Web3dAppCamp",
"wechat" => "197626581"
}
},
"core_members" => [],
"members" => [],
"partners" => [
"0x01234567",
%{"avatar" => "xxx", "link" => "https://google.com", "name" => "谷歌"}
],
"sub_daos" => []
}
end

test "data format" do
test_user_return =
{:ok, "all check is passed!"} == DataHandler.check_format(data_user(), "user")

test_dao_return = {:ok, "all check is passed!"} == DataHandler.check_format(data_dao(), "dao")

assert test_user_return
assert test_dao_return
end
end
17 changes: 17 additions & 0 deletions test/code_on_chain/ipfs_interactor.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule CodeOnChain.SoulCard.IpfsInteractorTest do
use ExUnit.Case

alias CodesOnChain.SoulCard.IpfsInteractor

test "test get data" do
# Base 64 of "hello, world"
test_read_return =
{:ok, "aGVsbG8sIHdvcmxk"} ==
IpfsInteractor.get_data("QmbJtyu82TQSHU52AzRMXBENZGQKYqPsmao9dPuTeorPui")

%{
test_read_return: test_read_return
}
|> IO.inspect()
end
end
90 changes: 90 additions & 0 deletions test/code_on_chain/user_manager.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
defmodule CodeOnChain.SoulCard.UserManagerTest do
use ExUnit.Case

def data_user() do
%{
"awesome_things" => [
%{"link" => "www.baidu.com", "title" => "Design for the transport"}
],
"basic_info" => %{
"avatar" => "test",
"location" => "California",
"name" => "Robert Fox",
"skills" => [
"Javascript",
"C++",
"Python",
"HTML",
"Node",
"C#",
"Java",
"Javascript",
"C++",
"Python",
"HTML",
"Node",
"C#",
"Java"
],
"slogan" => "Have more than 6 years of Digital Product Design experience.",
"social_links" => %{
"discord " => "hitchhacker@3691",
"github_link " => "https://github.com/WeLightProject",
"mirror_link " => "https://mirror.xyz/apecoder.eth",
"twitter" => "https://twitter.com/Web3dAppCamp",
"wechat " => "197626581"
}
},
"daos_joined" => ["0x73c7448760517E3E6e416b2c130E3c6dB2026A1d"]
}
end

def data_dao() do
%{
"awesome_things" => [%{"link" => "www.google.com", "title" => "Ho"}],
"basic_info" => %{
"avatar" => "https://leeduckgo.com/assets/images/ava.jpeg",
"contract_addresses" => [%{"addr" => "0x0", "alias" => "BYAC NFT"}],
"homepage" => "https://noncegeek.com",
"location" => "California",
"name" => "NonceGeekDAO",
"slogan" => "sth_sth",
"social_links" => %{
"discord" => "hitchhacker@3691",
"github_link" => "https://github.com/WeLightProject",
"mirror_link" => "https://mirror.xyz/apecoder.eth",
"twitter" => "https://twitter.com/Web3dAppCamp",
"wechat" => "197626581"
}
},
"core_members" => [],
"members" => [],
"partners" => [
"0x01234567",
%{"avatar" => "xxx", "link" => "https://google.com", "name" => "谷歌"}
],
"sub_daos" => []
}
end

test "create user" do
{%{addr: addr}, msg, %{sig: signature}} = gen_rand_acct_msg_and_sig()
UserManager.create_user(data_user(), "user", addr, msg, signature)
end

test "create user dao" do
{%{addr: addr}, msg, %{sig: signature}} = gen_rand_acct_msg_and_sig()
UserManager.create_user(data_dao(), "dao", addr, msg, signature)
end

def gen_rand_acct_msg_and_sig() do
%{priv: priv} = acct = EthWallet.generate_keys()
msg = DataHandler.rand_msg()
sig_full = sign(msg, priv)
{acct, msg, sig_full}
end

def sign(msg, priv) do
EthWallet.sign_compact(msg, priv)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ defmodule FunctionServerBasedOnArweave.CodeFetchers.NFTTest do
use ExUnit.Case

alias FunctionServerBasedOnArweave.CodeFetchers.NFT

test "get from nft" do
{r, _} = NFT.get_from_nft("1")
assert r == :ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ defmodule FunctionServerBasedOnArweaveWeb.ErrorViewTest do
import Phoenix.View

test "renders 404.html" do
assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "404.html", []) == "Not Found"
assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "404.html", []) ==
"Not Found"
end

test "renders 500.html" do
assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "500.html", []) == "Internal Server Error"
assert render_to_string(FunctionServerBasedOnArweaveWeb.ErrorView, "500.html", []) ==
"Internal Server Error"
end
end
10 changes: 7 additions & 3 deletions test/support/channel_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ChannelCase do
@moduledoc """
This module defines the test case to be used by
channel tests.

Such tests rely on `Phoenix.ChannelTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.

Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
Expand All @@ -29,7 +29,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ChannelCase do
end

setup tags do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, shared: not tags[:async])
pid =
Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo,
shared: not tags[:async]
)

on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
:ok
end
Expand Down
10 changes: 7 additions & 3 deletions test/support/conn_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ConnCase do
@moduledoc """
This module defines the test case to be used by
tests that require setting up a connection.

Such tests rely on `Phoenix.ConnTest` and also
import other functionality to make it easier
to build common data structures and query the data layer.

Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
Expand All @@ -32,7 +32,11 @@ defmodule FunctionServerBasedOnArweaveWeb.ConnCase do
end

setup tags do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, shared: not tags[:async])
pid =
Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo,
shared: not tags[:async]
)

on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
{:ok, conn: Phoenix.ConnTest.build_conn()}
end
Expand Down
14 changes: 9 additions & 5 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ defmodule FunctionServerBasedOnArweave.DataCase do
@moduledoc """
This module defines the setup for tests requiring
access to the application's data layer.

You may define functions here to be used as helpers in
your tests.

Finally, if the test case interacts with the database,
we enable the SQL sandbox, so changes done to the database
are reverted at the end of every test. If you are using
Expand All @@ -28,18 +28,22 @@ defmodule FunctionServerBasedOnArweave.DataCase do
end

setup tags do
pid = Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo, shared: not tags[:async])
pid =
Ecto.Adapters.SQL.Sandbox.start_owner!(FunctionServerBasedOnArweave.Repo,
shared: not tags[:async]
)

on_exit(fn -> Ecto.Adapters.SQL.Sandbox.stop_owner(pid) end)
:ok
end

@doc """
A helper that transforms changeset errors into a map of messages.

assert {:error, changeset} = Accounts.create_user(%{password: "short"})
assert "password is too short" in errors_on(changeset).password
assert %{password: ["password is too short"]} = errors_on(changeset)

"""
def errors_on(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
Expand Down