Skip to content

Commit

Permalink
Rework tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed May 27, 2020
1 parent 831142b commit 3ea1248
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 241 deletions.
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ config :sanbase, Sanbase.Repo,
pool_size: {:system, "SANBASE_POOL_SIZE", "20"},
max_overflow: 5,
# because of pgbouncer
prepare: :unnamed
prepare: :unnamed,
migration_timestamps: [type: :naive_datetime_usec]

config :sanbase, Sanbase.Auth.Hmac, secret_key: {:system, "APIKEY_HMAC_SECRET_KEY", nil}

Expand Down
6 changes: 2 additions & 4 deletions lib/sanbase/billing/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ defmodule Sanbase.Billing.GraphqlSchema do

require SanbaseWeb.Graphql.Schema

# @query_type Absinthe.Schema.lookup_type(SanbaseWeb.Graphql.Schema, :query)
# @fields @query_type.fields |> Map.keys()
@query_type %{fields: []}
@fields []
@query_type Absinthe.Schema.lookup_type(SanbaseWeb.Graphql.Schema, :query)
@fields @query_type.fields |> Map.keys()

@doc ~s"""
Return a map of {query, product_id} key-value pairs. The key is a query that
Expand Down
6 changes: 3 additions & 3 deletions lib/sanbase/comments/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ defmodule Sanbase.Comment do

defp multi_run(multi, :select_root_parent_id, %{parent_id: parent_id}) do
multi
|> Ecto.Multi.run(:select_root_parent_id, fn _ ->
|> Ecto.Multi.run(:select_root_parent_id, fn _repo, _changes ->
root_parent_id =
from(c in __MODULE__, where: c.id == ^parent_id, select: c.root_parent_id)
|> Repo.one()
Expand All @@ -211,7 +211,7 @@ defmodule Sanbase.Comment do
multi
|> Ecto.Multi.run(
:create_new_comment,
fn %{select_root_parent_id: parent_root_parent_id} ->
fn _repo, %{select_root_parent_id: parent_root_parent_id} ->
# Handle all case: If the parent has a parent_root_id - inherit it
# If the parent does not have it - then the parent is a top level comment
# and the current parent_root_id should be se to parent_id
Expand All @@ -233,7 +233,7 @@ defmodule Sanbase.Comment do
multi
|> Ecto.Multi.run(
:update_subcomments_count,
fn %{create_new_comment: %__MODULE__{root_parent_id: root_id}} ->
fn _repo, %{create_new_comment: %__MODULE__{root_parent_id: root_id}} ->
{:ok, _} = update_subcomments_counts(root_id)
{:ok, "Updated all subcomment counts in the tree"}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/sanbase/comments/entity_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ defmodule Sanbase.Comments.EntityComment do
Ecto.Multi.new()
|> Ecto.Multi.run(
:create_comment,
fn _changes -> Comment.create(user_id, content, parent_id) end
fn _repo, _changes -> Comment.create(user_id, content, parent_id) end
)
|> Ecto.Multi.run(:link_comment_and_entity, fn
%{create_comment: comment} ->
_repo, %{create_comment: comment} ->
link(entity, entity_id, comment.id)
end)
|> Repo.transaction()
Expand Down
2 changes: 1 addition & 1 deletion lib/sanbase/signals/evaluator/scheduler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ defmodule Sanbase.Signal.Scheduler do
user_trigger_id: id,
user_id: user_id,
payload: payload,
triggered_at: max_last_triggered(last_triggered),
triggered_at: max_last_triggered(last_triggered) |> DateTime.to_naive(),
data: %{user_trigger_data: identifier_kv_map}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/sanbase_web/graphql/middlewares/access_control.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ defmodule SanbaseWeb.Graphql.Middlewares.AccessControl do
middleware_args
) do
if Plan.AccessChecker.is_restricted?(query) do
IO.inspect("#{inspect(query)} IS RESTRICTED????")
restricted_query(resolution, middleware_args, query)
else
not_restricted_query(resolution, middleware_args)
Expand Down Expand Up @@ -253,7 +254,9 @@ defmodule SanbaseWeb.Graphql.Middlewares.AccessControl do
defp check_from_to_params(%Resolution{} = resolution), do: resolution
defp check_from_to_both_outside(%Resolution{state: :resolved} = resolution), do: resolution

defp check_from_to_both_outside(%Resolution{arguments: %{from: from, to: to}} = resolution) do
defp check_from_to_both_outside(
%Resolution{arguments: %{from: from, to: to}, context: context} = resolution
) do
case to_param_is_after_from(from, to) do
true ->
resolution
Expand All @@ -267,7 +270,10 @@ defmodule SanbaseWeb.Graphql.Middlewares.AccessControl do
{:error,
"""
Both `from` and `to` parameters are outside the allowed interval
you can query with your current subscription plan.
you can query with your current subscription plan #{context[:auth][:plan] || :free}.
`from` resolved to: #{from}
`to` resolved to: #{to}
"""}
)
end
Expand Down
8 changes: 5 additions & 3 deletions test/sanbase/statistics/statistics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,10 @@ defmodule Sanbase.StatisticsTest do
end

test "active users statistics" do
with_mock Sanbase.ClickhouseRepo,
query: fn _, _ -> {:ok, %{rows: [3]}} end do
rows = [3]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
statistics = Sanbase.Statistics.get_all()

assert {"active_users",
Expand All @@ -140,7 +142,7 @@ defmodule Sanbase.StatisticsTest do
"active_users_in_last_7d" => 3,
"active_users_in_last_30d" => 3
}} in statistics
end
end)
end

test "returns the number of users, which are subscribed" do
Expand Down
21 changes: 9 additions & 12 deletions test/sanbase_web/graphql/clickhouse/exchanges_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,14 @@ defmodule SanbaseWeb.Graphql.ExchangesTest do
end

test "test fetching volume for exchange", context do
with_mock Sanbase.ClickhouseRepo,
query: fn _, _ ->
{:ok,
%{
rows: [
[from_iso8601_to_unix!("2017-05-13T00:00:00Z"), 2000, 1000],
[from_iso8601_to_unix!("2017-05-15T00:00:00Z"), 1800, 1300],
[from_iso8601_to_unix!("2017-05-18T00:00:00Z"), 1000, 1100]
]
}}
end do
rows = [
[from_iso8601_to_unix!("2017-05-13T00:00:00Z"), 2000, 1000],
[from_iso8601_to_unix!("2017-05-15T00:00:00Z"), 1800, 1300],
[from_iso8601_to_unix!("2017-05-18T00:00:00Z"), 1000, 1100]
]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
query =
exchange_volume_query(
context.exchange,
Expand Down Expand Up @@ -88,7 +85,7 @@ defmodule SanbaseWeb.Graphql.ExchangesTest do
"exchange_outflow" => 1100
}
]
end
end)
end

describe "#exchangeMarketPairToSlugs" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
use SanbaseWeb.ConnCase, async: false

import Sanbase.TestHelpers
import SanbaseWeb.Graphql.TestHelpers
import Mock
import Sanbase.DateTimeUtils, only: [from_iso8601!: 1]
Expand Down Expand Up @@ -87,35 +88,25 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
end

test "historical balances when interval is bigger than balances values interval", context do
dt1 = DateTimeUtils.from_iso8601!("2017-05-11T00:00:00Z") |> DateTime.to_unix()
dt2 = DateTimeUtils.from_iso8601!("2017-05-12T00:00:00Z") |> DateTime.to_unix()
dt3 = DateTimeUtils.from_iso8601!("2017-05-13T00:00:00Z") |> DateTime.to_unix()
dt4 = DateTimeUtils.from_iso8601!("2017-05-14T00:00:00Z") |> DateTime.to_unix()
dt5 = DateTimeUtils.from_iso8601!("2017-05-15T00:00:00Z") |> DateTime.to_unix()
dt6 = DateTimeUtils.from_iso8601!("2017-05-16T00:00:00Z") |> DateTime.to_unix()
dt7 = DateTimeUtils.from_iso8601!("2017-05-17T00:00:00Z") |> DateTime.to_unix()
dt8 = DateTimeUtils.from_iso8601!("2017-05-18T00:00:00Z") |> DateTime.to_unix()
dt9 = DateTimeUtils.from_iso8601!("2017-05-19T00:00:00Z") |> DateTime.to_unix()
dt10 = DateTimeUtils.from_iso8601!("2017-05-20T00:00:00Z") |> DateTime.to_unix()

with_mock Sanbase.ClickhouseRepo, [:passthrough],
query: fn _, _ ->
{:ok,
%{
rows: [
[dt1, 0, 1],
[dt2, 0, 1],
[dt3, 2000 * @eth_decimals, 1],
[dt4, 1800 * @eth_decimals, 1],
[dt5, 0, 0],
[dt6, 1500 * @eth_decimals, 1],
[dt7, 1900 * @eth_decimals, 1],
[dt8, 1000 * @eth_decimals, 1],
[dt9, 0, 0],
[dt10, 0, 0]
]
}}
end do
[dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8, dt9, dt10] =
generate_datetimes(~U[2017-05-11T00:00:00Z], "1d", 10)
|> Enum.map(&DateTime.to_unix/1)

rows = [
[dt1, 0, 1],
[dt2, 0, 1],
[dt3, 2000 * @eth_decimals, 1],
[dt4, 1800 * @eth_decimals, 1],
[dt5, 0, 0],
[dt6, 1500 * @eth_decimals, 1],
[dt7, 1900 * @eth_decimals, 1],
[dt8, 1000 * @eth_decimals, 1],
[dt9, 0, 0],
[dt10, 0, 0]
]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
selector = %{infrastructure: "ETH", slug: "ethereum"}

query =
Expand Down Expand Up @@ -145,27 +136,24 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
%{"balance" => 1000.0, "datetime" => "2017-05-19T00:00:00Z"},
%{"balance" => 1000.0, "datetime" => "2017-05-20T00:00:00Z"}
]
end
end)
end

test "historical balances when last interval is not full", context do
dt1 = DateTimeUtils.from_iso8601!("2017-05-13T00:00:00Z") |> DateTime.to_unix()
dt2 = DateTimeUtils.from_iso8601!("2017-05-15T00:00:00Z") |> DateTime.to_unix()
dt3 = DateTimeUtils.from_iso8601!("2017-05-17T00:00:00Z") |> DateTime.to_unix()
[dt1, dt2, dt3] =
generate_datetimes(~U[2017-05-13T00:00:00Z], "2d", 3)
|> Enum.map(&DateTime.to_unix/1)

rows = [
[dt1, 2000 * @eth_decimals, 1],
[dt2, 1800 * @eth_decimals, 1],
[dt3, 1400 * @eth_decimals, 1]
]

with_mock Sanbase.ClickhouseRepo, [:passthrough],
query: fn _, _ ->
{:ok,
%{
rows: [
[dt1, 2000 * @eth_decimals, 1],
[dt2, 1800 * @eth_decimals, 1],
[dt3, 1400 * @eth_decimals, 1]
]
}}
end do
from = from_iso8601!("2017-05-13T00:00:00Z")
to = from_iso8601!("2017-05-18T00:00:00Z")
Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
from = ~U[2017-05-13T00:00:00Z]
to = ~U[2017-05-18T00:00:00Z]
selector = %{infrastructure: "ETH", slug: "ethereum"}
query = historical_balances_query(selector, context.address, from, to, "2d")

Expand All @@ -181,14 +169,14 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
%{"balance" => 1800.0, "datetime" => "2017-05-15T00:00:00Z"},
%{"balance" => 1400.0, "datetime" => "2017-05-17T00:00:00Z"}
]
end
end)
end

test "historical balances when query returns error", context do
error = "Some error description here"
error = "Something went wrong"

with_mock Sanbase.ClickhouseRepo,
query: fn _, _ -> {:error, error} end do
Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:error, error})
|> Sanbase.Mock.run_with_mocks(fn ->
selector = %{infrastructure: "ETH", slug: "ethereum"}

query =
Expand All @@ -215,11 +203,12 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
"Can't fetch Historical Balances for selector: #{inspect(selector)}, Reason: #{
inspect(error)
}"
end
end)
end

test "historical balances when query returns no rows", context do
with_mock Sanbase.ClickhouseRepo, query: fn _, _ -> {:ok, %{rows: []}} end do
Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: []}})
|> Sanbase.Mock.run_with_mocks(fn ->
selector = %{infrastructure: "ETH", slug: "ethereum"}

query =
Expand All @@ -237,28 +226,29 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do

historical_balance = json_response(result, 200)["data"]["historicalBalance"]
assert historical_balance == []
end
end)
end

test "historical balances with project with contract", context do
with_mock Sanbase.ClickhouseRepo,
query: fn _, _ ->
{:ok,
%{
rows: [
[~U[2017-05-11 00:00:00Z] |> DateTime.to_unix(), 0, 1],
[~U[2017-05-12 00:00:00Z] |> DateTime.to_unix(), 0, 1],
[~U[2017-05-13 00:00:00Z] |> DateTime.to_unix(), :math.pow(10, 18) * 2000, 1],
[~U[2017-05-14 00:00:00Z] |> DateTime.to_unix(), :math.pow(10, 18) * 1800, 1],
[~U[2017-05-15 00:00:00Z] |> DateTime.to_unix(), 0, 0],
[~U[2017-05-16 00:00:00Z] |> DateTime.to_unix(), :math.pow(10, 18) * 1500, 1],
[~U[2017-05-17 00:00:00Z] |> DateTime.to_unix(), :math.pow(10, 18) * 1900, 1],
[~U[2017-05-18 00:00:00Z] |> DateTime.to_unix(), :math.pow(10, 18) * 1000, 1],
[~U[2017-05-19 00:00:00Z] |> DateTime.to_unix(), 0, 0],
[~U[2017-05-20 00:00:00Z] |> DateTime.to_unix(), 0, 0]
]
}}
end do
[dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8, dt9, dt10] =
generate_datetimes(~U[2017-05-11T00:00:00Z], "1d", 10)
|> Enum.map(&DateTime.to_unix/1)

rows = [
[dt1, 0, 1],
[dt2, 0, 1],
[dt3, :math.pow(10, 18) * 2000, 1],
[dt4, :math.pow(10, 18) * 1800, 1],
[dt5, 0, 0],
[dt6, :math.pow(10, 18) * 1500, 1],
[dt7, :math.pow(10, 18) * 1900, 1],
[dt8, :math.pow(10, 18) * 1000, 1],
[dt9, 0, 0],
[dt10, 0, 0]
]

Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}})
|> Sanbase.Mock.run_with_mocks(fn ->
selector = %{infrastructure: "ETH", slug: context.project_with_contract.slug}

query =
Expand Down Expand Up @@ -289,7 +279,7 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
%{"balance" => 1000.0, "datetime" => "2017-05-19T00:00:00Z"},
%{"balance" => 1000.0, "datetime" => "2017-05-20T00:00:00Z"}
]
end
end)
end

test "historical balances with project without contract", context do
Expand Down Expand Up @@ -326,10 +316,8 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
test "historical balances when clickhouse returns error", context do
error = "Something bad happened"

with_mock Sanbase.ClickhouseRepo,
query: fn _, _ ->
{:error, error}
end do
Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:error, error})
|> Sanbase.Mock.run_with_mocks(fn ->
selector = %{infrastructure: "ETH", slug: context.project_with_contract.slug}

query =
Expand All @@ -355,7 +343,7 @@ defmodule SanbaseWeb.Graphql.Clickhouse.HistoricalBalancesTest do
end)

assert log =~ "Can't fetch Historical Balances for selector"
end
end)
end

defp historical_balances_query(selector, address, from, to, interval) do
Expand Down
Loading

0 comments on commit 3ea1248

Please sign in to comment.