| 314 | } |
| 315 | |
| 316 | func TestFromWithJoins(t *testing.T) { |
| 317 | var result User |
| 318 | |
| 319 | newDB := DB.Session(&gorm.Session{NewDB: true, DryRun: true}).Table("users") |
| 320 | |
| 321 | newDB.Clauses( |
| 322 | clause.From{ |
| 323 | Tables: []clause.Table{{Name: "users"}}, |
| 324 | Joins: []clause.Join{ |
| 325 | { |
| 326 | Table: clause.Table{Name: "companies", Raw: false}, |
| 327 | ON: clause.Where{ |
| 328 | Exprs: []clause.Expression{ |
| 329 | clause.Eq{ |
| 330 | Column: clause.Column{ |
| 331 | Table: "users", |
| 332 | Name: "company_id", |
| 333 | }, |
| 334 | Value: clause.Column{ |
| 335 | Table: "companies", |
| 336 | Name: "id", |
| 337 | }, |
| 338 | }, |
| 339 | }, |
| 340 | }, |
| 341 | }, |
| 342 | }, |
| 343 | }, |
| 344 | ) |
| 345 | |
| 346 | newDB.Joins("inner join rgs on rgs.id = user.id") |
| 347 | |
| 348 | stmt := newDB.First(&result).Statement |
| 349 | str := stmt.SQL.String() |
| 350 | |
| 351 | if !strings.Contains(str, "rgs.id = user.id") { |
| 352 | t.Errorf("The second join condition is over written instead of combining") |
| 353 | } |
| 354 | |
| 355 | if !strings.Contains(str, "`users`.`company_id` = `companies`.`id`") && !strings.Contains(str, "\"users\".\"company_id\" = \"companies\".\"id\"") { |
| 356 | t.Errorf("The first join condition is over written instead of combining") |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | func TestToSQL(t *testing.T) { |
| 361 | // By default DB.DryRun should false |