Skip to content

Commit

Permalink
tests for list_nodes_by_episode_sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Aug 1, 2024
1 parent 3f01de3 commit 93bfc45
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,32 @@ defmodule Radiator.OutlineTest do
end)
end

test "within level the nodes are sorted by previous id", %{node_4: node_4} do

Check failure on line 648 in test/radiator/outline_test.exs

View workflow job for this annotation

GitHub Actions / Build & Test

test get_node_tree/1 within level the nodes are sorted by previous id (Radiator.OutlineTest)
episode_id = node_4.episode_id

attrs = %{
"episode_id" => episode_id,
"parent_id" => node_4.parent_id,
"prev_id" => node_4.uuid,
"content" => "inserted node"
}

{:ok, %{node: _new_node}} = Outline.insert_node(attrs)
{:ok, tree} = Outline.get_node_tree(episode_id)

Enum.each([0, 1, 2], fn level ->
nodes_in_level = Enum.filter(tree, fn node -> node.level == level end)

Enum.reduce(nodes_in_level, nil, fn node, previous_node ->
if previous_node != nil do
assert previous_node.uuid == node.prev_id
end

node
end)
end)
end

test "associated the correct level", %{
node_1: node_1,
node_2: node_2,
Expand Down Expand Up @@ -696,6 +722,64 @@ defmodule Radiator.OutlineTest do
end
end

describe "list_nodes_by_episode_sorted/1" do
setup :complex_node_fixture

test "returns all nodes from a episode", %{parent_node: parent_node} do
episode_id = parent_node.episode_id
tree = Outline.list_nodes_by_episode_sorted(episode_id)

all_nodes = NodeRepository.list_nodes_by_episode(episode_id)

assert Enum.count(tree) == Enum.count(all_nodes)

Enum.each(tree, fn node ->
assert node.uuid ==
List.first(Enum.filter(all_nodes, fn n -> n.uuid == node.uuid end)).uuid
end)
end

test "does not return a node from another episode", %{
parent_node: parent_node
} do
episode_id = parent_node.episode_id
other_node = node_fixture(parent_id: nil, prev_id: nil, content: "other content")
assert other_node.episode_id != episode_id
tree = Outline.list_nodes_by_episode_sorted(episode_id)
assert Enum.filter(tree, fn n -> n.uuid == other_node.uuid end) == []
end

test "tree can have more than one parent node", %{
parent_node: parent_node
} do
episode_id = parent_node.episode_id

other_parent_node =
node_fixture(
parent_id: nil,
prev_id: parent_node.uuid,
episode_id: episode_id,
content: "also a parent"
)

_third_parent_node =
node_fixture(
parent_id: nil,
prev_id: other_parent_node.uuid,
episode_id: episode_id,
content: "even another root element"
)

tree = Outline.list_nodes_by_episode_sorted(parent_node.episode_id)

num_nodes_without_parents =
tree
|> Enum.count(fn n -> n.parent_id == nil end)

assert num_nodes_without_parents == 3
end
end

describe "order_child_nodes/1" do
setup :complex_node_fixture

Expand Down

0 comments on commit 93bfc45

Please sign in to comment.