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

Function TestGroupConditions

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

Source from the content-addressed store, hash-verified

222}
223
224func TestGroupConditions(t *testing.T) {
225 type Pizza struct {
226 ID uint
227 Name string
228 Size string
229 }
230 dryRunDB := DB.Session(&gorm.Session{DryRun: true})
231
232 stmt := dryRunDB.Where(
233 DB.Where("pizza = ?", "pepperoni").Where(DB.Where("size = ?", "small").Or("size = ?", "medium")),
234 ).Or(
235 DB.Where("pizza = ?", "hawaiian").Where("size = ?", "xlarge"),
236 ).Find(&Pizza{}).Statement
237
238 execStmt := dryRunDB.Exec("WHERE (pizza = ? AND (size = ? OR size = ?)) OR (pizza = ? AND size = ?)", "pepperoni", "small", "medium", "hawaiian", "xlarge").Statement
239
240 result := DB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
241 expects := DB.Dialector.Explain(execStmt.SQL.String(), execStmt.Vars...)
242
243 if !strings.HasSuffix(result, expects) {
244 t.Errorf("expects: %v, got %v", expects, result)
245 }
246
247 stmt2 := dryRunDB.Where(
248 DB.Scopes(NameIn1And2),
249 ).Or(
250 DB.Where("pizza = ?", "hawaiian").Where("size = ?", "xlarge"),
251 ).Find(&Pizza{}).Statement
252
253 execStmt2 := dryRunDB.Exec(`WHERE name in ? OR (pizza = ? AND size = ?)`, []string{"ScopeUser1", "ScopeUser2"}, "hawaiian", "xlarge").Statement
254
255 result2 := DB.Dialector.Explain(stmt2.SQL.String(), stmt2.Vars...)
256 expects2 := DB.Dialector.Explain(execStmt2.SQL.String(), execStmt2.Vars...)
257
258 if !strings.HasSuffix(result2, expects2) {
259 t.Errorf("expects: %v, got %v", expects2, result2)
260 }
261}
262
263func TestCombineStringConditions(t *testing.T) {
264 dryRunDB := DB.Session(&gorm.Session{DryRun: true})

Callers

nothing calls this directly

Calls 8

SessionMethod · 0.80
FindMethod · 0.65
OrMethod · 0.65
WhereMethod · 0.65
ExecMethod · 0.65
ExplainMethod · 0.65
ScopesMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected