MCPcopy
hub / github.com/go-gorm/gorm / TestTable

Function TestTable

tests/table_test.go:25–95  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

23}
24
25func 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 }

Callers

nothing calls this directly

Calls 10

SessionMethod · 0.80
ModelMethod · 0.80
FindMethod · 0.65
TableMethod · 0.65
SelectMethod · 0.65
CreateMethod · 0.65
NameMethod · 0.65
WhereMethod · 0.65
AssertEqualFunction · 0.50
StringMethod · 0.45

Tested by

no test coverage detected