Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/vehicles/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ defmodule Vehicles.Repo do

@behaviour Behaviour

@impl Behaviour
def get(vehicle_id) do
__MODULE__
|> :ets.select([{{vehicle_id, :_, :_, :_, :"$1"}, [], [:"$1"]}])
|> case do
[%Vehicle{} = vehicle] ->
vehicle

_ ->
nil
end
end

@impl Behaviour
def route(route_id, opts \\ []) do
direction_id =
Expand Down
2 changes: 2 additions & 0 deletions lib/vehicles/repo/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Vehicles.Repo.Behaviour do

alias Vehicles.Vehicle

@callback get(String.t()) :: Vehicle.t() | nil

@callback route(String.t(), Keyword.t()) :: [Vehicle.t()]

@callback trip(String.t()) :: Vehicle.t() | nil
Expand Down
11 changes: 11 additions & 0 deletions test/vehicles/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ defmodule Vehicles.RepoTest do

def pubsub_fn(_, "vehicles"), do: :ok

describe "get/1" do
test "given a vehicle ID, return the matching vehicle" do
id = @vehicles |> Enum.map(& &1.id) |> Faker.Util.pick()
assert %Vehicle{} = Repo.get(id)
end

test "return nil otherwise" do
refute Repo.get("not-in-@vehicles")
end
end

describe "route/1" do
test "given a route ID, finds vehicle statuses for that route", %{name: name} do
assert Enum.sort(Enum.filter(@vehicles, &(&1.route_id == "86"))) ==
Expand Down