Skip to content

Commit

Permalink
Do not call MailJet functions outside tests where mailjet is tested
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanIvanoff committed Nov 25, 2024
1 parent c78a31e commit d187dcc
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 41 deletions.
4 changes: 4 additions & 0 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ config :sanbase, Sanbase.EventBus.KafkaExporterSubscriber,
can_send_after_interval: 0,
kafka_flush_timeout: 0

config :sanbase, Sanbase.EventBus.MetricRegistrySubscriber,
metric_registry_change_handler:
{Sanbase.EventBus.MetricRegistrySubscriber, :on_metric_registry_change_test_env}

config :sanbase, Sanbase.ExternalServices.RateLimiting.Server,
implementation_module: Sanbase.ExternalServices.RateLimiting.TestServer

Expand Down
8 changes: 8 additions & 0 deletions lib/sanbase/event_bus/metric_registry_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Sanbase.EventBus.MetricRegistrySubscriber do
use GenServer

alias Sanbase.Utils.Config

require Logger

def topics(), do: ["metric_registry_events"]
Expand Down Expand Up @@ -51,6 +52,13 @@ defmodule Sanbase.EventBus.MetricRegistrySubscriber do
:ok
end

def on_metric_registry_change_test_env(event_type, metric) do
# In test env this is the handler in order to avoid Ecto DBConnection
# ownership errors
Logger.warning("Metric Registry Change - Event Type: #{event_type}, Metric: #{metric}")
:ok
end

defp handle_event(
%{data: %{event_type: event_type, metric: metric}} = event,
event_shadow,
Expand Down
1 change: 0 additions & 1 deletion lib/sanbase/event_bus/user_events_subscriber.ex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ defmodule Sanbase.EventBus.UserEventsSubscriber do
) do
email = Sanbase.Accounts.get_user!(user_id).email
if email, do: Sanbase.Email.MailjetApi.client().subscribe(:metric_updates, email)

EventBus.mark_as_completed({__MODULE__, event_shadow})
state
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ defmodule Sanbase.Alert.TriggerTrendingWordsSendAtPredefiendTimeTest do
get_currently_trending_words: fn _, _ ->
{:ok, top_words()}
end do
assert capture_log(fn ->
Sanbase.Alert.Scheduler.run_alert(TrendingWordsTriggerSettings)
end) =~
log =
capture_log(fn ->
Sanbase.Alert.Scheduler.run_alert(TrendingWordsTriggerSettings)
end)

assert log =~
"In total 1/1 trending_words alerts were sent successfully"

{:ok, ut} = UserTrigger.by_user_and_id(context.user.id, context.trigger_trending_words.id)
Expand Down
2 changes: 2 additions & 0 deletions test/sanbase/metric_registry/change_suggestion_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ defmodule Sanbase.MetricRegisty.ChangeSuggestionTest do
alias Sanbase.Metric.Registry
alias Sanbase.Metric.Registry.ChangeSuggestion

@moduletag capture_log: true

test "creating a change suggestion" do
assert {:ok, metric} = Sanbase.Metric.Registry.by_name("price_usd_5m", "timeseries")

Expand Down
32 changes: 0 additions & 32 deletions test/sanbase/metric_registry/metric_registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,7 @@ defmodule Sanbase.MetricRegistyTest do
use Sanbase.DataCase
import ExUnit.CaptureLog

defmodule Handler do
require Logger

def on_metric_registry_change(event_type, metric) do
Logger.info("Metric Registry Change - Event Type: #{event_type}, Metric: #{metric}")
:ok
end
end

setup do
env = Application.get_env(:sanbase, Sanbase.EventBus.MetricRegistrySubscriber, [])

new_env =
Keyword.put(env, :metric_registry_change_handler, {Handler, :on_metric_registry_change})

Application.put_env(:sanbase, Sanbase.EventBus.MetricRegistrySubscriber, new_env)

on_exit(fn ->
Application.put_env(:sanbase, Sanbase.EventBus.MetricRegistrySubscriber, env)
end)

%{}
end

test "creating a new metric" do
Logger.configure(level: :info)

log =
capture_log(fn ->
assert {:ok, result} =
Expand All @@ -55,16 +29,12 @@ defmodule Sanbase.MetricRegistyTest do
Process.sleep(50)
end)

Logger.configure(level: :warning)

assert log =~ "Metric Registry Change - Event Type: create_metric_registry, Metric: my_metric"
end

test "updating a metric" do
assert {:ok, metric} = Sanbase.Metric.Registry.by_name("price_usd_5m", "timeseries")

Logger.configure(level: :info)

log =
capture_log(fn ->
assert {:ok, updated} =
Expand All @@ -82,8 +52,6 @@ defmodule Sanbase.MetricRegistyTest do
Process.sleep(50)
end)

Logger.configure(level: :warning)

assert log =~
"Metric Registry Change - Event Type: update_metric_registry, Metric: price_usd_5m"
end
Expand Down
2 changes: 2 additions & 0 deletions test/sanbase/notifications/notification_actions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule Sanbase.Notifications.NotificationActionsTest do
alias Sanbase.Repo

setup do
Application.put_env(:sanbase, :mailjet_mocked, true)
on_exit(fn -> Application.put_env(:sanbase, :mailjet_mocked, false) end)
create_default_templates()
:ok
end
Expand Down
7 changes: 2 additions & 5 deletions test/sanbase_web/graphql/metric/api_metric_metadata_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ defmodule SanbaseWeb.Graphql.ApiMetricMetadataTest do

assert Enum.all?(
metadata["availableSelectors"],
&Enum.member?(
[
&(&1 in [
"ADDRESS",
"BLOCKCHAIN_ADDRESS",
"BLOCKCHAIN",
Expand All @@ -68,9 +67,7 @@ defmodule SanbaseWeb.Graphql.ApiMetricMetadataTest do
"SOURCE",
"TEXT",
"TOKEN_ID"
],
&1
)
])
)

assert Enum.all?(
Expand Down

0 comments on commit d187dcc

Please sign in to comment.