Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: namer identifier lenght #6872

Merged
merged 1 commit into from Mar 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 87 additions & 0 deletions tests/table_test.go
Expand Up @@ -2,8 +2,10 @@ package tests_test

import (
"regexp"
"sync"
"testing"

"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
Expand Down Expand Up @@ -172,3 +174,88 @@ func TestTableWithNamer(t *testing.T) {
t.Errorf("Table with namer, got %v", sql)
}
}

func TestPostgresTableWithIdentifierLength(t *testing.T) {
if DB.Dialector.Name() != "postgres" {
return
}

type LongString struct {
ThisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString string `gorm:"unique"`
}

t.Run("default", func(t *testing.T) {
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{})
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
if err != nil {
t.Fatalf("failed to parse user unique, got error %v", err)
}

constraints := user.ParseUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("failed to find unique constraint, got %v", constraints)
}

for key := range constraints {
if len(key) != 63 {
t.Errorf("failed to find unique constraint, got %v", constraints)
}
}
})

t.Run("naming strategy", func(t *testing.T) {
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{
NamingStrategy: schema.NamingStrategy{},
})

user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
if err != nil {
t.Fatalf("failed to parse user unique, got error %v", err)
}

constraints := user.ParseUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("failed to find unique constraint, got %v", constraints)
}

for key := range constraints {
if len(key) != 63 {
t.Errorf("failed to find unique constraint, got %v", constraints)
}
}
})

t.Run("namer", func(t *testing.T) {
uname := "custom_unique_name"
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{
NamingStrategy: mockUniqueNamingStrategy{
UName: uname,
},
})

user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy)
if err != nil {
t.Fatalf("failed to parse user unique, got error %v", err)
}

constraints := user.ParseUniqueConstraints()
if len(constraints) != 1 {
t.Fatalf("failed to find unique constraint, got %v", constraints)
}

for key := range constraints {
if key != uname {
t.Errorf("failed to find unique constraint, got %v", constraints)
}
}
})
}

type mockUniqueNamingStrategy struct {
UName string
schema.NamingStrategy
}

func (a mockUniqueNamingStrategy) UniqueName(table, column string) string {
return a.UName
}