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

Function TestFromWithJoins

tests/sql_builder_test.go:316–358  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

314}
315
316func 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
360func TestToSQL(t *testing.T) {
361 // By default DB.DryRun should false

Callers

nothing calls this directly

Calls 7

SessionMethod · 0.80
ClausesMethod · 0.80
ContainsMethod · 0.80
TableMethod · 0.65
JoinsMethod · 0.65
FirstMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected