|
| 1 | +defmodule ApiWeb.StopEventViewTest do |
| 2 | + use ApiWeb.ConnCase |
| 3 | + |
| 4 | + # Bring render/3 and render_to_string/3 for testing custom views |
| 5 | + import Phoenix.View |
| 6 | + |
| 7 | + alias Model.StopEvent |
| 8 | + |
| 9 | + @stop_event %StopEvent{ |
| 10 | + id: "trip1-route1-v1-1", |
| 11 | + vehicle_id: "v1", |
| 12 | + start_date: ~D[2026-02-24], |
| 13 | + trip_id: "trip1", |
| 14 | + direction_id: 0, |
| 15 | + route_id: "route1", |
| 16 | + start_time: "10:00:00", |
| 17 | + revenue: :REVENUE, |
| 18 | + stop_id: "stop1", |
| 19 | + stop_sequence: 1, |
| 20 | + arrived: ~U[2026-02-24 15:28:06Z], |
| 21 | + departed: ~U[2026-02-24 15:40:46Z] |
| 22 | + } |
| 23 | + |
| 24 | + setup %{conn: conn} do |
| 25 | + conn = Phoenix.Controller.put_view(conn, ApiWeb.StopEventView) |
| 26 | + {:ok, %{conn: conn}} |
| 27 | + end |
| 28 | + |
| 29 | + test "renders stop event with all attributes", %{conn: conn} do |
| 30 | + rendered = render(ApiWeb.StopEventView, "index.json-api", data: @stop_event, conn: conn) |
| 31 | + |
| 32 | + assert rendered["data"]["type"] == "stop_event" |
| 33 | + assert rendered["data"]["id"] == "trip1-route1-v1-1" |
| 34 | + |
| 35 | + assert rendered["data"]["attributes"] == %{ |
| 36 | + "vehicle_id" => "v1", |
| 37 | + "start_date" => ~D[2026-02-24], |
| 38 | + "trip_id" => "trip1", |
| 39 | + "direction_id" => 0, |
| 40 | + "route_id" => "route1", |
| 41 | + "start_time" => "10:00:00", |
| 42 | + "revenue" => :REVENUE, |
| 43 | + "stop_id" => "stop1", |
| 44 | + "stop_sequence" => 1, |
| 45 | + "arrived" => "2026-02-24T15:28:06Z", |
| 46 | + "departed" => "2026-02-24T15:40:46Z" |
| 47 | + } |
| 48 | + end |
| 49 | + |
| 50 | + test "renders stop event with nil arrived (first stop)", %{conn: conn} do |
| 51 | + stop_event = %StopEvent{@stop_event | arrived: nil} |
| 52 | + rendered = render(ApiWeb.StopEventView, "index.json-api", data: stop_event, conn: conn) |
| 53 | + |
| 54 | + assert rendered["data"]["attributes"]["arrived"] == nil |
| 55 | + assert rendered["data"]["attributes"]["departed"] == "2026-02-24T15:40:46Z" |
| 56 | + end |
| 57 | + |
| 58 | + test "renders stop event with nil departed (last stop)", %{conn: conn} do |
| 59 | + stop_event = %StopEvent{@stop_event | departed: nil} |
| 60 | + rendered = render(ApiWeb.StopEventView, "index.json-api", data: stop_event, conn: conn) |
| 61 | + |
| 62 | + assert rendered["data"]["attributes"]["arrived"] == "2026-02-24T15:28:06Z" |
| 63 | + assert rendered["data"]["attributes"]["departed"] == nil |
| 64 | + end |
| 65 | + |
| 66 | + test "renders non-revenue trip", %{conn: conn} do |
| 67 | + stop_event = %StopEvent{@stop_event | revenue: :NON_REVENUE} |
| 68 | + rendered = render(ApiWeb.StopEventView, "index.json-api", data: stop_event, conn: conn) |
| 69 | + |
| 70 | + assert rendered["data"]["attributes"]["revenue"] == :NON_REVENUE |
| 71 | + end |
| 72 | + |
| 73 | + test "does not include values which aren't requested", %{conn: conn} do |
| 74 | + # JSON:API sparse fieldsets: when client requests empty field list, |
| 75 | + # no attributes are returned (only id, type, and relationships) |
| 76 | + conn = assign(conn, :opts, %{fields: %{"stop_event" => []}}) |
| 77 | + |
| 78 | + rendered = |
| 79 | + render(ApiWeb.StopEventView, "index.json-api", |
| 80 | + data: @stop_event, |
| 81 | + conn: conn, |
| 82 | + opts: conn.assigns.opts |
| 83 | + ) |
| 84 | + |
| 85 | + assert rendered["data"]["attributes"] == %{} |
| 86 | + end |
| 87 | + |
| 88 | + describe "relationships" do |
| 89 | + test "includes trip relationship when requested", %{conn: conn} do |
| 90 | + trip = %Model.Trip{ |
| 91 | + id: "trip1", |
| 92 | + route_id: "route1", |
| 93 | + direction_id: 0 |
| 94 | + } |
| 95 | + |
| 96 | + State.Trip.new_state([trip]) |
| 97 | + |
| 98 | + rendered = |
| 99 | + render(ApiWeb.StopEventView, "index.json-api", |
| 100 | + data: @stop_event, |
| 101 | + conn: conn, |
| 102 | + opts: %{include: "trip"} |
| 103 | + ) |
| 104 | + |
| 105 | + assert get_in(rendered, ["data", "relationships", "trip", "data", "id"]) == "trip1" |
| 106 | + end |
| 107 | + |
| 108 | + test "includes stop relationship when requested", %{conn: conn} do |
| 109 | + stop = %Model.Stop{ |
| 110 | + id: "stop1", |
| 111 | + name: "Test Stop" |
| 112 | + } |
| 113 | + |
| 114 | + State.Stop.new_state([stop]) |
| 115 | + |
| 116 | + rendered = |
| 117 | + render(ApiWeb.StopEventView, "index.json-api", |
| 118 | + data: @stop_event, |
| 119 | + conn: conn, |
| 120 | + opts: %{include: "stop"} |
| 121 | + ) |
| 122 | + |
| 123 | + assert get_in(rendered, ["data", "relationships", "stop", "data", "id"]) == "stop1" |
| 124 | + end |
| 125 | + |
| 126 | + test "includes route relationship when requested", %{conn: conn} do |
| 127 | + route = %Model.Route{ |
| 128 | + id: "route1", |
| 129 | + type: 3 |
| 130 | + } |
| 131 | + |
| 132 | + State.Route.new_state([route]) |
| 133 | + |
| 134 | + rendered = |
| 135 | + render(ApiWeb.StopEventView, "index.json-api", |
| 136 | + data: @stop_event, |
| 137 | + conn: conn, |
| 138 | + opts: %{include: "route"} |
| 139 | + ) |
| 140 | + |
| 141 | + assert get_in(rendered, ["data", "relationships", "route", "data", "id"]) == "route1" |
| 142 | + end |
| 143 | + |
| 144 | + test "includes vehicle relationship when requested", %{conn: conn} do |
| 145 | + vehicle = %Model.Vehicle{ |
| 146 | + id: "v1", |
| 147 | + revenue: :REVENUE |
| 148 | + } |
| 149 | + |
| 150 | + State.Vehicle.new_state([vehicle]) |
| 151 | + |
| 152 | + rendered = |
| 153 | + render(ApiWeb.StopEventView, "index.json-api", |
| 154 | + data: @stop_event, |
| 155 | + conn: conn, |
| 156 | + opts: %{include: "vehicle"} |
| 157 | + ) |
| 158 | + |
| 159 | + assert get_in(rendered, ["data", "relationships", "vehicle", "data", "id"]) == "v1" |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + describe "location" do |
| 164 | + test "returns the correct stop event location", %{conn: conn} do |
| 165 | + rendered = render(ApiWeb.StopEventView, "index.json-api", data: @stop_event, conn: conn) |
| 166 | + |
| 167 | + assert rendered["data"]["links"]["self"] =~ "/stop_events/trip1-route1-v1-1" |
| 168 | + end |
| 169 | + end |
| 170 | +end |
0 commit comments