| 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 | } |