Skip to content

Commit

Permalink
refactor, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Aug 25, 2024
1 parent c227f77 commit 763f0c6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
37 changes: 20 additions & 17 deletions lib/radiator/outline/validations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,34 @@ defmodule Radiator.Outline.Validations do
# every level has 1 node with prev_id nil
# all other nodes in level have prev_id set and are connected to the previous node
if Enum.count(tree_nodes) == NodeRepository.count_nodes_by_episode(episode_id) do
# iterate through the levels of the tree
tree_nodes
|> Enum.group_by(& &1.level)
|> Enum.map(fn {_level, nodes} ->
# get the node with prev_id nil
first_node = Enum.find(nodes, &(&1.prev_id == nil))
# get the rest of the nodes
rest_nodes = Enum.reject(nodes, &(&1.prev_id == nil))
# iterate through the rest of the nodes
Enum.reduce(rest_nodes, first_node, fn node, prev_node ->
is_node_prev_node_of(prev_node, node)
end)
end)

{:ok}
validate_tree_nodes(tree_nodes)
else
{:error, :node_count_not_consistent}
end
end

def is_node_prev_node_of(
# iterate through the levels of the tree
defp validate_tree_nodes(tree_nodes) do
tree_nodes
|> Enum.group_by(& &1.level)
|> Enum.map(fn {_level, nodes} ->

Check warning on line 58 in lib/radiator/outline/validations.ex

View workflow job for this annotation

GitHub Actions / Build & Test

There should be no unused return values for Enum functions.
# get the node with prev_id nil
first_node = Enum.find(nodes, &(&1.prev_id == nil))
# get the rest of the nodes
rest_nodes = Enum.reject(nodes, &(&1.prev_id == nil))
# iterate through the rest of the nodes
Enum.reduce(rest_nodes, first_node, fn node, prev_node ->
check_node_prev_node_of(prev_node, node)
end)
end)
:ok
end

defp check_node_prev_node_of(
%Node{uuid: id},
%Node{prev_id: id} = node
),
do: node

def is_node_prev_node_of(_, _), do: {:error, :prev_id_not_consistent}
defp check_node_prev_node_of(_, _), do: {:error, :prev_id_not_consistent}
end
36 changes: 21 additions & 15 deletions test/radiator/outline/validations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,38 @@ defmodule Radiator.Outline.ValidationsTest do
test "validates a tree", %{
node_1: %Node{episode_id: episode_id}
} do
assert {:ok} = Validations.validate_tree_for_episode(episode_id)
assert :ok = Validations.validate_tree_for_episode(episode_id)
end

test "when a parent has two childs with prev_id the tree is invalid", %{
node_1: %Node{episode_id: episode_id} = node_1
} do
{:ok, %Node{} = _node_invalid} = %{
episode_id: episode_id,
parent_id: node_1.parent_id,
prev_id: nil
} |> NodeRepository.create_node()

assert {:error, :node_count_not_consistent} = Validations.validate_tree_for_episode(episode_id)
{:ok, %Node{} = _node_invalid} =
%{
episode_id: episode_id,
parent_id: node_1.parent_id,
prev_id: nil
}
|> NodeRepository.create_node()

assert {:error, :node_count_not_consistent} =
Validations.validate_tree_for_episode(episode_id)
end

test "a tree where a parent and prev are not consistent is invalid", %{
parent_node: %Node{episode_id: episode_id} = parent_node,
nested_node_2: nested_node_2
} do
{:ok, %Node{} = _node_invalid} = %{
episode_id: episode_id,
parent_id: parent_node.uuid,
prev_id: nested_node_2.uuid
} |> NodeRepository.create_node()

assert {:error, :node_count_not_consistent} = Validations.validate_tree_for_episode(episode_id)
{:ok, %Node{} = _node_invalid} =
%{
episode_id: episode_id,
parent_id: parent_node.uuid,
prev_id: nested_node_2.uuid
}
|> NodeRepository.create_node()

assert {:error, :node_count_not_consistent} =
Validations.validate_tree_for_episode(episode_id)
end
end
end

0 comments on commit 763f0c6

Please sign in to comment.