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

Function TestChainableAPI

tests/chainable_api_test.go:43–133  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

41}
42
43func 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")

Callers

nothing calls this directly

Calls 14

newTestDBFunction · 0.85
ModelMethod · 0.80
LenMethod · 0.80
TableMethod · 0.65
DistinctMethod · 0.65
WhereMethod · 0.65
OrderMethod · 0.65
OffsetMethod · 0.65
LimitMethod · 0.65
JoinsMethod · 0.65
PreloadMethod · 0.65
ScopesMethod · 0.65

Tested by

no test coverage detected