Skip to content

Commit

Permalink
fix: podcast fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Jan 23, 2024
1 parent 7da8243 commit a4186a3
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
37 changes: 37 additions & 0 deletions lib/radiator/podcast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ defmodule Radiator.Podcast do
Repo.all(Network)
end

@doc """
Returns the list of networks with preloaded associations.
## Examples
iex> list_networks(preload: :shows)
[%Network{}, ...]
"""

def list_networks(preload: preload) do
Network
|> Repo.all()
|> Repo.preload(preload)
end

@doc """
Gets a single network.
Expand Down Expand Up @@ -132,6 +148,27 @@ defmodule Radiator.Podcast do
"""
def get_show!(id), do: Repo.get!(Show, id)

@doc """
Gets a single show with preloaded associations.
Raises `Ecto.NoResultsError` if the Show does not exist.
## Examples
iex> get_show!(123, preload: :episodes)
%Show{}
iex> get_show!(456, preload: :episodes)
** (Ecto.NoResultsError)
"""

def get_show!(id, preload: preload) do
Show
|> Repo.get!(id)
|> Repo.preload(preload)
end

@doc """
Creates a show.
Expand Down
2 changes: 2 additions & 0 deletions lib/radiator/podcast/episode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ defmodule Radiator.Podcast.Episode do
schema "episodes" do
field :title, :string
field :number, :integer

belongs_to :show, Show

timestamps(type: :utc_datetime)
end

Expand Down
1 change: 1 addition & 0 deletions lib/radiator/podcast/network.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Radiator.Podcast.Network do
field :title, :string

has_many(:shows, Show)

timestamps(type: :utc_datetime)
end

Expand Down
3 changes: 3 additions & 0 deletions lib/radiator/podcast/show.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ defmodule Radiator.Podcast.Show do

schema "shows" do
field :title, :string

belongs_to :network, Network

has_many(:episodes, Episode)

timestamps(type: :utc_datetime)
end

Expand Down
17 changes: 16 additions & 1 deletion test/radiator/podcast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ defmodule Radiator.PodcastTest do
assert Podcast.list_networks() == [network]
end

test "list_networks/1 returns all networks with preloaded shows" do
show = show_fixture()

assert [%Network{shows: shows}] = Podcast.list_networks(preload: :shows)
assert shows == [show]
end

test "get_network!/1 returns the network with given id" do
network = network_fixture()
assert Podcast.get_network!(network.id) == network
Expand Down Expand Up @@ -57,7 +64,7 @@ defmodule Radiator.PodcastTest do
end

describe "shows" do
@invalid_attrs %{title: nil, hostname: nil}
@invalid_attrs %{title: nil}

test "list_shows/0 returns all shows" do
show = show_fixture()
Expand All @@ -69,6 +76,14 @@ defmodule Radiator.PodcastTest do
assert Podcast.get_show!(show.id) == show
end

test "get_show!/2 returns the show with preloaded episodes" do
show = show_fixture()
episode = episode_fixture(%{show_id: show.id})

assert %Show{episodes: episodes} = Podcast.get_show!(show.id, preload: :episodes)
assert episodes == [episode]
end

test "create_show/1 with valid data creates a show" do
network = network_fixture()
valid_attrs = %{title: "some title", network_id: network.id}
Expand Down
13 changes: 9 additions & 4 deletions test/support/fixtures/podcast_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ defmodule Radiator.PodcastFixtures do
Generate a show.
"""
def show_fixture(attrs \\ %{}) do
network = network_fixture()
network = get_network(attrs)

{:ok, show} =
attrs
|> Enum.into(%{
hostname: "some hostname",
title: "some title",
network: network
network_id: network.id
})
|> Podcast.create_show()

Expand All @@ -41,7 +40,7 @@ defmodule Radiator.PodcastFixtures do
Generate a episode.
"""
def episode_fixture(attrs \\ %{}) do
show = show_fixture()
show = get_show(attrs)

{:ok, episode} =
attrs
Expand All @@ -53,4 +52,10 @@ defmodule Radiator.PodcastFixtures do

episode
end

defp get_network(%{network_id: id}), do: Podcast.get_network!(id)
defp get_network(_), do: network_fixture()

defp get_show(%{show_id: id}), do: Podcast.get_show!(id)
defp get_show(_), do: show_fixture()
end

0 comments on commit a4186a3

Please sign in to comment.