@@ -13,6 +13,7 @@ import (
13
13
"github.com/xfali/gobatis"
14
14
"github.com/xfali/gobatis/factory"
15
15
"github.com/xfali/gobatis/logging"
16
+ "strings"
16
17
"testing"
17
18
"time"
18
19
)
@@ -25,17 +26,76 @@ type TestTable struct {
25
26
}
26
27
27
28
func TestPageHelper (t * testing.T ) {
28
- ctx , _ := context .WithTimeout (context .Background (), 2 * time .Second )
29
- ctx = StartPage (1 , 2 , ctx )
30
-
31
- p := ctx .Value (pageHelperValue )
32
- printV (t , p )
33
-
34
- select {
35
- case <- ctx .Done ():
36
- break
37
- }
38
- printV (t , p )
29
+ t .Run ("StartPage" , func (t * testing.T ) {
30
+ ctx , _ := context .WithTimeout (context .Background (), 2 * time .Second )
31
+ ctx = StartPage (1 , 2 , ctx )
32
+
33
+ p := ctx .Value (pageHelperValue )
34
+ printPage (t , p )
35
+
36
+ select {
37
+ case <- ctx .Done ():
38
+ break
39
+ }
40
+ printPage (t , p )
41
+ })
42
+
43
+ t .Run ("OrderBy" , func (t * testing.T ) {
44
+ ctx , _ := context .WithTimeout (context .Background (), 2 * time .Second )
45
+ ctx = OrderBy ("test" , ASC , ctx )
46
+
47
+ p := ctx .Value (orderHelperValue )
48
+ printOrder (t , p )
49
+
50
+ select {
51
+ case <- ctx .Done ():
52
+ break
53
+ }
54
+ printOrder (t , p )
55
+ })
56
+
57
+ t .Run ("PageHelper and OrderBy" , func (t * testing.T ) {
58
+ ctx , _ := context .WithTimeout (context .Background (), 2 * time .Second )
59
+ ctx = OrderBy ("test" , ASC , ctx )
60
+ ctx = StartPage (1 , 2 , ctx )
61
+
62
+ o := ctx .Value (orderHelperValue )
63
+ printOrder (t , o )
64
+
65
+ p := ctx .Value (pageHelperValue )
66
+ printPage (t , p )
67
+
68
+ select {
69
+ case <- ctx .Done ():
70
+ break
71
+ }
72
+ printPage (t , p )
73
+ printOrder (t , o )
74
+ })
75
+
76
+ t .Run ("complex" , func (t * testing.T ) {
77
+ ctx , _ := context .WithTimeout (context .Background (), 2 * time .Second )
78
+ ctx = OrderBy ("test" , ASC , ctx )
79
+ ctx = StartPage (1 , 2 , ctx )
80
+ ctx = StartPage (3 , 10 , ctx )
81
+ ctx = OrderBy ("tat" , DESC , ctx )
82
+ ctx , _ = context .WithTimeout (ctx , time .Second )
83
+
84
+ now := time .Now ()
85
+ o := ctx .Value (orderHelperValue )
86
+ printOrder (t , o )
87
+ t .Logf ("time :%d ms \n " , time .Since (now )/ time .Millisecond )
88
+
89
+ p := ctx .Value (pageHelperValue )
90
+ printPage (t , p )
91
+
92
+ select {
93
+ case <- ctx .Done ():
94
+ break
95
+ }
96
+ printPage (t , p )
97
+ printOrder (t , o )
98
+ })
39
99
}
40
100
41
101
func TestPageHelper2 (t * testing.T ) {
@@ -63,14 +123,56 @@ func TestPageHelper2(t *testing.T) {
63
123
session .Select ("SELECT * FROM TBL_TEST" ).Param ().Result (& ret )
64
124
}
65
125
66
- func TestModify (t * testing.T ) {
126
+ func TestModifyPage (t * testing.T ) {
67
127
sql := modifySql ("select * from x" , & PageParam {1 , 2 })
68
128
t .Log (sql )
69
129
}
70
130
71
- func printV (t * testing.T , p interface {}) {
131
+ func order (sql string , params ... interface {}) (string , []interface {}) {
132
+ return modifySqlOrder (sql , & OrderParam {"test" , ASC }, params )
133
+ }
134
+
135
+ func TestModifyOrder (t * testing.T ) {
136
+ sql , p := order ("select ? from x" , "field1" )
137
+ t .Log (sql )
138
+ if len (p ) != 2 {
139
+ t .Fatal ()
140
+ }
141
+ for _ , v := range p {
142
+ t .Log (v )
143
+ }
144
+ }
145
+
146
+ func TestModifyOrderAndPage (t * testing.T ) {
147
+ sql , p := order ("select ? from x" , "field1" )
148
+ t .Log (sql )
149
+ if len (p ) != 2 {
150
+ t .Fatal ()
151
+ }
152
+
153
+ sql = modifySql (sql , & PageParam {1 , 2 })
154
+
155
+ t .Log (sql )
156
+ for _ , v := range p {
157
+ t .Log (v )
158
+ }
159
+
160
+ if strings .TrimSpace (sql ) != "select ? from x ORDER BY ? ASC LIMIT 2, 2" {
161
+ t .Fail ()
162
+ }
163
+ }
164
+
165
+ func printPage (t * testing.T , p interface {}) {
72
166
if p , ok := p .(* PageParam ); ok {
73
- t .Logf ("param: %d %d" , p .Page , p .PageSize )
167
+ t .Logf ("page param: %d %d" , p .Page , p .PageSize )
168
+ } else {
169
+ t .Fail ()
170
+ }
171
+ }
172
+
173
+ func printOrder (t * testing.T , p interface {}) {
174
+ if p , ok := p .(* OrderParam ); ok {
175
+ t .Logf ("order param: %s %s" , p .Field , p .Order )
74
176
} else {
75
177
t .Fail ()
76
178
}
0 commit comments