From 3ea1248260e551a4d8848aa6bc1b6c9aa81640b5 Mon Sep 17 00:00:00 2001 From: Ivan Ivanov Date: Fri, 22 May 2020 11:58:50 +0300 Subject: [PATCH] Rework tests --- config/config.exs | 3 +- lib/sanbase/billing/graphql_schema.ex | 6 +- lib/sanbase/comments/comment.ex | 6 +- lib/sanbase/comments/entity_comment.ex | 4 +- lib/sanbase/signals/evaluator/scheduler.ex | 2 +- .../graphql/middlewares/access_control.ex | 10 +- test/sanbase/statistics/statistics_test.exs | 8 +- .../graphql/clickhouse/exchanges_test.exs | 21 ++- .../historical_balances_test.exs | 142 ++++++++---------- .../dev_activity/dev_activity_api_test.exs | 16 +- .../project_api_eth_spent_over_time_test.exs | 71 +++------ .../projects/project_api_eth_spent_test.exs | 128 +++++++--------- 12 files changed, 176 insertions(+), 241 deletions(-) diff --git a/config/config.exs b/config/config.exs index 77ecc77ef7..f76c773710 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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} diff --git a/lib/sanbase/billing/graphql_schema.ex b/lib/sanbase/billing/graphql_schema.ex index afdf9ba440..b65e1e7958 100644 --- a/lib/sanbase/billing/graphql_schema.ex +++ b/lib/sanbase/billing/graphql_schema.ex @@ -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 diff --git a/lib/sanbase/comments/comment.ex b/lib/sanbase/comments/comment.ex index acb116f76c..c7ad044ccc 100644 --- a/lib/sanbase/comments/comment.ex +++ b/lib/sanbase/comments/comment.ex @@ -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() @@ -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 @@ -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 diff --git a/lib/sanbase/comments/entity_comment.ex b/lib/sanbase/comments/entity_comment.ex index 24cf6a1902..6d07392771 100644 --- a/lib/sanbase/comments/entity_comment.ex +++ b/lib/sanbase/comments/entity_comment.ex @@ -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() diff --git a/lib/sanbase/signals/evaluator/scheduler.ex b/lib/sanbase/signals/evaluator/scheduler.ex index bb183e7825..a53ddf802e 100644 --- a/lib/sanbase/signals/evaluator/scheduler.ex +++ b/lib/sanbase/signals/evaluator/scheduler.ex @@ -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} } diff --git a/lib/sanbase_web/graphql/middlewares/access_control.ex b/lib/sanbase_web/graphql/middlewares/access_control.ex index 9d42186017..998e4a4c42 100644 --- a/lib/sanbase_web/graphql/middlewares/access_control.ex +++ b/lib/sanbase_web/graphql/middlewares/access_control.ex @@ -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) @@ -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 @@ -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 diff --git a/test/sanbase/statistics/statistics_test.exs b/test/sanbase/statistics/statistics_test.exs index 24942a1fca..c7cce29d34 100644 --- a/test/sanbase/statistics/statistics_test.exs +++ b/test/sanbase/statistics/statistics_test.exs @@ -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", @@ -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 diff --git a/test/sanbase_web/graphql/clickhouse/exchanges_test.exs b/test/sanbase_web/graphql/clickhouse/exchanges_test.exs index bbe5a649f8..783065ad7b 100644 --- a/test/sanbase_web/graphql/clickhouse/exchanges_test.exs +++ b/test/sanbase_web/graphql/clickhouse/exchanges_test.exs @@ -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, @@ -88,7 +85,7 @@ defmodule SanbaseWeb.Graphql.ExchangesTest do "exchange_outflow" => 1100 } ] - end + end) end describe "#exchangeMarketPairToSlugs" do diff --git a/test/sanbase_web/graphql/clickhouse/historical_balance/historical_balances_test.exs b/test/sanbase_web/graphql/clickhouse/historical_balance/historical_balances_test.exs index 0bc8af70cd..a568c3b26b 100644 --- a/test/sanbase_web/graphql/clickhouse/historical_balance/historical_balances_test.exs +++ b/test/sanbase_web/graphql/clickhouse/historical_balance/historical_balances_test.exs @@ -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] @@ -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 = @@ -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") @@ -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 = @@ -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 = @@ -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 = @@ -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 @@ -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 = @@ -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 diff --git a/test/sanbase_web/graphql/dev_activity/dev_activity_api_test.exs b/test/sanbase_web/graphql/dev_activity/dev_activity_api_test.exs index fe72764185..0594d58c19 100644 --- a/test/sanbase_web/graphql/dev_activity/dev_activity_api_test.exs +++ b/test/sanbase_web/graphql/dev_activity/dev_activity_api_test.exs @@ -24,23 +24,15 @@ defmodule SanbaseWeb.Graphql.ProjectApiGithubTest do market_segments: [market_segment] }) - project3 = - insert(:random_project, %{ - github_organizations: [], - market_segments: [] - }) - - dt1 = "2019-01-01T00:00:00Z" |> from_iso8601!() - dt2 = "2019-01-02T00:00:00Z" |> from_iso8601!() - dt3 = "2019-01-03T00:00:00Z" |> from_iso8601!() + project3 = insert(:random_project, %{github_organizations: [], market_segments: []}) %{ project1: project1, project2: project2, project3: project3, - dt1: dt1, - dt2: dt2, - dt3: dt3, + dt1: ~U[2019-01-01T00:00:00Z], + dt2: ~U[2019-01-02T00:00:00Z], + dt3: ~U[2019-01-03T00:00:00Z], market_segment: market_segment, market_segment_without_projects: market_segment_without_projects } diff --git a/test/sanbase_web/graphql/projects/project_api_eth_spent_over_time_test.exs b/test/sanbase_web/graphql/projects/project_api_eth_spent_over_time_test.exs index 731eb14d28..c6c63ff6b3 100644 --- a/test/sanbase_web/graphql/projects/project_api_eth_spent_over_time_test.exs +++ b/test/sanbase_web/graphql/projects/project_api_eth_spent_over_time_test.exs @@ -2,6 +2,7 @@ defmodule SanbaseWeb.Graphql.ProjectApiEthSpentOverTimeTest do use SanbaseWeb.ConnCase, async: false import Mock + import Sanbase.TestHelpers import SanbaseWeb.Graphql.TestHelpers import Sanbase.Factory @@ -25,27 +26,20 @@ defmodule SanbaseWeb.Graphql.ProjectApiEthSpentOverTimeTest do end test "project eth spent over time", context do - dt1 = DateTimeUtils.from_iso8601!("2017-05-13T00:00:00Z") |> DateTime.to_unix() - dt2 = DateTimeUtils.from_iso8601!("2017-05-14T00:00:00Z") |> DateTime.to_unix() - dt3 = DateTimeUtils.from_iso8601!("2017-05-15T00:00:00Z") |> DateTime.to_unix() - dt4 = DateTimeUtils.from_iso8601!("2017-05-16T00:00:00Z") |> DateTime.to_unix() - dt5 = DateTimeUtils.from_iso8601!("2017-05-17T00:00:00Z") |> DateTime.to_unix() - dt6 = DateTimeUtils.from_iso8601!("2017-05-18T00:00:00Z") |> DateTime.to_unix() + [dt1, dt2, dt3, dt4, dt5, dt6] = + generate_datetimes(~U[2017-05-13T00:00:00Z], "1d", 6) |> Enum.map(&DateTime.to_unix/1) + + rows = [ + [dt1, -500 * @eth_decimals], + [dt2, -1500 * @eth_decimals], + [dt3, -6000 * @eth_decimals], + [dt4, 0], + [dt5, 0], + [dt6, -6500 * @eth_decimals] + ] - with_mock Sanbase.ClickhouseRepo, [:passthrough], - query: fn _, _ -> - {:ok, - %{ - rows: [ - [dt1, -500 * @eth_decimals], - [dt2, -1500 * @eth_decimals], - [dt3, -6000 * @eth_decimals], - [dt4, 0], - [dt5, 0], - [dt6, -6500 * @eth_decimals] - ] - }} - end do + Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}}) + |> Sanbase.Mock.run_with_mocks(fn -> query = """ { project(id: #{context.project.id}) { @@ -68,35 +62,12 @@ defmodule SanbaseWeb.Graphql.ProjectApiEthSpentOverTimeTest do assert length(eth_spent_over_time) == 6 - assert %{ - "datetime" => "2017-05-13T00:00:00Z", - "ethSpent" => 500.0 - } in eth_spent_over_time - - assert %{ - "datetime" => "2017-05-14T00:00:00Z", - "ethSpent" => 1500.0 - } in eth_spent_over_time - - assert %{ - "datetime" => "2017-05-15T00:00:00Z", - "ethSpent" => 6000.0 - } in eth_spent_over_time - - assert %{ - "datetime" => "2017-05-16T00:00:00Z", - "ethSpent" => 0.0 - } in eth_spent_over_time - - assert %{ - "datetime" => "2017-05-17T00:00:00Z", - "ethSpent" => 0.0 - } in eth_spent_over_time - - assert %{ - "datetime" => "2017-05-18T00:00:00Z", - "ethSpent" => 6500.0 - } in eth_spent_over_time - end + assert %{"datetime" => "2017-05-13T00:00:00Z", "ethSpent" => 500.0} in eth_spent_over_time + assert %{"datetime" => "2017-05-14T00:00:00Z", "ethSpent" => 1500.0} in eth_spent_over_time + assert %{"datetime" => "2017-05-15T00:00:00Z", "ethSpent" => 6000.0} in eth_spent_over_time + assert %{"datetime" => "2017-05-16T00:00:00Z", "ethSpent" => 0.0} in eth_spent_over_time + assert %{"datetime" => "2017-05-17T00:00:00Z", "ethSpent" => 0.0} in eth_spent_over_time + assert %{"datetime" => "2017-05-18T00:00:00Z", "ethSpent" => 6500.0} in eth_spent_over_time + end) end end diff --git a/test/sanbase_web/graphql/projects/project_api_eth_spent_test.exs b/test/sanbase_web/graphql/projects/project_api_eth_spent_test.exs index 92a407863f..28e268bb36 100644 --- a/test/sanbase_web/graphql/projects/project_api_eth_spent_test.exs +++ b/test/sanbase_web/graphql/projects/project_api_eth_spent_test.exs @@ -3,6 +3,7 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do import Mock import Sanbase.Factory + import Sanbase.TestHelpers import SanbaseWeb.Graphql.TestHelpers @eth_decimals 1_000_000_000_000_000_000 @@ -14,19 +15,12 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do eth_infr = insert(:infrastructure, %{code: "ETH"}) - p = - insert(:project, %{ - name: "Santiment", - ticker: "SAN", - slug: "santiment", - infrastructure_id: eth_infr.id, - main_contract_address: "0x" <> rand_hex_str() - }) + project = insert(:random_erc20_project) project_address = p.eth_addresses |> List.first() [ - project: p, + project: project, project_address: project_address.address, dates_day_diff1: Timex.diff(datetime1, datetime3, :days) + 1, expected_sum1: 20_000, @@ -38,20 +32,17 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do end test "project total eth spent whole interval", context do - with_mock Sanbase.ClickhouseRepo, [:passthrough], - query: fn _, _ -> - {:ok, - %{ - rows: [ - [ - context.project_address, - 30_000 * @eth_decimals, - 10_000 * @eth_decimals, - -20_000 * @eth_decimals - ] - ] - }} - end do + rows = [ + [ + context.project_address, + 30_000 * @eth_decimals, + 10_000 * @eth_decimals, + -20_000 * @eth_decimals + ] + ] + + Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}}) + |> Sanbase.Mock.run_with_mocks(fn -> query = """ { project(id: #{context.project.id}) { @@ -67,26 +58,23 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do trx_sum = json_response(result, 200)["data"]["project"] assert trx_sum == %{"ethSpent" => context.expected_sum1} - end + end) end test "project total eth spent part of interval", context do eth_spent = 4500 - with_mock Sanbase.ClickhouseRepo, [:passthrough], - query: fn _, _ -> - {:ok, - %{ - rows: [ - [ - context.project_address, - 20_000 * @eth_decimals, - 15_500 * @eth_decimals, - -4500 * @eth_decimals - ] - ] - }} - end do + rows = [ + [ + context.project_address, + 20_000 * @eth_decimals, + 15_500 * @eth_decimals, + -4500 * @eth_decimals + ] + ] + + Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}}) + |> Sanbase.Mock.run_with_mocks(fn -> query = """ { project(id: #{context.project.id}) { @@ -102,26 +90,23 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do trx_sum = json_response(result, 200)["data"]["project"] assert trx_sum == %{"ethSpent" => eth_spent} - end + end) end test "eth spent by erc20 projects", context do eth_spent = 30_000 - with_mock Sanbase.ClickhouseRepo, [:passthrough], - query: fn _, _ -> - {:ok, - %{ - rows: [ - [ - context.project_address, - 100_000 * @eth_decimals, - 70_000 * @eth_decimals, - -30_000 * @eth_decimals - ] - ] - }} - end do + rows = [ + [ + context.project_address, + 100_000 * @eth_decimals, + 70_000 * @eth_decimals, + -30_000 * @eth_decimals + ] + ] + + Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}}) + |> Sanbase.Mock.run_with_mocks(fn -> query = """ { ethSpentByErc20Projects( @@ -137,29 +122,24 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do total_eth_spent = json_response(result, 200)["data"]["ethSpentByErc20Projects"] assert total_eth_spent == eth_spent - end + end) end test "eth spent over time by erc20 projects", context do - dt1 = Timex.now() |> DateTime.to_unix() - dt2 = Timex.shift(Timex.now(), days: -1) |> DateTime.to_unix() - dt3 = Timex.shift(Timex.now(), days: -2) |> DateTime.to_unix() - dt4 = Timex.shift(Timex.now(), days: -3) |> DateTime.to_unix() - dt5 = Timex.shift(Timex.now(), days: -4) |> DateTime.to_unix() - - with_mock Sanbase.ClickhouseRepo, [:passthrough], - query: fn _, _ -> - {:ok, - %{ - rows: [ - [dt1, -16_500 * @eth_decimals], - [dt2, -5500 * @eth_decimals], - [dt3, -3500 * @eth_decimals], - [dt4, -2500 * @eth_decimals], - [dt5, -500 * @eth_decimals] - ] - }} - end do + [dt1, dt2, dt3, dt4, dt5] = + generate_datetimes(Timex.shift(Timex.now(), days: -4), "1d", 5) + |> Enum.map(&DateTime.to_unix/1) + + rows = [ + [dt1, -16_500 * @eth_decimals], + [dt2, -5500 * @eth_decimals], + [dt3, -3500 * @eth_decimals], + [dt4, -2500 * @eth_decimals], + [dt5, -500 * @eth_decimals] + ] + + Sanbase.Mock.prepare_mock2(&Sanbase.ClickhouseRepo.query/2, {:ok, %{rows: rows}}) + |> Sanbase.Mock.run_with_mocks(fn -> query = """ { ethSpentOverTimeByErc20Projects( @@ -182,7 +162,7 @@ defmodule SanbaseWeb.Graphql.ProjecApiEthSpentTest do assert %{"ethSpent" => 3500.0} in total_spent assert %{"ethSpent" => 2500.0} in total_spent assert %{"ethSpent" => 500.0} in total_spent - end + end) end # Private functions