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 Jul 30, 2024
1 parent 3f01de3 commit ac11351
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,80 @@ 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 "associated the correct level", %{
node_1: node_1,
node_2: node_2,
node_3: node_3,
node_4: node_4,
node_5: node_5,
node_6: node_6,
nested_node_1: nested_node_1,
nested_node_2: nested_node_2,
parent_node: parent_node
} do
tree = Outline.list_nodes_by_episode_sorted(parent_node.episode_id)
assert tree == [parent_node, node_1, node_2, node_3, node_4, node_5, node_6, nested_node_1, nested_node_2]
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.filter(fn n -> n.parent_id == nil end)
|> Enum.count

Check warning on line 767 in test/radiator/outline_test.exs

View workflow job for this annotation

GitHub Actions / Build & Test

`Enum.count/2` is more efficient than `Enum.filter/2 |> Enum.count/1`.

assert num_nodes_without_parents == 3
end
end

describe "order_child_nodes/1" do
setup :complex_node_fixture

Expand Down

0 comments on commit ac11351

Please sign in to comment.