mirror of
https://github.com/TheRealOwenRees/plantid-discord-bot.git
synced 2026-07-23 04:26:57 +00:00
basic error handling test
This commit is contained in:
+1
-1
@@ -2,7 +2,7 @@ import Config
|
|||||||
|
|
||||||
config :plantid_discord_bot,
|
config :plantid_discord_bot,
|
||||||
api: Nostrum.Api,
|
api: Nostrum.Api,
|
||||||
guild: Nostrum.Cache.GuildCache,
|
guild: PlantIdDiscordBot.Guild,
|
||||||
port: 4321
|
port: 4321
|
||||||
|
|
||||||
config :plantid_discord_bot, :environment, :dev
|
config :plantid_discord_bot, :environment, :dev
|
||||||
|
|||||||
+1
-1
@@ -2,7 +2,7 @@ import Config
|
|||||||
|
|
||||||
config :plantid_discord_bot,
|
config :plantid_discord_bot,
|
||||||
api: Nostrum.Api,
|
api: Nostrum.Api,
|
||||||
guild: Nostrum.Cache.GuildCache,
|
guild: PlantIdDiscordBot.Guild,
|
||||||
port: 4000
|
port: 4000
|
||||||
|
|
||||||
config :plantid_discord_bot, :environment, :prod
|
config :plantid_discord_bot, :environment, :prod
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ config :plantid_discord_bot, :environment, :test
|
|||||||
|
|
||||||
config :logger, :console,
|
config :logger, :console,
|
||||||
format: "$time $metadata[$level] $message\n",
|
format: "$time $metadata[$level] $message\n",
|
||||||
|
metadata: [:guild_id, :guild_name],
|
||||||
level: :debug
|
level: :debug
|
||||||
|
|||||||
@@ -3,6 +3,13 @@ defmodule PlantIdDiscordBotTest.Mocks.Nostrum.Api do
|
|||||||
Mocks the Nostrum.Api module.
|
Mocks the Nostrum.Api module.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# def create_message(_channel_id, content), do: {:ok, content}
|
||||||
|
|
||||||
|
def create_message(_channel_id, content) do
|
||||||
|
send(self(), {:create_message, 123456, content})
|
||||||
|
{:ok, content}
|
||||||
|
end
|
||||||
|
|
||||||
def create_interaction_response(_interaction, response), do: {:ok, response}
|
def create_interaction_response(_interaction, response), do: {:ok, response}
|
||||||
|
|
||||||
def get_application_information() do
|
def get_application_information() do
|
||||||
|
|||||||
@@ -7,7 +7,11 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
alias PlantIdDiscordBot.PlantNet.Parser
|
alias PlantIdDiscordBot.PlantNet.Parser
|
||||||
alias PlantIdDiscordBot.FileServer.File
|
alias PlantIdDiscordBot.FileServer.File
|
||||||
|
|
||||||
# @guild Application.compile_env(:plantid_discord_bot, :guild)
|
# mock modules
|
||||||
|
@api Application.compile_env(:plantid_discord_bot, :api)
|
||||||
|
@guild Application.compile_env(:plantid_discord_bot, :guild)
|
||||||
|
|
||||||
|
# env vars
|
||||||
@plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url)
|
@plantnet_api_base_url Application.compile_env(:plantid_discord_bot, :plantnet_api_base_url)
|
||||||
@max_results Application.compile_env(:plantid_discord_bot, :max_results)
|
@max_results Application.compile_env(:plantid_discord_bot, :max_results)
|
||||||
|
|
||||||
@@ -35,10 +39,10 @@ defmodule PlantIdDiscordBot.Cog.PlantNetMessage do
|
|||||||
e ->
|
e ->
|
||||||
Logger.error(Exception.format(:error, e, __STACKTRACE__),
|
Logger.error(Exception.format(:error, e, __STACKTRACE__),
|
||||||
guild_id: message.guild_id,
|
guild_id: message.guild_id,
|
||||||
guild_name: Guild.get_guild_name!(message.guild_id)
|
guild_name: @guild.get_guild_name!(message.guild_id)
|
||||||
)
|
)
|
||||||
|
|
||||||
Api.create_message(message.channel_id,
|
@api.create_message(message.channel_id,
|
||||||
content: "An error has occured. Please try again later."
|
content: "An error has occured. Please try again later."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,19 @@
|
|||||||
defmodule PlantIdDiscordBot.ErrorHandlingTest do
|
defmodule PlantIdDiscordBot.ErrorHandlingTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
|
import ExUnit.CaptureLog
|
||||||
|
|
||||||
test "do_identification/1 returns nil when an error occurs" do
|
@guild Application.compile_env(:plantid_discord_bot, :guild)
|
||||||
|
|
||||||
|
test "do_identification/1 returns invokes logger on error" do
|
||||||
message = PlantNetFixtures.Message.message()
|
message = PlantNetFixtures.Message.message()
|
||||||
|
|
||||||
|
log = capture_log(fn ->
|
||||||
PlantIdDiscordBot.Cog.PlantNetMessage.do_identification(message)
|
PlantIdDiscordBot.Cog.PlantNetMessage.do_identification(message)
|
||||||
|
|
||||||
|
assert_received {:create_message, 123456, content: "An error has occured. Please try again later."}
|
||||||
|
end)
|
||||||
|
|
||||||
|
assert log =~ "guild_id=#{message.guild_id}"
|
||||||
|
assert log =~ "guild_name=#{@guild.get_guild_name!(message.guild_id)}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ ExUnit.start()
|
|||||||
|
|
||||||
defmodule PlantNetFixtures.Message do
|
defmodule PlantNetFixtures.Message do
|
||||||
@message %{
|
@message %{
|
||||||
guild_id: 1_002_507_312_159_797_318
|
channel_id: 1178600825380155412,
|
||||||
|
guild_id: 1_002_507_312_159_797_318,
|
||||||
|
attachments: [%{url: "http://invalid-url.com/image.jpg"}],
|
||||||
}
|
}
|
||||||
|
|
||||||
def message(), do: @message
|
def message(), do: @message
|
||||||
|
|||||||
Reference in New Issue
Block a user