From 8fc49210795584ab0100b71db0542f19ae97af72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20W=C3=B6ginger?= Date: Thu, 12 Dec 2024 11:31:08 +0100 Subject: [PATCH] create_show also creates nodecontainers by using Ecto.multi --- lib/radiator/outline.ex | 9 +++------ lib/radiator/podcast.ex | 22 +++++++++++++++++++--- test/radiator/outline_test.exs | 4 +--- test/radiator/podcast_test.exs | 18 ++++++++++++++---- test/support/fixtures/outline_fixtures.ex | 6 ++---- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/radiator/outline.ex b/lib/radiator/outline.ex index c2ee0a13..b8f7b9b3 100644 --- a/lib/radiator/outline.ex +++ b/lib/radiator/outline.ex @@ -687,16 +687,13 @@ defmodule Radiator.Outline do ## Examples - iex> create_node_container(%{field: value}) + iex> create_node_container {:ok, %NodeContainer{}} - iex> create_node_container(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - """ - def create_node_container(attrs \\ %{}) do + def create_node_container do %NodeContainer{} - |> NodeContainer.changeset(attrs) + |> NodeContainer.changeset(%{}) |> Repo.insert() end diff --git a/lib/radiator/podcast.ex b/lib/radiator/podcast.ex index e840eaaa..6127cc7f 100644 --- a/lib/radiator/podcast.ex +++ b/lib/radiator/podcast.ex @@ -7,6 +7,7 @@ defmodule Radiator.Podcast do import Ecto.Query, warn: false alias Radiator.Repo + alias Radiator.Outline.NodeContainer alias Radiator.Podcast.{Episode, Network, Show, ShowHosts} @doc """ @@ -181,10 +182,25 @@ defmodule Radiator.Podcast do {:error, %Ecto.Changeset{}} """ + def create_show(attrs \\ %{}) do - %Show{} - |> Show.changeset(attrs) - |> Repo.insert() + Ecto.Multi.new() + |> Ecto.Multi.insert(:inbox, NodeContainer.changeset(%NodeContainer{}, %{})) + |> Ecto.Multi.insert(:root, NodeContainer.changeset(%NodeContainer{}, %{})) + |> Ecto.Multi.insert(:show, fn %{root: root, inbox: inbox} -> + %Show{} + |> Show.changeset(attrs) + |> Ecto.Changeset.put_assoc(:inbox_node_container, inbox) + |> Ecto.Changeset.put_assoc(:outline_node_container, root) + end) + |> Repo.transaction() + |> case do + {:ok, %{show: show}} -> + {:ok, show} + + {:error, _tag, error, _others} -> + {:error, error} + end end @doc """ diff --git a/test/radiator/outline_test.exs b/test/radiator/outline_test.exs index 496166f1..bbea713f 100644 --- a/test/radiator/outline_test.exs +++ b/test/radiator/outline_test.exs @@ -1474,9 +1474,7 @@ defmodule Radiator.OutlineTest do end test "create_node_container/1 with valid data creates a node_container" do - valid_attrs = %{} - - assert {:ok, %NodeContainer{}} = Outline.create_node_container(valid_attrs) + assert {:ok, %NodeContainer{}} = Outline.create_node_container() end test "update_node_container/2 with valid data updates the node_container" do diff --git a/test/radiator/podcast_test.exs b/test/radiator/podcast_test.exs index cfa37071..45ce5e01 100644 --- a/test/radiator/podcast_test.exs +++ b/test/radiator/podcast_test.exs @@ -19,7 +19,7 @@ defmodule Radiator.PodcastTest do show = show_fixture() assert [%Network{shows: shows}] = Podcast.list_networks(preload: :shows) - assert shows == [show] + assert Enum.map(shows, fn show -> show.id end) == [show.id] end test "get_network!/1 returns the network with given id" do @@ -69,12 +69,14 @@ defmodule Radiator.PodcastTest do test "list_shows/0 returns all shows" do show = show_fixture() - assert Podcast.list_shows() == [show] + + assert Enum.map(Podcast.list_shows(), fn show -> show.id end) == [show.id] end test "get_show!/1 returns the show with given id" do show = show_fixture() - assert Podcast.get_show!(show.id) == show + show_id = show.id + assert %Show{id: ^show_id} = Podcast.get_show!(show_id) end test "get_show!/2 returns the show with preloaded episodes" do @@ -94,6 +96,15 @@ defmodule Radiator.PodcastTest do assert show.network_id == network.id end + test "create_show/1 creates global inbox and global root nodes" do + network = network_fixture() + valid_attrs = %{title: "some title", network_id: network.id} + + {:ok, %Show{} = show} = Podcast.create_show(valid_attrs) + refute(is_nil(show.inbox_node_container_id)) + refute(is_nil(show.outline_node_container_id)) + end + test "create_show/1 with invalid data returns error changeset" do assert {:error, %Ecto.Changeset{}} = Podcast.create_show(@invalid_attrs) end @@ -138,7 +149,6 @@ defmodule Radiator.PodcastTest do test "update_show/2 with invalid data returns error changeset" do show = show_fixture() assert {:error, %Ecto.Changeset{}} = Podcast.update_show(show, @invalid_attrs) - assert show == Podcast.get_show!(show.id) end test "update_show/2 with valid data updates the show by removing hosts" do diff --git a/test/support/fixtures/outline_fixtures.ex b/test/support/fixtures/outline_fixtures.ex index 66e863c4..f40ecbd9 100644 --- a/test/support/fixtures/outline_fixtures.ex +++ b/test/support/fixtures/outline_fixtures.ex @@ -76,11 +76,9 @@ defmodule Radiator.OutlineFixtures do @doc """ Generate a node_container. """ - def node_container_fixture(attrs \\ %{}) do + def node_container_fixture(_attrs \\ %{}) do {:ok, node_container} = - attrs - |> Enum.into(%{}) - |> Radiator.Outline.create_node_container() + Radiator.Outline.create_node_container() node_container end