Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaddox5 committed Dec 2, 2024
1 parent 9b3fc4a commit 140e2e9
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 28 deletions.
63 changes: 63 additions & 0 deletions test/fixtures/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,69 @@
"refresh_if_loaded_before": null,
"tags": [],
"vendor": "lg_mri"
},
"EIG-604": {
"disabled": false,
"name": null,
"app_id": "gl_eink_v2",
"app_params": {
"header": {
"direction_id": 0,
"route_id": "Green-E"
},
"departures": {
"sections": [
{
"header": {
"title": null,
"arrow": null,
"read_as": null
},
"filters": {
"max_minutes": null,
"route_directions": null
},
"query": {
"opts": {
"include_schedules": false
},
"params": {
"direction_id": 0,
"stop_ids": ["place-nuniv"],
"route_ids": ["Green-E"],
"route_type": null
}
},
"layout": {
"max": null,
"min": 1,
"base": null,
"include_later": false
},
"bidirectional": false
}
]
},
"footer": {
"stop_id": "place-nuniv"
},
"alerts": {
"stop_id": "70243"
},
"evergreen_content": [],
"line_map": {
"stop_id": "70243",
"direction_id": 0,
"route_id": "Green-E",
"station_id": "place-nuniv"
},
"platform_location": "front"
},
"device_id": null,
"hidden_from_screenplay": false,
"refresh_if_loaded_before": null,
"tags": [],
"vendor": "mercury"
}
}
}
29 changes: 1 addition & 28 deletions test/screens/v2/screen_data_test.exs
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
defmodule Screens.V2.ScreenDataTest.Stub do
defmacro candidate_generator(name, instances_fn) do
quote do
defmodule unquote(name) do
alias Screens.V2.CandidateGenerator
alias Screens.V2.Template.Builder
alias Screens.V2.WidgetInstance.Placeholder

@behaviour CandidateGenerator

@impl CandidateGenerator
def screen_template(), do: Builder.build_template({:screen, %{normal: [:main]}})

@impl CandidateGenerator
def candidate_instances(config) do
unquote(instances_fn).(config)
end

@impl CandidateGenerator
def audio_only_instances(_widgets, _config), do: []

defp placeholder(color), do: %Placeholder{color: color, slot_names: [:main]}
end
end
end
end

defmodule Screens.V2.ScreenDataTest do
use ExUnit.Case, async: true

alias ScreensConfig.Screen
alias Screens.V2.ScreenData
alias Screens.V2.WidgetInstance.MockWidget
alias Screens.V2.WidgetInstance.Placeholder
alias Screens.V2.ScreenDataTest.Stub
alias Screens.TestSupport.CandidateGeneratorStub, as: Stub

import ExUnit.CaptureLog
import Screens.Inject
Expand Down
79 changes: 79 additions & 0 deletions test/screens_web/controllers/v2/screen_api_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
defmodule ScreensWeb.V2.ScreenApiControllerTest do
use ScreensWeb.ConnCase

alias ScreensConfig.Screen
alias Screens.TestSupport.CandidateGeneratorStub, as: Stub

import Screens.Inject
import Mox
setup :verify_on_exit!

@cache injected(Screens.Config.Cache)
@parameters injected(Screens.V2.ScreenData.Parameters)

require Stub

Stub.candidate_generator(MercuryGenerator, fn _ -> [placeholder(:green)] end)
Stub.candidate_generator(LgMriGenerator, fn _ -> [placeholder(:red)] end)

describe "show/2" do
test "only returns flex_zone for Mercury screens", %{conn: conn} do
expect(@cache, :screen, fn
"EIG-604" ->
struct(Screen, app_id: :gl_eink_v2, vendor: :mercury)
end)

expect(@parameters, :variants, fn _ -> [nil] end)

stub(
@parameters,
:candidate_generator,
fn %Screen{vendor: :mercury}, nil -> MercuryGenerator end
)

stub(@parameters, :refresh_rate, fn _app_id -> 0 end)

conn = get(conn, "/v2/api/screen/EIG-604?last_refresh=2024-12-02T00:00:00Z")

assert %{
"audio_data" => "",
"data" => %{
"main" => %{"color" => "green", "type" => "placeholder"},
"type" => "normal"
},
"disabled" => false,
"flex_zone" => [],
"force_reload" => false,
"last_deploy_timestamp" => nil
} == json_response(conn, 200)
end

test "omits flex_zone from non-Mercury screens", %{conn: conn} do
expect(@cache, :screen, fn
"1401" ->
struct(Screen, app_id: :bus_shelter_v2, vendor: :lg_mri)
end)

expect(@parameters, :variants, fn _ -> [nil] end)

stub(
@parameters,
:candidate_generator,
fn %Screen{vendor: :lg_mri}, nil -> LgMriGenerator end
)

stub(@parameters, :refresh_rate, fn _app_id -> 0 end)

conn = get(conn, "/v2/api/screen/1401?last_refresh=2024-12-02T00:00:00Z")

assert %{
"data" => %{
"main" => %{"color" => "red", "type" => "placeholder"},
"type" => "normal"
},
"disabled" => false,
"force_reload" => false
} == json_response(conn, 200)
end
end
end

0 comments on commit 140e2e9

Please sign in to comment.