1616// limitations under the License.
1717package xb
1818
19- import "testing"
19+ import (
20+ "testing"
21+ )
2022
2123// 测试 ASC 函数
2224func TestASC (t * testing.T ) {
@@ -35,35 +37,45 @@ func TestDESC(t *testing.T) {
3537}
3638
3739// 测试 Sort 结构
40+ type testStruct struct {
41+ Id int64 `db:"id"`
42+ CreatedAt int64 `db:"created_at"`
43+ Status string `db:"status"`
44+ }
45+
46+ func (* testStruct ) TableName () string {
47+ return "test_table"
48+ }
49+
3850func TestSort (t * testing.T ) {
3951 // 测试 ASC 排序
4052 builder := Of (& testStruct {}).
41- Sort ("id" , ASC () ).
53+ Sort ("id" , ASC ).
4254 Build ()
4355
44- sql , _ , _ := builder .SqlOfCond ()
45- if sql != "ORDER BY id ASC" {
56+ sql , _ , _ := builder .SqlOfSelect ()
57+ if ! containsString ( sql , "ORDER BY id ASC" ) {
4658 t .Errorf ("Sort ASC failed, got: %s" , sql )
4759 }
4860
4961 // 测试 DESC 排序
5062 builder = Of (& testStruct {}).
51- Sort ("created_at" , DESC () ).
63+ Sort ("created_at" , DESC ).
5264 Build ()
5365
54- sql , _ , _ = builder .SqlOfCond ()
55- if sql != "ORDER BY created_at DESC" {
66+ sql , _ , _ = builder .SqlOfSelect ()
67+ if ! containsString ( sql , "ORDER BY created_at DESC" ) {
5668 t .Errorf ("Sort DESC failed, got: %s" , sql )
5769 }
5870
5971 // 测试多个排序
6072 builder = Of (& testStruct {}).
61- Sort ("status" , ASC () ).
62- Sort ("id" , DESC () ).
73+ Sort ("status" , ASC ).
74+ Sort ("id" , DESC ).
6375 Build ()
6476
65- sql , _ , _ = builder .SqlOfCond ()
66- if sql != "ORDER BY status ASC, id DESC" {
77+ sql , _ , _ = builder .SqlOfSelect ()
78+ if ! containsString ( sql , "ORDER BY status ASC, id DESC" ) {
6779 t .Errorf ("Multiple Sort failed, got: %s" , sql )
6880 }
6981}
0 commit comments