Skip to content

Commit

Permalink
test: Rollbar.Item (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy authored Jun 11, 2024
1 parent bf73898 commit dab2954
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/tower_rollbar/rollbar/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule TowerRollbar.Rollbar.Item do
}
}
}
|> item_from_body(options)
|> item_from_body(Keyword.merge([level: :error], options))
end

def from_message(message, options \\ []) when is_binary(message) do
Expand All @@ -19,7 +19,7 @@ defmodule TowerRollbar.Rollbar.Item do
"body" => message
}
}
|> item_from_body(options)
|> item_from_body(Keyword.merge([level: :info], options))
end

defp item_from_body(body, options) when is_map(body) do
Expand All @@ -32,6 +32,7 @@ defmodule TowerRollbar.Rollbar.Item do
"data" =>
%{
"environment" => environment(),
"timestamp" => :os.system_time(:second),
"body" => body
}
|> maybe_put_request_data(plug_conn)
Expand Down
54 changes: 54 additions & 0 deletions test/tower_rollbar/rollbar/item_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
defmodule TowerRollbar.Rollbar.ItemTest do
use ExUnit.Case
doctest TowerRollbar.Rollbar.Item

alias TowerRollbar.Rollbar

test "from_exception" do
Application.put_env(:tower_rollbar, :environment, "test")

item =
try do
raise "a test"
rescue
exception in RuntimeError ->
Rollbar.Item.from_exception(exception, __STACKTRACE__)
end

assert %{
"data" => %{
"environment" => "test",
"timestamp" => _,
"level" => "error",
"body" => %{
"trace" => %{
"frames" => [],
"exception" => %{
"class" => "RuntimeError",
"message" => "a test"
}
}
}
}
} = item
end

test "from_message" do
Application.put_env(:tower_rollbar, :environment, "test")

item = Rollbar.Item.from_message("something interesting happened")

assert %{
"data" => %{
"environment" => "test",
"timestamp" => _,
"level" => "info",
"body" => %{
"message" => %{
"body" => "something interesting happened"
}
}
}
} = item
end
end

0 comments on commit dab2954

Please sign in to comment.