Skip to content

Commit 9efae65

Browse files
authored
test: namer identifier lenght (#6872)
1 parent f17a752 commit 9efae65

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

tests/table_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package tests_test
22

33
import (
44
"regexp"
5+
"sync"
56
"testing"
67

8+
"gorm.io/driver/postgres"
79
"gorm.io/gorm"
810
"gorm.io/gorm/schema"
911
"gorm.io/gorm/utils/tests"
@@ -172,3 +174,88 @@ func TestTableWithNamer(t *testing.T) {
172174
t.Errorf("Table with namer, got %v", sql)
173175
}
174176
}
177+
178+
func TestPostgresTableWithIdentifierLength(t *testing.T) {
179+
if DB.Dialector.Name() != "postgres" {
180+
return
181+
}
182+
183+
type LongString struct {
184+
ThisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString string `gorm:"unique"`
185+
}
186+
187+
t.Run("default", func(t *testing.T) {
188+
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{})
189+
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
190+
if err != nil {
191+
t.Fatalf("failed to parse user unique, got error %v", err)
192+
}
193+
194+
constraints := user.ParseUniqueConstraints()
195+
if len(constraints) != 1 {
196+
t.Fatalf("failed to find unique constraint, got %v", constraints)
197+
}
198+
199+
for key := range constraints {
200+
if len(key) != 63 {
201+
t.Errorf("failed to find unique constraint, got %v", constraints)
202+
}
203+
}
204+
})
205+
206+
t.Run("naming strategy", func(t *testing.T) {
207+
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{
208+
NamingStrategy: schema.NamingStrategy{},
209+
})
210+
211+
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
212+
if err != nil {
213+
t.Fatalf("failed to parse user unique, got error %v", err)
214+
}
215+
216+
constraints := user.ParseUniqueConstraints()
217+
if len(constraints) != 1 {
218+
t.Fatalf("failed to find unique constraint, got %v", constraints)
219+
}
220+
221+
for key := range constraints {
222+
if len(key) != 63 {
223+
t.Errorf("failed to find unique constraint, got %v", constraints)
224+
}
225+
}
226+
})
227+
228+
t.Run("namer", func(t *testing.T) {
229+
uname := "custom_unique_name"
230+
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{
231+
NamingStrategy: mockUniqueNamingStrategy{
232+
UName: uname,
233+
},
234+
})
235+
236+
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
237+
if err != nil {
238+
t.Fatalf("failed to parse user unique, got error %v", err)
239+
}
240+
241+
constraints := user.ParseUniqueConstraints()
242+
if len(constraints) != 1 {
243+
t.Fatalf("failed to find unique constraint, got %v", constraints)
244+
}
245+
246+
for key := range constraints {
247+
if key != uname {
248+
t.Errorf("failed to find unique constraint, got %v", constraints)
249+
}
250+
}
251+
})
252+
}
253+
254+
type mockUniqueNamingStrategy struct {
255+
UName string
256+
schema.NamingStrategy
257+
}
258+
259+
func (a mockUniqueNamingStrategy) UniqueName(table, column string) string {
260+
return a.UName
261+
}

0 commit comments

Comments
 (0)