added test for partially matched table name/alias as suffix

This commit is contained in:
Gani Georgiev 2025-01-12 13:23:07 +02:00
parent f4108cb354
commit e4d637e6e0
2 changed files with 10 additions and 2 deletions

View File

@ -64,8 +64,9 @@ func NormalizeUniqueIndexError(err error, tableOrAlias string, fieldNames []stri
normalizedErrs := validation.Errors{} normalizedErrs := validation.Errors{}
for _, name := range fieldNames { for _, name := range fieldNames {
// note: extra space to exclude other fields starting with the current field name // note: extra spaces to exclude table name with suffix matching the current one
if strings.Contains(msg, strings.ToLower(tableOrAlias+"."+name+" ")) { // OR other fields starting with the current field name
if strings.Contains(msg, strings.ToLower(" "+tableOrAlias+"."+name+" ")) {
normalizedErrs[name] = validation.NewError("validation_not_unique", "Value must be unique") normalizedErrs[name] = validation.NewError("validation_not_unique", "Value must be unique")
} }
} }

View File

@ -78,6 +78,13 @@ func TestNormalizeUniqueIndexError(t *testing.T) {
[]string{"a", "b"}, []string{"a", "b"},
nil, nil,
}, },
{
"unique index error with table name suffix matching the specified one",
errors.New("UNIQUE constraint failed for fields test_suffix.a,test_suffix.b"),
"suffix",
[]string{"a", "b", "c"},
nil,
},
{ {
"unique index error but mismatched fields", "unique index error but mismatched fields",
errors.New("UNIQUE constraint failed for fields test.a,test.b"), errors.New("UNIQUE constraint failed for fields test.a,test.b"),