Feat/error logs (#16)

* added list of tasks to achieve

* update deps

* update elixir version in CI/CD pipeline

* update todo with branch push checks

* underscores separation of large numbers

* update elixir version and deps

* add basic module  docs

* remove IO.inspect call

* check if list is empty rather than traversing list for length

* revert empty list code

* duration tests

* add mime type tests

* use logger instread of inspect, remove todos

* replace list length check with Enum.empty

* replace list length check with !Enum.empty?

* numbers readability fix by using underscores on large numbers

* iucn parser replaced case with multiclause function and added test

* replace map and join with map_join

* fix multiple spec issues where map() must be inside a list
This commit is contained in:
2025-12-06 23:41:59 +01:00
committed by GitHub
parent 3d3059e77f
commit 02372f2076
27 changed files with 241 additions and 45 deletions
+8 -8
View File
@@ -7,15 +7,15 @@ defmodule PlantIdDiscordBot.ErrorHandlingTest do
test "do_identification/1 returns invokes logger on error" do
message = PlantNetFixtures.Message.message()
log = capture_log(fn ->
PlantIdDiscordBot.Cog.PlantNetMessage.do_identification(message)
log =
capture_log(fn ->
PlantIdDiscordBot.Cog.PlantNetMessage.do_identification(message)
assert_received {:create_message, 123456, content: "An error has occured. Please try again later."}
end)
assert_received {:create_message, 123_456,
content: "An error has occured. Please try again later."}
end)
IO.inspect(log)
assert log =~ "guild_id=#{message.guild_id}"
assert log =~ "guild_name=#{@guild.get_guild_name!(message.guild_id)}"
assert log =~ "guild_id=#{message.guild_id}"
assert log =~ "guild_name=#{@guild.get_guild_name!(message.guild_id)}"
end
end
+43
View File
@@ -0,0 +1,43 @@
defmodule PlantIdDiscordBotTest.FileServer.MimeTypes do
use ExUnit.Case, async: true
alias PlantIdDiscordBot.FileServer.MimeTypes
doctest MimeTypes
describe "detect_mime_type/1" do
test "detects webp" do
bin = <<0x52, 0x49, 0x46, 0x46, 0x00>>
assert MimeTypes.detect_mime_type(bin) == {:ok, "image/webp"}
end
test "detects jpeg" do
bin = <<0xFF, 0xD8, 0xFF, 0x00>>
assert MimeTypes.detect_mime_type(bin) == {:ok, "image/jpeg"}
end
test "detects png" do
bin = <<0x89, 0x50, 0x4E, 0x47, 0x00>>
assert MimeTypes.detect_mime_type(bin) == {:ok, "image/png"}
end
test "detects gif" do
bin = <<0x47, 0x49, 0x46, 0x38, 0x00>>
assert MimeTypes.detect_mime_type(bin) == {:ok, "image/gif"}
end
test "detects bmp" do
bin = <<0x42, 0x4D, 0x00>>
assert MimeTypes.detect_mime_type(bin) == {:ok, "image/bmp"}
end
test "detects tiff" do
bin = <<0x49, 0x49, 0x2A, 0x00, 0x00>>
assert MimeTypes.detect_mime_type(bin) == {:ok, "image/tiff"}
end
test "returns error for unsupported type" do
bin = <<0x00, 0x11, 0x22, 0x33>>
assert MimeTypes.detect_mime_type(bin) == {:error, "Unsupported file type"}
end
end
end
+15
View File
@@ -63,4 +63,19 @@ defmodule PlantIdDiscordBotTest.PlantNet.Parser do
"My best guess is **Prunus cerasifera** with a confidence of **88%**.\n\nSpecies info from plant databases:\n[GBIF](<https://www.gbif.org/species/3021730>) | [PFAF](<https://pfaf.org/user/Plant.aspx?LatinName=/Prunus+cerasifera>) | [POWO](<https://powo.science.kew.org/taxon/729568-1>)\n\nConservation status: Data Deficient\n\nAlternatives include **Prunus × cistena**."
end
end
describe "iucn_parser/1" do
test "return correct string for abbreviation" do
assert Parser.iucn_parser("DD") == "Data Deficient"
assert Parser.iucn_parser("LC") == "Least Concern"
assert Parser.iucn_parser("NT") == "Near Threatened"
assert Parser.iucn_parser("VU") == "Vulnerable"
assert Parser.iucn_parser("EN") == "Endangered"
assert Parser.iucn_parser("CR") == "Critically Endangered"
assert Parser.iucn_parser("EW") == "Extinct in the Wild"
assert Parser.iucn_parser("EX") == "Extinct"
assert Parser.iucn_parser("NE") == "Not Evaluated"
assert Parser.iucn_parser("ABC") == "Unknown"
end
end
end
+2 -2
View File
@@ -2,9 +2,9 @@ ExUnit.start()
defmodule PlantNetFixtures.Message do
@message %{
channel_id: 1178600825380155412,
channel_id: 1_178_600_825_380_155_412,
guild_id: 1_002_507_312_159_797_318,
attachments: [%{url: "http://invalid-url.com/image.jpg"}],
attachments: [%{url: "http://invalid-url.com/image.jpg"}]
}
def message(), do: @message
+39
View File
@@ -0,0 +1,39 @@
defmodule PlantIdDiscordBotTest.Utils.Duration do
use ExUnit.Case, async: true
alias PlantIdDiscordBot.Utils.Duration
doctest Duration
describe "sec_to_str/1" do
test "seconds" do
assert Duration.sec_to_str(5) == "5s"
refute Duration.sec_to_str(60) == "60s"
end
test "minutes" do
assert Duration.sec_to_str(60) == "1m"
assert Duration.sec_to_str(75) == "1m 15s"
refute Duration.sec_to_str(3600) == "60m"
end
test "hours" do
assert Duration.sec_to_str(3600) == "1h"
assert Duration.sec_to_str(7205) == "2h 5s"
assert Duration.sec_to_str(36_300) == "10h 5m"
assert Duration.sec_to_str(36_315) == "10h 5m 15s"
refute Duration.sec_to_str(86_400) == "24h"
end
test "days" do
assert Duration.sec_to_str(86_400) == "1d"
assert Duration.sec_to_str(86_715) == "1d 5m 15s"
refute Duration.sec_to_str(604_800) == "7d"
end
test "weeks" do
assert Duration.sec_to_str(604_800) == "1w"
assert Duration.sec_to_str(6_048_000) == "10w"
end
end
end