@@ -3082,3 +3082,133 @@ func BenchmarkIndexUpdateText(b *testing.B) {
30823082 }
30833083 }
30843084}
3085+
3086+ func TestIndexUpdateNestedMapping (t * testing.T ) {
3087+ // Helper: create a mapping with optional nested structure
3088+ createCompanyMapping := func (nestedEmployees , nestedDepartments , nestedProjects , nestedLocations bool ) * mapping.IndexMappingImpl {
3089+ rv := mapping .NewIndexMapping ()
3090+ companyMapping := mapping .NewDocumentMapping ()
3091+
3092+ // Basic fields
3093+ companyMapping .AddFieldMappingsAt ("id" , mapping .NewTextFieldMapping ())
3094+ companyMapping .AddFieldMappingsAt ("name" , mapping .NewTextFieldMapping ())
3095+
3096+ var deptMapping * mapping.DocumentMapping
3097+ // Departments nested conditionally
3098+ if ! nestedDepartments {
3099+ deptMapping = mapping .NewDocumentMapping ()
3100+ } else {
3101+ deptMapping = mapping .NewNestedDocumentMapping ()
3102+ }
3103+ deptMapping .AddFieldMappingsAt ("name" , mapping .NewTextFieldMapping ())
3104+ deptMapping .AddFieldMappingsAt ("budget" , mapping .NewNumericFieldMapping ())
3105+
3106+ // Employees nested conditionally
3107+ var empMapping * mapping.DocumentMapping
3108+ if ! nestedEmployees {
3109+ empMapping = mapping .NewNestedDocumentMapping ()
3110+ } else {
3111+ empMapping = mapping .NewDocumentMapping ()
3112+ }
3113+ empMapping .AddFieldMappingsAt ("name" , mapping .NewTextFieldMapping ())
3114+ empMapping .AddFieldMappingsAt ("role" , mapping .NewTextFieldMapping ())
3115+ deptMapping .AddSubDocumentMapping ("employees" , empMapping )
3116+
3117+ // Projects nested conditionally
3118+ var projMapping * mapping.DocumentMapping
3119+ if ! nestedProjects {
3120+ projMapping = mapping .NewNestedDocumentMapping ()
3121+ } else {
3122+ projMapping = mapping .NewDocumentMapping ()
3123+ }
3124+ projMapping .AddFieldMappingsAt ("title" , mapping .NewTextFieldMapping ())
3125+ projMapping .AddFieldMappingsAt ("status" , mapping .NewTextFieldMapping ())
3126+ deptMapping .AddSubDocumentMapping ("projects" , projMapping )
3127+
3128+ companyMapping .AddSubDocumentMapping ("departments" , deptMapping )
3129+
3130+ // Locations nested conditionally
3131+ var locMapping * mapping.DocumentMapping
3132+ if nestedLocations {
3133+ locMapping = mapping .NewNestedDocumentMapping ()
3134+ } else {
3135+ locMapping = mapping .NewDocumentMapping ()
3136+ }
3137+ locMapping .AddFieldMappingsAt ("address" , mapping .NewTextFieldMapping ())
3138+ locMapping .AddFieldMappingsAt ("city" , mapping .NewTextFieldMapping ())
3139+
3140+ companyMapping .AddSubDocumentMapping ("locations" , locMapping )
3141+
3142+ rv .DefaultMapping .AddSubDocumentMapping ("company" , companyMapping )
3143+ return rv
3144+ }
3145+
3146+ tests := []struct {
3147+ name string
3148+ original * mapping.IndexMappingImpl
3149+ updated * mapping.IndexMappingImpl
3150+ expectErr bool
3151+ }{
3152+ {
3153+ name : "No nested to all nested" ,
3154+ original : createCompanyMapping (false , false , false , false ),
3155+ updated : createCompanyMapping (true , true , true , true ),
3156+ expectErr : true ,
3157+ },
3158+ {
3159+ name : "No nested to mixed nested" ,
3160+ original : createCompanyMapping (false , false , false , false ),
3161+ updated : createCompanyMapping (true , false , true , false ),
3162+ expectErr : true ,
3163+ },
3164+ {
3165+ name : "No nested to mixed nested" ,
3166+ original : createCompanyMapping (false , false , false , false ),
3167+ updated : createCompanyMapping (true , true , true , false ),
3168+ expectErr : true ,
3169+ },
3170+ {
3171+ name : "Mixed nested to no nested" ,
3172+ original : createCompanyMapping (false , true , false , true ),
3173+ updated : createCompanyMapping (false , false , true , true ),
3174+ expectErr : true ,
3175+ },
3176+ {
3177+ name : "All nested to no nested" ,
3178+ original : createCompanyMapping (true , true , true , true ),
3179+ updated : createCompanyMapping (false , false , false , false ),
3180+ expectErr : true ,
3181+ },
3182+ {
3183+ name : "Mixed nested to all nested" ,
3184+ original : createCompanyMapping (true , false , true , false ),
3185+ updated : createCompanyMapping (true , true , true , true ),
3186+ expectErr : true ,
3187+ },
3188+ {
3189+ name : "All nested to mixed nested" ,
3190+ original : createCompanyMapping (true , true , true , true ),
3191+ updated : createCompanyMapping (true , false , true , false ),
3192+ expectErr : true ,
3193+ },
3194+ {
3195+ name : "No nested to no nested" ,
3196+ original : createCompanyMapping (false , false , false , false ),
3197+ updated : createCompanyMapping (false , false , false , false ),
3198+ expectErr : false ,
3199+ },
3200+ {
3201+ name : "All nested to all nested" ,
3202+ original : createCompanyMapping (true , true , true , true ),
3203+ updated : createCompanyMapping (true , true , true , true ),
3204+ expectErr : false ,
3205+ },
3206+ }
3207+
3208+ for _ , test := range tests {
3209+ _ , err := DeletedFields (test .original , test .updated )
3210+ if (err != nil ) != test .expectErr {
3211+ t .Errorf ("Test '%s' unexpected error state: got %v, expectErr %t" , test .name , err , test .expectErr )
3212+ }
3213+ }
3214+ }
0 commit comments