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

Function TestJoinConds

tests/joins_test.go:61–113  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

59}
60
61func TestJoinConds(t *testing.T) {
62 user := *GetUser("joins-conds", Config{Account: true, Pets: 3})
63 DB.Save(&user)
64
65 var users1 []User
66 DB.Joins("inner join pets on pets.user_id = users.id").Where("users.name = ?", user.Name).Find(&users1)
67 if len(users1) != 3 {
68 t.Errorf("should find two users using left join, but got %v", len(users1))
69 }
70
71 var users2 []User
72 DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Where("users.name = ?", user.Name).First(&users2)
73 if len(users2) != 1 {
74 t.Errorf("should find one users using left join with conditions, but got %v", len(users2))
75 }
76
77 var users3 []User
78 DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where("users.name = ?", user.Name).First(&users3)
79 if len(users3) != 1 {
80 t.Errorf("should find one users using multiple left join conditions, but got %v", len(users3))
81 }
82
83 var users4 []User
84 DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number+"non-exist").Where("users.name = ?", user.Name).First(&users4)
85 if len(users4) != 0 {
86 t.Errorf("should find no user when searching with unexisting credit card, but got %v", len(users4))
87 }
88
89 var users5 []User
90 db5 := DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where(User{Model: gorm.Model{ID: 1}}).Where(Account{Model: gorm.Model{ID: 1}}).Not(Pet{Model: gorm.Model{ID: 1}}).Find(&users5)
91 if db5.Error != nil {
92 t.Errorf("Should not raise error for join where identical fields in different tables. Error: %s", db5.Error.Error())
93 }
94
95 var users6 []User
96 DB.Joins("inner join pets on pets.user_id = users.id AND pets.name = @Name", user.Pets[0]).Where("users.name = ?", user.Name).First(&users6)
97 if len(users6) != 1 {
98 t.Errorf("should find one users using left join with conditions, but got %v", len(users6))
99 }
100
101 dryDB := DB.Session(&gorm.Session{DryRun: true})
102 stmt := dryDB.Joins("left join pets on pets.user_id = users.id AND pets.name = ?", user.Pets[0].Name).Joins("join accounts on accounts.user_id = users.id AND accounts.number = ?", user.Account.Number).Where(User{Model: gorm.Model{ID: 1}}).Where(Account{Model: gorm.Model{ID: 1}}).Not(Pet{Model: gorm.Model{ID: 1}}).Find(&users5).Statement
103
104 if !regexp.MustCompile("SELECT .* FROM .users. left join pets.*join accounts.*").MatchString(stmt.SQL.String()) {
105 t.Errorf("joins should be ordered, but got %v", stmt.SQL.String())
106 }
107
108 iv := DB.Table(`table_invoices`).Select(`seller, SUM(total) as total, SUM(paid) as paid, SUM(balance) as balance`).Group(`seller`)
109 stmt = dryDB.Table(`table_employees`).Select(`id, name, iv.total, iv.paid, iv.balance`).Joins(`LEFT JOIN (?) AS iv ON iv.seller = table_employees.id`, iv).Scan(&user).Statement
110 if !regexp.MustCompile("SELECT id, name, iv.total, iv.paid, iv.balance FROM .table_employees. LEFT JOIN \\(SELECT seller, SUM\\(total\\) as total, SUM\\(paid\\) as paid, SUM\\(balance\\) as balance FROM .table_invoices. GROUP BY .seller.\\) AS iv ON iv.seller = table_employees.id").MatchString(stmt.SQL.String()) {
111 t.Errorf("joins should be ordered, but got %v", stmt.SQL.String())
112 }
113}
114
115func TestJoinOn(t *testing.T) {
116 user := *GetUser("joins-on", Config{Pets: 2})

Callers

nothing calls this directly

Calls 14

GetUserFunction · 0.85
SaveMethod · 0.80
SessionMethod · 0.80
FindMethod · 0.65
WhereMethod · 0.65
JoinsMethod · 0.65
FirstMethod · 0.65
NotMethod · 0.65
ErrorMethod · 0.65
GroupMethod · 0.65
SelectMethod · 0.65
TableMethod · 0.65

Tested by

no test coverage detected