| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"regexp" |
| 5 |
"sync" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"gorm.io/driver/gaussdb" |
| 9 |
"gorm.io/driver/postgres" |
| 10 |
"gorm.io/gorm" |
| 11 |
"gorm.io/gorm/schema" |
| 12 |
"gorm.io/gorm/utils/tests" |
| 13 |
. "gorm.io/gorm/utils/tests" |
| 14 |
) |
| 15 |
|
| 16 |
type UserWithTable struct { |
| 17 |
gorm.Model |
| 18 |
Name string |
| 19 |
} |
| 20 |
|
| 21 |
func (UserWithTable) TableName() string { |
| 22 |
return "gorm.user" |
| 23 |
} |
| 24 |
|
| 25 |
func TestTable(t *testing.T) { |
| 26 |
dryDB := DB.Session(&gorm.Session{DryRun: true}) |
| 27 |
|
| 28 |
r := dryDB.Table("`user`").Find(&User{}).Statement |
| 29 |
if !regexp.MustCompile("SELECT \\* FROM `user`").MatchString(r.Statement.SQL.String()) { |
| 30 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 31 |
} |
| 32 |
|
| 33 |
r = dryDB.Table("user as u").Select("name").Find(&User{}).Statement |
| 34 |
if !regexp.MustCompile("SELECT .name. FROM user as u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 35 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 36 |
} |
| 37 |
|
| 38 |
r = dryDB.Table("`people`").Table("`user`").Find(&User{}).Statement |
| 39 |
if !regexp.MustCompile("SELECT \\* FROM `user`").MatchString(r.Statement.SQL.String()) { |
| 40 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 41 |
} |
| 42 |
|
| 43 |
r = dryDB.Table("people as p").Table("user as u").Find(&User{}).Statement |
| 44 |
if !regexp.MustCompile("SELECT \\* FROM user as u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 45 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 46 |
} |
| 47 |
|
| 48 |
r = dryDB.Table("people as p").Table("user").Find(&User{}).Statement |
| 49 |
if !regexp.MustCompile("SELECT \\* FROM .user. WHERE .user.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 50 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 51 |
} |
| 52 |
|
| 53 |
r = dryDB.Table("gorm.people").Table("user").Find(&User{}).Statement |
| 54 |
if !regexp.MustCompile("SELECT \\* FROM .user. WHERE .user.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 55 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 56 |
} |
| 57 |
|
| 58 |
r = dryDB.Table("gorm.user").Select("name").Find(&User{}).Statement |
| 59 |
if !regexp.MustCompile("SELECT .name. FROM .gorm.\\..user. WHERE .user.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 60 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 61 |
} |
| 62 |
|
| 63 |
r = dryDB.Select("name").Find(&UserWithTable{}).Statement |
| 64 |
if !regexp.MustCompile("SELECT .name. FROM .gorm.\\..user. WHERE .user.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 65 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 66 |
} |
| 67 |
|
| 68 |
r = dryDB.Create(&UserWithTable{}).Statement |
| 69 |
if DB.Dialector.Name() != "sqlite" { |
| 70 |
if !regexp.MustCompile(`INSERT INTO .gorm.\..user. (.*name.*) VALUES (.*)`).MatchString(r.Statement.SQL.String()) { |
| 71 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 72 |
} |
| 73 |
} else { |
| 74 |
if !regexp.MustCompile(`INSERT INTO .user. (.*name.*) VALUES (.*)`).MatchString(r.Statement.SQL.String()) { |
| 75 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
r = dryDB.Table("(?) as u", DB.Model(&User{}).Select("name")).Find(&User{}).Statement |
| 80 |
if !regexp.MustCompile("SELECT \\* FROM \\(SELECT .name. FROM .users. WHERE .users.\\..deleted_at. IS NULL\\) as u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 81 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 82 |
} |
| 83 |
|
| 84 |
r = dryDB.Table("(?) as u, (?) as p", DB.Model(&User{}).Select("name"), DB.Model(&Pet{}).Select("name")).Find(&User{}).Statement |
| 85 |
if !regexp.MustCompile("SELECT \\* FROM \\(SELECT .name. FROM .users. WHERE .users.\\..deleted_at. IS NULL\\) as u, \\(SELECT .name. FROM .pets. WHERE .pets.\\..deleted_at. IS NULL\\) as p WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 86 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 87 |
} |
| 88 |
|
| 89 |
r = dryDB.Where("name = ?", 1).Table("(?) as u, (?) as p", DB.Model(&User{}).Select("name").Where("name = ?", 2), DB.Model(&Pet{}).Where("name = ?", 4).Select("name")).Where("name = ?", 3).Find(&User{}).Statement |
| 90 |
if !regexp.MustCompile("SELECT \\* FROM \\(SELECT .name. FROM .users. WHERE name = .+ AND .users.\\..deleted_at. IS NULL\\) as u, \\(SELECT .name. FROM .pets. WHERE name = .+ AND .pets.\\..deleted_at. IS NULL\\) as p WHERE name = .+ AND name = .+ AND .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 91 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 92 |
} |
| 93 |
|
| 94 |
AssertEqual(t, r.Statement.Vars, []interface{}{2, 4, 1, 3}) |
| 95 |
} |
| 96 |
|
| 97 |
func TestTableWithAllFields(t *testing.T) { |
| 98 |
dryDB := DB.Session(&gorm.Session{DryRun: true, QueryFields: true}) |
| 99 |
userQuery := "SELECT .*user.*id.*user.*created_at.*user.*updated_at.*user.*deleted_at.*user.*name.*user.*age" + |
| 100 |
".*user.*birthday.*user.*company_id.*user.*manager_id.*user.*active.* " |
| 101 |
|
| 102 |
r := dryDB.Table("`user`").Find(&User{}).Statement |
| 103 |
if !regexp.MustCompile(userQuery + "FROM `user`").MatchString(r.Statement.SQL.String()) { |
| 104 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 105 |
} |
| 106 |
|
| 107 |
r = dryDB.Table("user as u").Select("name").Find(&User{}).Statement |
| 108 |
if !regexp.MustCompile("SELECT .name. FROM user as u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 109 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 110 |
} |
| 111 |
|
| 112 |
r = dryDB.Table("gorm.user").Select("name").Find(&User{}).Statement |
| 113 |
if !regexp.MustCompile("SELECT .name. FROM .gorm.\\..user. WHERE .user.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 114 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 115 |
} |
| 116 |
|
| 117 |
r = dryDB.Select("name").Find(&UserWithTable{}).Statement |
| 118 |
if !regexp.MustCompile("SELECT .name. FROM .gorm.\\..user. WHERE .user.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 119 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 120 |
} |
| 121 |
|
| 122 |
r = dryDB.Create(&UserWithTable{}).Statement |
| 123 |
if DB.Dialector.Name() != "sqlite" { |
| 124 |
if !regexp.MustCompile(`INSERT INTO .gorm.\..user. (.*name.*) VALUES (.*)`).MatchString(r.Statement.SQL.String()) { |
| 125 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 126 |
} |
| 127 |
} else { |
| 128 |
if !regexp.MustCompile(`INSERT INTO .user. (.*name.*) VALUES (.*)`).MatchString(r.Statement.SQL.String()) { |
| 129 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
userQueryCharacter := "SELECT .*u.*id.*u.*created_at.*u.*updated_at.*u.*deleted_at.*u.*name.*u.*age.*u.*birthday" + |
| 134 |
".*u.*company_id.*u.*manager_id.*u.*active.* " |
| 135 |
|
| 136 |
r = dryDB.Table("(?) as u", DB.Model(&User{}).Select("name")).Find(&User{}).Statement |
| 137 |
if !regexp.MustCompile(userQueryCharacter + "FROM \\(SELECT .name. FROM .users. WHERE .users.\\..deleted_at. IS NULL\\) as u WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 138 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 139 |
} |
| 140 |
|
| 141 |
r = dryDB.Table("(?) as u, (?) as p", DB.Model(&User{}).Select("name"), DB.Model(&Pet{}).Select("name")).Find(&User{}).Statement |
| 142 |
if !regexp.MustCompile(userQueryCharacter + "FROM \\(SELECT .name. FROM .users. WHERE .users.\\..deleted_at. IS NULL\\) as u, \\(SELECT .name. FROM .pets. WHERE .pets.\\..deleted_at. IS NULL\\) as p WHERE .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 143 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 144 |
} |
| 145 |
|
| 146 |
r = dryDB.Where("name = ?", 1).Table("(?) as u, (?) as p", DB.Model(&User{}).Select("name").Where("name = ?", 2), DB.Model(&Pet{}).Where("name = ?", 4).Select("name")).Where("name = ?", 3).Find(&User{}).Statement |
| 147 |
if !regexp.MustCompile(userQueryCharacter + "FROM \\(SELECT .name. FROM .users. WHERE name = .+ AND .users.\\..deleted_at. IS NULL\\) as u, \\(SELECT .name. FROM .pets. WHERE name = .+ AND .pets.\\..deleted_at. IS NULL\\) as p WHERE name = .+ AND name = .+ AND .u.\\..deleted_at. IS NULL").MatchString(r.Statement.SQL.String()) { |
| 148 |
t.Errorf("Table with escape character, got %v", r.Statement.SQL.String()) |
| 149 |
} |
| 150 |
|
| 151 |
AssertEqual(t, r.Statement.Vars, []interface{}{2, 4, 1, 3}) |
| 152 |
} |
| 153 |
|
| 154 |
type UserWithTableNamer struct { |
| 155 |
gorm.Model |
| 156 |
Name string |
| 157 |
} |
| 158 |
|
| 159 |
func (UserWithTableNamer) TableName(namer schema.Namer) string { |
| 160 |
return namer.TableName("user") |
| 161 |
} |
| 162 |
|
| 163 |
func TestTableWithNamer(t *testing.T) { |
| 164 |
db, _ := gorm.Open(tests.DummyDialector{}, &gorm.Config{ |
| 165 |
NamingStrategy: schema.NamingStrategy{ |
| 166 |
TablePrefix: "t_", |
| 167 |
}, |
| 168 |
}) |
| 169 |
|
| 170 |
sql := db.ToSQL(func(tx *gorm.DB) *gorm.DB { |
| 171 |
return tx.Model(&UserWithTableNamer{}).Find(&UserWithTableNamer{}) |
| 172 |
}) |
| 173 |
|
| 174 |
if !regexp.MustCompile("SELECT \\* FROM `t_users`").MatchString(sql) { |
| 175 |
t.Errorf("Table with namer, got %v", sql) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestPostgresTableWithIdentifierLength(t *testing.T) { |
| 180 |
if DB.Dialector.Name() != "postgres" { |
| 181 |
return |
| 182 |
} |
| 183 |
|
| 184 |
type LongString struct { |
| 185 |
ThisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString string `gorm:"unique"` |
| 186 |
} |
| 187 |
|
| 188 |
t.Run("default", func(t *testing.T) { |
| 189 |
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{}) |
| 190 |
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy) |
| 191 |
if err != nil { |
| 192 |
t.Fatalf("failed to parse user unique, got error %v", err) |
| 193 |
} |
| 194 |
|
| 195 |
constraints := user.ParseUniqueConstraints() |
| 196 |
if len(constraints) != 1 { |
| 197 |
t.Fatalf("failed to find unique constraint, got %v", constraints) |
| 198 |
} |
| 199 |
|
| 200 |
for key := range constraints { |
| 201 |
if len(key) != 63 { |
| 202 |
t.Errorf("failed to find unique constraint, got %v", constraints) |
| 203 |
} |
| 204 |
} |
| 205 |
}) |
| 206 |
|
| 207 |
t.Run("naming strategy", func(t *testing.T) { |
| 208 |
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{ |
| 209 |
NamingStrategy: schema.NamingStrategy{}, |
| 210 |
}) |
| 211 |
|
| 212 |
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy) |
| 213 |
if err != nil { |
| 214 |
t.Fatalf("failed to parse user unique, got error %v", err) |
| 215 |
} |
| 216 |
|
| 217 |
constraints := user.ParseUniqueConstraints() |
| 218 |
if len(constraints) != 1 { |
| 219 |
t.Fatalf("failed to find unique constraint, got %v", constraints) |
| 220 |
} |
| 221 |
|
| 222 |
for key := range constraints { |
| 223 |
if len(key) != 63 { |
| 224 |
t.Errorf("failed to find unique constraint, got %v", constraints) |
| 225 |
} |
| 226 |
} |
| 227 |
}) |
| 228 |
|
| 229 |
t.Run("namer", func(t *testing.T) { |
| 230 |
uname := "custom_unique_name" |
| 231 |
db, _ := gorm.Open(postgres.Open(postgresDSN), &gorm.Config{ |
| 232 |
NamingStrategy: mockUniqueNamingStrategy{ |
| 233 |
UName: uname, |
| 234 |
}, |
| 235 |
}) |
| 236 |
|
| 237 |
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy) |
| 238 |
if err != nil { |
| 239 |
t.Fatalf("failed to parse user unique, got error %v", err) |
| 240 |
} |
| 241 |
|
| 242 |
constraints := user.ParseUniqueConstraints() |
| 243 |
if len(constraints) != 1 { |
| 244 |
t.Fatalf("failed to find unique constraint, got %v", constraints) |
| 245 |
} |
| 246 |
|
| 247 |
for key := range constraints { |
| 248 |
if key != uname { |
| 249 |
t.Errorf("failed to find unique constraint, got %v", constraints) |
| 250 |
} |
| 251 |
} |
| 252 |
}) |
| 253 |
} |
| 254 |
|
| 255 |
func TestGaussDBTableWithIdentifierLength(t *testing.T) { |
| 256 |
if DB.Dialector.Name() != "gaussdb" { |
| 257 |
return |
| 258 |
} |
| 259 |
|
| 260 |
type LongString struct { |
| 261 |
ThisIsAVeryVeryVeryVeryVeryVeryVeryVeryVeryLongString string `gorm:"unique"` |
| 262 |
} |
| 263 |
|
| 264 |
t.Run("default", func(t *testing.T) { |
| 265 |
db, _ := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{}) |
| 266 |
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy) |
| 267 |
if err != nil { |
| 268 |
t.Fatalf("failed to parse user unique, got error %v", err) |
| 269 |
} |
| 270 |
|
| 271 |
constraints := user.ParseUniqueConstraints() |
| 272 |
if len(constraints) != 1 { |
| 273 |
t.Fatalf("failed to find unique constraint, got %v", constraints) |
| 274 |
} |
| 275 |
|
| 276 |
for key := range constraints { |
| 277 |
if len(key) != 63 { |
| 278 |
t.Errorf("failed to find unique constraint, got %v", constraints) |
| 279 |
} |
| 280 |
} |
| 281 |
}) |
| 282 |
|
| 283 |
t.Run("naming strategy", func(t *testing.T) { |
| 284 |
db, _ := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{ |
| 285 |
NamingStrategy: schema.NamingStrategy{}, |
| 286 |
}) |
| 287 |
|
| 288 |
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy) |
| 289 |
if err != nil { |
| 290 |
t.Fatalf("failed to parse user unique, got error %v", err) |
| 291 |
} |
| 292 |
|
| 293 |
constraints := user.ParseUniqueConstraints() |
| 294 |
if len(constraints) != 1 { |
| 295 |
t.Fatalf("failed to find unique constraint, got %v", constraints) |
| 296 |
} |
| 297 |
|
| 298 |
for key := range constraints { |
| 299 |
if len(key) != 63 { |
| 300 |
t.Errorf("failed to find unique constraint, got %v", constraints) |
| 301 |
} |
| 302 |
} |
| 303 |
}) |
| 304 |
|
| 305 |
t.Run("namer", func(t *testing.T) { |
| 306 |
uname := "custom_unique_name" |
| 307 |
db, _ := gorm.Open(gaussdb.Open(gaussdbDSN), &gorm.Config{ |
| 308 |
NamingStrategy: mockUniqueNamingStrategy{ |
| 309 |
UName: uname, |
| 310 |
}, |
| 311 |
}) |
| 312 |
|
| 313 |
user, err := schema.Parse(&LongString{}, &sync.Map{}, db.Config.NamingStrategy) |
| 314 |
if err != nil { |
| 315 |
t.Fatalf("failed to parse user unique, got error %v", err) |
| 316 |
} |
| 317 |
|
| 318 |
constraints := user.ParseUniqueConstraints() |
| 319 |
if len(constraints) != 1 { |
| 320 |
t.Fatalf("failed to find unique constraint, got %v", constraints) |
| 321 |
} |
| 322 |
|
| 323 |
for key := range constraints { |
| 324 |
if key != uname { |
| 325 |
t.Errorf("failed to find unique constraint, got %v", constraints) |
| 326 |
} |
| 327 |
} |
| 328 |
}) |
| 329 |
} |
| 330 |
|
| 331 |
type mockUniqueNamingStrategy struct { |
| 332 |
UName string |
| 333 |
schema.NamingStrategy |
| 334 |
} |
| 335 |
|
| 336 |
func (a mockUniqueNamingStrategy) UniqueName(table, column string) string { |
| 337 |
return a.UName |
| 338 |
} |