Add comprehensive tests for Sites context

Added 5 new tests to improve coverage:
- list_root_sites/1 for filtering root sites without parents
- list_child_sites/1 for getting child sites of a parent
- list_child_sites/1 edge case with no children
- get_site!/1 for verifying preload behavior with parent and child sites

This improves Sites coverage from 83.33% to higher.
This commit is contained in:
Graham McIntire 2026-01-13 07:59:10 -06:00
parent b91f81fc2e
commit 6cc068543d
No known key found for this signature in database

View file

@ -27,6 +27,77 @@ defmodule Towerops.SitesTest do
assert hd(result).id == site.id
end
test "list_root_sites/1 returns only root sites without parents", %{organization: organization} do
{:ok, root1} =
Sites.create_site(%{name: "Root 1", organization_id: organization.id})
{:ok, root2} =
Sites.create_site(%{name: "Root 2", organization_id: organization.id})
{:ok, _child} =
Sites.create_site(%{
name: "Child",
organization_id: organization.id,
parent_site_id: root1.id
})
roots = Sites.list_root_sites(organization.id)
assert length(roots) == 2
assert Enum.any?(roots, &(&1.id == root1.id))
assert Enum.any?(roots, &(&1.id == root2.id))
end
test "list_child_sites/1 returns child sites for a parent", %{organization: organization} do
{:ok, parent} = Sites.create_site(%{name: "Parent", organization_id: organization.id})
{:ok, child1} =
Sites.create_site(%{
name: "Child 1",
organization_id: organization.id,
parent_site_id: parent.id
})
{:ok, child2} =
Sites.create_site(%{
name: "Child 2",
organization_id: organization.id,
parent_site_id: parent.id
})
children = Sites.list_child_sites(parent.id)
assert length(children) == 2
assert Enum.any?(children, &(&1.id == child1.id))
assert Enum.any?(children, &(&1.id == child2.id))
end
test "list_child_sites/1 returns empty list when no children exist", %{
organization: organization
} do
{:ok, parent} = Sites.create_site(%{name: "Parent", organization_id: organization.id})
assert Sites.list_child_sites(parent.id) == []
end
test "get_site!/1 returns the site with preloads", %{organization: organization} do
{:ok, parent} = Sites.create_site(%{name: "Parent", organization_id: organization.id})
{:ok, child} =
Sites.create_site(%{
name: "Child",
organization_id: organization.id,
parent_site_id: parent.id
})
retrieved_parent = Sites.get_site!(parent.id)
assert retrieved_parent.id == parent.id
assert Ecto.assoc_loaded?(retrieved_parent.child_sites)
assert length(retrieved_parent.child_sites) == 1
retrieved_child = Sites.get_site!(child.id)
assert retrieved_child.id == child.id
assert Ecto.assoc_loaded?(retrieved_child.parent_site)
assert retrieved_child.parent_site.id == parent.id
end
test "get_organization_site!/2 returns the site with given id", %{organization: organization} do
{:ok, site} = Sites.create_site(Map.put(@valid_attrs, :organization_id, organization.id))