mirror of
https://github.com/luxfi/explorer-v1.git
synced 2026-10-30 19:34:59 +00:00
86 lines
2.9 KiB
Elixir
86 lines
2.9 KiB
Elixir
defmodule Explorer.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,
|
|
it cannot be async. For this reason, every test runs
|
|
inside a transaction which is reset at the beginning
|
|
of the test unless the test case is marked as async.
|
|
"""
|
|
|
|
use ExUnit.CaseTemplate
|
|
|
|
alias Ecto.Changeset
|
|
|
|
using do
|
|
quote do
|
|
use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney
|
|
|
|
import Ecto
|
|
import Ecto.Changeset
|
|
import Ecto.Query
|
|
import Explorer.DataCase
|
|
import Explorer.Factory
|
|
|
|
alias Explorer.Repo
|
|
end
|
|
end
|
|
|
|
setup tags do
|
|
ExVCR.Config.cassette_library_dir("test/support/fixture/vcr_cassettes")
|
|
|
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Explorer.Repo)
|
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Explorer.Repo.Account)
|
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Explorer.Repo.EventNotifications)
|
|
|
|
if !tags[:async] do
|
|
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo, {:shared, self()})
|
|
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo.Account, {:shared, self()})
|
|
Ecto.Adapters.SQL.Sandbox.mode(Explorer.Repo.EventNotifications, {:shared, self()})
|
|
end
|
|
|
|
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.BlockNumber.child_id())
|
|
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.BlockNumber.child_id())
|
|
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.Blocks.child_id())
|
|
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.Blocks.child_id())
|
|
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.Transactions.child_id())
|
|
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.Transactions.child_id())
|
|
Supervisor.terminate_child(Explorer.Supervisor, Explorer.Chain.Cache.Accounts.child_id())
|
|
Supervisor.restart_child(Explorer.Supervisor, Explorer.Chain.Cache.Accounts.child_id())
|
|
|
|
:ok
|
|
end
|
|
|
|
def wait_for_results(producer) do
|
|
wait_for_results(producer, 30)
|
|
end
|
|
|
|
def wait_for_results(_producer, 0) do
|
|
raise "wait_for_results timed out after exhausting retries"
|
|
end
|
|
|
|
def wait_for_results(producer, retries) when retries > 0 do
|
|
Process.sleep(100)
|
|
producer.()
|
|
rescue
|
|
_error in [DBConnection.ConnectionError, Ecto.NoResultsError] ->
|
|
Process.sleep(300)
|
|
wait_for_results(producer, retries - 1)
|
|
end
|
|
|
|
@doc """
|
|
Converts a changeset to a map of fields with lists of formatted error messages.
|
|
"""
|
|
def changeset_errors(%Changeset{} = changeset) do
|
|
Changeset.traverse_errors(changeset, fn {error_message, opts} ->
|
|
Enum.reduce(opts, error_message, fn {key, value}, error_message ->
|
|
String.replace(error_message, "%{#{key}}", to_string(value))
|
|
end)
|
|
end)
|
|
end
|
|
end
|