feat: type not editable

This commit is contained in:
carla 2026-02-18 16:42:54 +01:00
parent adea380d86
commit e47e266570
6 changed files with 180 additions and 14 deletions

View file

@ -8,6 +8,7 @@ defmodule Mv.Membership.CustomFieldValidationTest do
- Description length validation (max 500 characters)
- Description trimming
- Required vs optional fields
- Value type immutability (cannot be changed after creation)
"""
use Mv.DataCase, async: true
@ -207,4 +208,101 @@ defmodule Mv.Membership.CustomFieldValidationTest do
assert [%{field: :value_type}] = changeset.errors
end
end
describe "value_type immutability" do
test "rejects attempt to change value_type after creation", %{actor: actor} do
# Create custom field with value_type :string
{:ok, custom_field} =
CustomField
|> Ash.Changeset.for_create(:create, %{
name: "test_field",
value_type: :string
})
|> Ash.create(actor: actor)
original_value_type = custom_field.value_type
assert original_value_type == :string
# Attempt to update value_type to :integer
assert {:error, %Ash.Error.Invalid{} = error} =
custom_field
|> Ash.Changeset.for_update(:update, %{
value_type: :integer
})
|> Ash.update(actor: actor)
# Verify error message contains expected text
error_message = Exception.message(error)
assert error_message =~ "cannot be changed" or error_message =~ "value_type"
# Reload and verify value_type remained unchanged
reloaded = Ash.get!(CustomField, custom_field.id, actor: actor)
assert reloaded.value_type == original_value_type
assert reloaded.value_type == :string
end
test "allows updating other fields while value_type remains unchanged", %{actor: actor} do
# Create custom field with value_type :string
{:ok, custom_field} =
CustomField
|> Ash.Changeset.for_create(:create, %{
name: "test_field",
value_type: :string,
description: "Original description"
})
|> Ash.create(actor: actor)
original_value_type = custom_field.value_type
assert original_value_type == :string
# Update other fields (name, description) without touching value_type
{:ok, updated_custom_field} =
custom_field
|> Ash.Changeset.for_update(:update, %{
name: "updated_name",
description: "Updated description"
})
|> Ash.update(actor: actor)
# Verify value_type remained unchanged
assert updated_custom_field.value_type == original_value_type
assert updated_custom_field.value_type == :string
# Verify other fields were updated
assert updated_custom_field.name == "updated_name"
assert updated_custom_field.description == "Updated description"
end
test "rejects value_type change even when other fields are updated", %{actor: actor} do
# Create custom field with value_type :boolean
{:ok, custom_field} =
CustomField
|> Ash.Changeset.for_create(:create, %{
name: "test_field",
value_type: :boolean
})
|> Ash.create(actor: actor)
original_value_type = custom_field.value_type
assert original_value_type == :boolean
# Attempt to update both name and value_type
assert {:error, %Ash.Error.Invalid{} = error} =
custom_field
|> Ash.Changeset.for_update(:update, %{
name: "updated_name",
value_type: :date
})
|> Ash.update(actor: actor)
# Verify error message
error_message = Exception.message(error)
assert error_message =~ "cannot be changed" or error_message =~ "value_type"
# Reload and verify value_type remained unchanged, but name was not updated either
reloaded = Ash.get!(CustomField, custom_field.id, actor: actor)
assert reloaded.value_type == original_value_type
assert reloaded.value_type == :boolean
assert reloaded.name == "test_field"
end
end
end