Skip to content

Commit

Permalink
added test + logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ErinLMoore committed Sep 14, 2023
1 parent e79c9ef commit 3e9797a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
11 changes: 8 additions & 3 deletions apps/parse/lib/parse/alerts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,14 @@ defmodule Parse.Alerts do
end

defp do_localized_image(%{"localized_image" => [_ | _] = translations}, %{default: default}) do
case Enum.find(translations, &(&1["language"] == "en")) do
%{"language" => "en", "url" => url} -> url
_ -> default
translations =
translations
|> Enum.filter(&(&1["language"] == "en" or &1["language"] == nil))
|> Enum.sort(:desc)

case length(translations) >= 1 do
true -> hd(translations)["url"]
false -> default
end
end

Expand Down
53 changes: 51 additions & 2 deletions apps/parse/test/parse/alerts_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ defmodule Parse.AlertsTest do
assert [%Alert{description: "Good morning"}] = parse_json(map)
end

test "returns english image url over unspecified language" do
test "returns english image url over other language" do
url =
"https://dev-mbta.pantheonsite.io/sites/default/files/styles/max_2600x2600/public/media/2023-09/QuincyCenter-Braintree-EA.png"

Expand All @@ -461,6 +461,10 @@ defmodule Parse.AlertsTest do
"media_type" => "image/png",
"language" => "fr"
},
%{
"url" => "some_other_url",
"media_type" => "image/png"
},
%{
"url" => url,
"media_type" => "image/png",
Expand All @@ -482,7 +486,7 @@ defmodule Parse.AlertsTest do
assert [%Alert{image: ^url}] = parse_json(map)
end

test "returns english image url over other language" do
test "returns english image url over unspecified language" do
url =
"https://dev-mbta.pantheonsite.io/sites/default/files/styles/max_2600x2600/public/media/2023-09/QuincyCenter-Braintree-EA.png"

Expand Down Expand Up @@ -527,6 +531,51 @@ defmodule Parse.AlertsTest do
assert [%Alert{image: ^url}] = parse_json(map)
end

test "returns unspecified url over non-english language if english is not present" do
url =
"https://dev-mbta.pantheonsite.io/sites/default/files/styles/max_2600x2600/public/media/2023-09/QuincyCenter-Braintree-EA.png"

map = %{
"timestamp" => "1496832813",
"alerts" => [
%{
"active_period" => [],
"alert_lifecycle" => "NEW",
"cause" => "UNKNOWN_CAUSE",
"created_timestamp" => 1_494_947_991,
"description_text" => [],
"duration_certainty" => "KNOWN",
"effect" => "NO_SERVICE",
"effect_detail" => "STATION_CLOSURE",
"header_text" => [],
"id" => "113791",
"image" => %{
"localized_image" => [
%{
"url" => "some_other_url",
"media_type" => "image/png",
"language" => "fr"
},
%{
"url" => url,
"media_type" => "image/png"
}
]
},
"informed_entity" => [%{}],
"last_modified_timestamp" => 1_494_947_991,
"last_push_notification_timestamp" => 1_494_947_991,
"service_effect_text" => [],
"severity" => 3,
"short_header_text" => [],
"timeframe_text" => []
}
]
}

assert [%Alert{image: ^url}] = parse_json(map)
end

test "still returns image url even if no language specified" do
url =
"https://dev-mbta.pantheonsite.io/sites/default/files/styles/max_2600x2600/public/media/2023-09/QuincyCenter-Braintree-EA.png"
Expand Down

0 comments on commit 3e9797a

Please sign in to comment.