(t *testing.T)
| 41 | } |
| 42 | |
| 43 | func TestChainableAPI(t *testing.T) { |
| 44 | db := newTestDB() |
| 45 | |
| 46 | // Model |
| 47 | m := &struct{ ID int }{} |
| 48 | tx := db.Model(m) |
| 49 | if tx.Statement.Model != m { |
| 50 | t.Fatalf("Model not set, got %v", tx.Statement.Model) |
| 51 | } |
| 52 | |
| 53 | // Table |
| 54 | tx = tx.Table("users") |
| 55 | if tx.Statement.Table != "users" { |
| 56 | t.Fatalf("Table not set, got %v", tx.Statement.Table) |
| 57 | } |
| 58 | if tx.Statement.TableExpr == nil { |
| 59 | t.Fatalf("TableExpr expected to be set") |
| 60 | } |
| 61 | |
| 62 | // Distinct + Select |
| 63 | tx = tx.Distinct("name", "age") |
| 64 | if !tx.Statement.Distinct { |
| 65 | t.Fatalf("Distinct expected true") |
| 66 | } |
| 67 | if len(tx.Statement.Selects) != 2 || tx.Statement.Selects[0] != "name" { |
| 68 | t.Fatalf("Selects expected [name age], got %v", tx.Statement.Selects) |
| 69 | } |
| 70 | |
| 71 | // Where |
| 72 | tx = tx.Where("age = ?", 20) |
| 73 | c, ok := tx.Statement.Clauses["WHERE"] |
| 74 | if !ok { |
| 75 | t.Fatalf("WHERE clause expected") |
| 76 | } |
| 77 | if where, ok := c.Expression.(clause.Where); !ok || len(where.Exprs) == 0 { |
| 78 | t.Fatalf("WHERE expressions expected, got %v", c.Expression) |
| 79 | } |
| 80 | |
| 81 | // Order |
| 82 | tx = tx.Order("name DESC") |
| 83 | if _, ok := tx.Statement.Clauses["ORDER BY"]; !ok { |
| 84 | t.Fatalf("ORDER BY clause expected") |
| 85 | } |
| 86 | |
| 87 | // Limit / Offset |
| 88 | tx = tx.Limit(10).Offset(5) |
| 89 | if cl, ok := tx.Statement.Clauses["LIMIT"]; !ok { |
| 90 | t.Fatalf("LIMIT clause expected") |
| 91 | } else { |
| 92 | if limit, ok := cl.Expression.(clause.Limit); !ok || limit.Limit == nil || *limit.Limit != 10 || limit.Offset != 5 { |
| 93 | t.Fatalf("LIMIT/Offset values unexpected: %v", cl.Expression) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Joins |
| 98 | tx = tx.Joins("JOIN accounts ON accounts.user_id = users.id") |
| 99 | if len(tx.Statement.Joins) == 0 { |
| 100 | t.Fatalf("Joins expected") |
nothing calls this directly
no test coverage detected