From b2f002016fb7438a4507f9cb1cc236a43fb71e27 Mon Sep 17 00:00:00 2001 From: "Julien Tant (aider)" Date: Wed, 12 Feb 2025 15:10:00 -0700 Subject: [PATCH] test: Add test case for preserving option IDs when patching select field --- .../app/custom_profile_attributes_test.go | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/server/channels/app/custom_profile_attributes_test.go b/server/channels/app/custom_profile_attributes_test.go index 811dd455c01..3730356abe6 100644 --- a/server/channels/app/custom_profile_attributes_test.go +++ b/server/channels/app/custom_profile_attributes_test.go @@ -272,6 +272,68 @@ func TestPatchCPAField(t *testing.T) { require.Empty(t, updatedField.TargetType, "CPA should not allow to patch the field's target type") require.Greater(t, updatedField.UpdateAt, createdField.UpdateAt) }) + + t.Run("should preserve option IDs when patching select field options", func(t *testing.T) { + // Create a select field with options + selectField := &model.PropertyField{ + GroupID: cpaGroupID, + Name: "Select Field", + Type: model.PropertyFieldTypeSelect, + Attrs: map[string]any{ + "options": []any{ + map[string]any{ + "Name": "Option 1", + "Color": "#111111", + }, + map[string]any{ + "Name": "Option 2", + "Color": "#222222", + }, + }, + }, + } + createdSelectField, err := th.App.CreateCPAField(selectField) + require.Nil(t, err) + + // Get the original option IDs + options := createdSelectField.Attrs["options"].(model.CustomProfileAttributesSelectOptions) + require.Len(t, options, 2) + originalID1 := options[0].ID + originalID2 := options[1].ID + require.NotEmpty(t, originalID1) + require.NotEmpty(t, originalID2) + + // Patch the field with updated option names and colors + selectPatch := &model.PropertyFieldPatch{ + Attrs: model.NewPointer(map[string]any{ + "options": []any{ + map[string]any{ + "ID": originalID1, + "Name": "Updated Option 1", + "Color": "#333333", + }, + map[string]any{ + "ID": originalID2, + "Name": "Updated Option 2", + "Color": "#444444", + }, + }, + }), + } + + updatedSelectField, err := th.App.PatchCPAField(createdSelectField.ID, selectPatch) + require.Nil(t, err) + + // Verify the options were updated while preserving IDs + updatedOptions := updatedSelectField.Attrs["options"].(model.CustomProfileAttributesSelectOptions) + require.Len(t, updatedOptions, 2) + require.Equal(t, originalID1, updatedOptions[0].ID) + require.Equal(t, "Updated Option 1", updatedOptions[0].Name) + require.Equal(t, "#333333", updatedOptions[0].Color) + require.Equal(t, originalID2, updatedOptions[1].ID) + require.Equal(t, "Updated Option 2", updatedOptions[1].Name) + require.Equal(t, "#444444", updatedOptions[1].Color) + }) } func TestDeleteCPAField(t *testing.T) {