diff --git a/test/towerops/sites_test.exs b/test/towerops/sites_test.exs index 0a70610a..c6cd5537 100644 --- a/test/towerops/sites_test.exs +++ b/test/towerops/sites_test.exs @@ -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))