5
5
"fmt"
6
6
"io/ioutil"
7
7
"os"
8
+ "sort"
8
9
"testing"
9
10
10
11
ds "github.com/ipfs/go-datastore"
@@ -101,6 +102,33 @@ func testQuery(t *testing.T, d *Datastore) {
101
102
"/a/b/d" ,
102
103
"/a/c" ,
103
104
}, rs )
105
+
106
+ // test order
107
+
108
+ rs , err = d .Query (dsq.Query {Orders : []dsq.Order {dsq.OrderByKey {}}})
109
+ if err != nil {
110
+ t .Fatal (err )
111
+ }
112
+
113
+ keys := make ([]string , 0 , len (testcases ))
114
+ for k := range testcases {
115
+ keys = append (keys , k )
116
+ }
117
+ sort .Strings (keys )
118
+
119
+ expectOrderedMatches (t , keys , rs )
120
+
121
+ rs , err = d .Query (dsq.Query {Orders : []dsq.Order {dsq.OrderByKeyDescending {}}})
122
+ if err != nil {
123
+ t .Fatal (err )
124
+ }
125
+
126
+ // reverse
127
+ for i , j := 0 , len (keys )- 1 ; i < j ; i , j = i + 1 , j - 1 {
128
+ keys [i ], keys [j ] = keys [j ], keys [i ]
129
+ }
130
+
131
+ expectOrderedMatches (t , keys , rs )
104
132
}
105
133
106
134
func TestQuery (t * testing.T ) {
@@ -125,6 +153,7 @@ func TestQueryRespectsProcessMem(t *testing.T) {
125
153
}
126
154
127
155
func expectMatches (t * testing.T , expect []string , actualR dsq.Results ) {
156
+ t .Helper ()
128
157
actual , err := actualR .Rest ()
129
158
if err != nil {
130
159
t .Error (err )
@@ -146,6 +175,23 @@ func expectMatches(t *testing.T, expect []string, actualR dsq.Results) {
146
175
}
147
176
}
148
177
178
+ func expectOrderedMatches (t * testing.T , expect []string , actualR dsq.Results ) {
179
+ t .Helper ()
180
+ actual , err := actualR .Rest ()
181
+ if err != nil {
182
+ t .Error (err )
183
+ }
184
+
185
+ if len (actual ) != len (expect ) {
186
+ t .Error ("not enough" , expect , actual )
187
+ }
188
+ for i := range expect {
189
+ if expect [i ] != actual [i ].Key {
190
+ t .Errorf ("expected %q, got %q" , expect [i ], actual [i ].Key )
191
+ }
192
+ }
193
+ }
194
+
149
195
func testBatching (t * testing.T , d * Datastore ) {
150
196
b , err := d .Batch ()
151
197
if err != nil {
0 commit comments