| 1016 | } |
| 1017 | |
| 1018 | func 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 | |
| 1051 | func TestOrderWithAllFields(t *testing.T) { |
| 1052 | dryDB := DB.Session(&gorm.Session{DryRun: true, QueryFields: true}) |