From 167f853a6344eb56a53e33329c4caeab25ea1f83 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Tue, 13 Jan 2026 09:05:02 -0600 Subject: [PATCH] Add comprehensive tests for Sites.Site schema - Test valid changesets with required and all fields - Test validation for name, description, location lengths - Test parent site relationships and circular reference validation - Test all constraint validations --- test/towerops/sites/site_test.exs | 154 ++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 test/towerops/sites/site_test.exs diff --git a/test/towerops/sites/site_test.exs b/test/towerops/sites/site_test.exs new file mode 100644 index 00000000..e1f1b454 --- /dev/null +++ b/test/towerops/sites/site_test.exs @@ -0,0 +1,154 @@ +defmodule Towerops.Sites.SiteTest do + use Towerops.DataCase + + import Towerops.AccountsFixtures + import Towerops.OrganizationsFixtures + + alias Towerops.Sites.Site + + describe "changeset/2" do + setup do + user = user_fixture() + organization = organization_fixture(user.id) + %{organization: organization} + end + + test "valid changeset with required fields", %{organization: organization} do + changeset = + Site.changeset(%Site{}, %{ + name: "Test Site", + organization_id: organization.id + }) + + assert changeset.valid? + end + + test "valid changeset with all fields", %{organization: organization} do + changeset = + Site.changeset(%Site{}, %{ + name: "Test Site", + description: "A test site", + location: "123 Main St", + organization_id: organization.id + }) + + assert changeset.valid? + end + + test "invalid changeset without name", %{organization: organization} do + changeset = + Site.changeset(%Site{}, %{ + organization_id: organization.id + }) + + refute changeset.valid? + assert "can't be blank" in errors_on(changeset).name + end + + test "invalid changeset without organization_id" do + changeset = + Site.changeset(%Site{}, %{ + name: "Test Site" + }) + + refute changeset.valid? + assert "can't be blank" in errors_on(changeset).organization_id + end + + test "invalid changeset with short name", %{organization: organization} do + changeset = + Site.changeset(%Site{}, %{ + name: "A", + organization_id: organization.id + }) + + refute changeset.valid? + assert "should be at least 2 character(s)" in errors_on(changeset).name + end + + test "invalid changeset with long name", %{organization: organization} do + long_name = String.duplicate("a", 201) + + changeset = + Site.changeset(%Site{}, %{ + name: long_name, + organization_id: organization.id + }) + + refute changeset.valid? + assert "should be at most 200 character(s)" in errors_on(changeset).name + end + + test "invalid changeset with long description", %{organization: organization} do + long_description = String.duplicate("a", 1001) + + changeset = + Site.changeset(%Site{}, %{ + name: "Test Site", + description: long_description, + organization_id: organization.id + }) + + refute changeset.valid? + assert "should be at most 1000 character(s)" in errors_on(changeset).description + end + + test "invalid changeset with long location", %{organization: organization} do + long_location = String.duplicate("a", 201) + + changeset = + Site.changeset(%Site{}, %{ + name: "Test Site", + location: long_location, + organization_id: organization.id + }) + + refute changeset.valid? + assert "should be at most 200 character(s)" in errors_on(changeset).location + end + + test "valid changeset with parent site", %{organization: organization} do + {:ok, parent_site} = + Towerops.Sites.create_site(%{ + name: "Parent Site", + organization_id: organization.id + }) + + changeset = + Site.changeset(%Site{}, %{ + name: "Child Site", + organization_id: organization.id, + parent_site_id: parent_site.id + }) + + assert changeset.valid? + end + + test "invalid changeset with circular parent reference", %{organization: organization} do + {:ok, site} = + Towerops.Sites.create_site(%{ + name: "Test Site", + organization_id: organization.id + }) + + changeset = + Site.changeset(site, %{ + parent_site_id: site.id + }) + + refute changeset.valid? + assert "cannot be the same as the site itself" in errors_on(changeset).parent_site_id + end + + test "changeset allows nil parent_site_id", %{organization: organization} do + changeset = + Site.changeset(%Site{}, %{ + name: "Test Site", + organization_id: organization.id, + parent_site_id: nil + }) + + assert changeset.valid? + end + end +end