Skip to content

Commit

Permalink
update fixture tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Dec 29, 2024
1 parent 9d91b7f commit 11fe1ab
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
29 changes: 27 additions & 2 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,33 @@ defmodule Radiator.OutlineTest do
alias Radiator.Outline.NodeRepository
alias Radiator.Podcast

describe "node_tree" do
test "generate from template" do
describe "generate nodes from template" do
test "single node" do
%{id: episode_id} = episode_fixture()

assert %Node{} = node = "node-1" |> node_tree_fixture(%{episode_id: episode_id})
assert [%Node{}] = list = ["node-1"] |> node_tree_fixture(%{episode_id: episode_id})

assert %{content: "node-1", parent_id: nil, prev_id: nil, episode_id: ^episode_id} = node
assert [%{content: "node-1", parent_id: nil, prev_id: nil, episode_id: ^episode_id}] = list
end

test "list of nodes" do
%{id: episode_id} = episode_fixture()

nodes = ["node-1", "node-2", "node-3"] |> node_tree_fixture(%{episode_id: episode_id})

assert length(nodes) == 3
assert Enum.all?(nodes, &match?(%Node{episode_id: ^episode_id}, &1))

assert [
%{uuid: uuid_1, content: "node-1", parent_id: nil, prev_id: nil},
%{uuid: uuid_2, content: "node-2", parent_id: nil, prev_id: uuid_1},
%{uuid: _uuid_3, content: "node-3", parent_id: nil, prev_id: uuid_2}
] = nodes
end

test "tree of nodes" do
%{id: episode_id, show_id: show_id} = episode_fixture()

nodes =
Expand Down
19 changes: 19 additions & 0 deletions test/support/data_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ defmodule Radiator.DataCase do
def simple_node_fixture_hierachical(_) do
episode = PodcastFixtures.episode_fixture()

attrs = %{
episode_id: episode.id,
show_id: episode.show_id,
outline_node_container_id: episode.outline_node_container_id
}

nodes =
[
"node-1",
{"node-2", ["node-2_1"]}
]
|> node_tree_fixture(attrs)

assert [
%{uuid: uuid_1, content: "node-1", parent_id: nil, prev_id: nil},
%{uuid: uuid_2, content: "node-2", parent_id: nil, prev_id: uuid_1},
%{uuid: _uuid_2_1, content: "node-2_1", parent_id: uuid_2, prev_id: nil}
] = nodes

node_1 =
node_fixture(
episode_id: episode.id,
Expand Down
26 changes: 13 additions & 13 deletions test/support/fixtures/outline_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ defmodule Radiator.OutlineFixtures do
"""
def node_tree_fixture(content, attrs \\ %{})

def node_tree_fixture(content, attrs) when is_bitstring(content) do
attrs
|> Map.merge(%{content: content})
|> node_fixture()
end

def node_tree_fixture({content, list}, attrs) do
node = node_tree_fixture(content, attrs)
child_attrs = Map.merge(attrs, %{parent_id: node.uuid, prev_id: nil})

[node, [node_tree_fixture(list, child_attrs)]]
end

def node_tree_fixture([content], attrs) do
[node_tree_fixture(content, attrs)]
end
Expand All @@ -74,6 +61,19 @@ defmodule Radiator.OutlineFixtures do
List.flatten([nodes | node_tree_fixture(tail, child_attrs)])
end

def node_tree_fixture({content, list}, attrs) do
node = node_tree_fixture(content, attrs)
child_attrs = Map.merge(attrs, %{parent_id: node.uuid, prev_id: nil})

[node, [node_tree_fixture(list, child_attrs)]]
end

def node_tree_fixture(content, attrs) when is_bitstring(content) do
attrs
|> Map.merge(%{content: content})
|> node_fixture()
end

@doc """
Generate a node_container.
"""
Expand Down

0 comments on commit 11fe1ab

Please sign in to comment.