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

Function TestOrder

tests/query_test.go:1018–1049  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1016}
1017
1018func TestOrder(t *testing.T) {
1019 dryDB := DB.Session(&gorm.Session{DryRun: true})
1020
1021 result := dryDB.Order("").Find(&User{})
1022 if !regexp.MustCompile("SELECT \\* FROM .*users.* IS NULL$").MatchString(result.Statement.SQL.String()) {
1023 t.Fatalf("Build Order condition, but got %v", result.Statement.SQL.String())
1024 }
1025
1026 result = dryDB.Order(nil).Find(&User{})
1027 if !regexp.MustCompile("SELECT \\* FROM .*users.* IS NULL$").MatchString(result.Statement.SQL.String()) {
1028 t.Fatalf("Build Order condition, but got %v", result.Statement.SQL.String())
1029 }
1030
1031 result = dryDB.Order("age desc, name").Find(&User{})
1032 if !regexp.MustCompile("SELECT \\* FROM .*users.* ORDER BY age desc, name").MatchString(result.Statement.SQL.String()) {
1033 t.Fatalf("Build Order condition, but got %v", result.Statement.SQL.String())
1034 }
1035
1036 result = dryDB.Order("age desc").Order("name").Find(&User{})
1037 if !regexp.MustCompile("SELECT \\* FROM .*users.* ORDER BY age desc,name").MatchString(result.Statement.SQL.String()) {
1038 t.Fatalf("Build Order condition, but got %v", result.Statement.SQL.String())
1039 }
1040
1041 stmt := dryDB.Clauses(clause.OrderBy{
1042 Expression: clause.Expr{SQL: "FIELD(id,?)", Vars: []interface{}{[]int{1, 2, 3}}, WithoutParentheses: true},
1043 }).Find(&User{}).Statement
1044
1045 explainedSQL := dryDB.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
1046 if !regexp.MustCompile("SELECT \\* FROM .*users.* ORDER BY FIELD\\(id,1,2,3\\)").MatchString(explainedSQL) {
1047 t.Fatalf("Build Order condition, but got %v", explainedSQL)
1048 }
1049}
1050
1051func TestOrderWithAllFields(t *testing.T) {
1052 dryDB := DB.Session(&gorm.Session{DryRun: true, QueryFields: true})

Callers

nothing calls this directly

Calls 6

SessionMethod · 0.80
ClausesMethod · 0.80
FindMethod · 0.65
OrderMethod · 0.65
ExplainMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected