UNPKG

7.24 kBJavaScriptView Raw
1'use strict';
2
3var testUtils = require('../test-utils');
4var should = testUtils.should;
5
6module.exports = function (dbType, context) {
7 describe(dbType + ': sorting', function () {
8
9 it('sorts correctly - just _id', function () {
10 var db = context.db;
11 return db.bulkDocs([
12 {_id: 'a', foo: 'a'},
13 {_id: 'b', foo: 'b'}
14 ]).then(function () {
15 return db.find({
16 "selector": {"_id": {$gte: "a"}},
17 "fields": ["_id", "foo"],
18 "sort": [{"_id": "asc"}]
19 });
20 }).then(function (resp) {
21 resp.should.deep.equal({
22 "docs": [
23 {"_id": "a", "foo": "a"},
24 {"_id": "b", "foo": "b"}
25 ]
26 });
27 });
28 });
29
30 it('sorts correctly - just _id desc', function () {
31 var db = context.db;
32 return db.bulkDocs([
33 {_id: 'a', foo: 'a'},
34 {_id: 'b', foo: 'b'}
35 ]).then(function () {
36 return db.find({
37 "selector": {"_id": {$gte: "a"}},
38 "fields": ["_id", "foo"],
39 "sort": [{"_id": "desc"}]
40 });
41 }).then(function (resp) {
42 resp.should.deep.equal({
43 "docs": [
44 {"_id": "b", "foo": "b"},
45 {"_id": "a", "foo": "a"}
46 ]
47 });
48 });
49 });
50
51 it('sorts correctly - foo desc', function () {
52 var db = context.db;
53 var index = {
54 "index": {
55 "fields": [{"foo": "desc"}]
56 },
57 "name": "foo-index",
58 "type": "json"
59 };
60 return db.createIndex(index).then(function () {
61 return db.bulkDocs([
62 {_id: 'a', foo: 'b'},
63 {_id: 'b', foo: 'a'},
64 {_id: 'c', foo: 'c'},
65 {_id: '0', foo: 'd'}
66 ]);
67 }).then(function () {
68 return db.find({
69 "selector": {"foo": {$lte: "d"}},
70 "fields": ["foo"]
71 });
72 }).then(function (resp) {
73 resp.should.deep.equal({
74 "docs": [
75 {"foo": "a"},
76 {"foo": "b"},
77 {"foo": "c"},
78 {"foo": "d"}
79 ]
80 });
81 });
82 });
83
84 it('sorts correctly - foo desc 2', function () {
85 var db = context.db;
86 var index = {
87 "index": {
88 "fields": [{"foo": "desc"}]
89 },
90 "name": "foo-index",
91 "type": "json"
92 };
93 return db.createIndex(index).then(function () {
94 return db.bulkDocs([
95 {_id: 'a', foo: 'b'},
96 {_id: 'b', foo: 'a'},
97 {_id: 'c', foo: 'c'},
98 {_id: '0', foo: 'd'}
99 ]);
100 }).then(function () {
101 return db.find({
102 "selector": {"foo": {$lte: "d"}},
103 "fields": ["foo"],
104 "sort": [{foo: "desc"}]
105 });
106 }).then(function (resp) {
107 resp.should.deep.equal({
108 "docs": [
109 {"foo": "d"},
110 {"foo": "c"},
111 {"foo": "b"},
112 {"foo": "a"}
113 ]
114 });
115 });
116 });
117
118 it('sorts correctly - complex', function () {
119 var db = context.db;
120 var index = {
121 "index": {
122 "fields": ["foo"]
123 },
124 "name": "foo-index",
125 "type": "json"
126 };
127 return db.createIndex(index).then(function () {
128 return db.bulkDocs([
129 { _id: '1', foo: 'AAA'},
130 { _id: '2', foo: 'aAA' },
131 { _id: '3', foo: 'BAA'},
132 { _id: '4', foo: 'bAA'},
133 { _id: '5', foo: '\u0000aAA'},
134 { _id: '6', foo: '\u0001AAA'}
135 ]);
136 }).then(function () {
137 return db.find({
138 "selector": {"foo": {"$gt": "\u0000\u0000"}},
139 "fields": ["_id", "foo"],
140 "sort": [{"foo": "asc"}]
141 });
142 }).then(function (resp) {
143 // ASCII vs ICU ordering. just gonna hack this
144 if (dbType === 'http') {
145 resp.should.deep.equal({
146 "docs": [
147 { "_id": "2", "foo": "aAA"},
148 { "_id": "5", "foo": "\u0000aAA"},
149 { "_id": "1", "foo": "AAA"},
150 { "_id": "6", "foo": "\u0001AAA"},
151 { "_id": "4", "foo": "bAA"},
152 { "_id": "3", "foo": "BAA"}
153 ]
154 });
155 } else {
156 resp.should.deep.equal({
157 docs: [
158 { _id: '5', foo: '\u0000aAA' },
159 { _id: '6', foo: '\u0001AAA' },
160 { _id: '1', foo: 'AAA' },
161 { _id: '3', foo: 'BAA' },
162 { _id: '2', foo: 'aAA' },
163 { _id: '4', foo: 'bAA' }
164 ]
165 });
166 }
167 });
168 });
169
170
171 it('supported mixed sort', function () {
172 var db = context.db;
173 var index = {
174 "index": {
175 "fields": [
176 "foo",
177 "bar"
178 ]
179 },
180 "name": "foo-index",
181 "type": "json"
182 };
183 return db.createIndex(index).then(function () {
184 return db.bulkDocs([
185 {_id: 'a1', foo: 'a', bar: '1'},
186 {_id: 'a2', foo: 'a', bar: '2'},
187 {_id: 'b1', foo: 'b', bar: '1'}
188 ]);
189 }).then(function () {
190 return db.find({
191 selector: {foo: {$gte: 'a'}}
192 });
193 }).then(function (res) {
194 res.docs.forEach(function (doc) {
195 should.exist(doc._rev);
196 delete doc._rev;
197 });
198 res.should.deep.equal({
199 "docs": [
200 {
201 "_id": "a1",
202 "foo": "a",
203 "bar": "1"
204 },
205 {
206 "_id": "a2",
207 "foo": "a",
208 "bar": "2"
209 },
210 {
211 "_id": "b1",
212 "foo": "b",
213 "bar": "1"
214 }
215 ]
216 });
217 });
218 });
219
220 it('supported mixed sort 2', function () {
221 var db = context.db;
222 var index = {
223 "index": {
224 "fields": [
225 "foo",
226 "bar"
227 ]
228 },
229 "name": "foo-index",
230 "type": "json"
231 };
232 return db.createIndex(index).then(function () {
233 return db.bulkDocs([
234 {_id: 'a1', foo: 'a', bar: '1'},
235 {_id: 'a2', foo: 'a', bar: '2'},
236 {_id: 'b1', foo: 'b', bar: '1'}
237 ]);
238 }).then(function () {
239 return db.find({
240 selector: {foo: {$gte: 'b'}}
241 });
242 }).then(function (res) {
243 res.docs.forEach(function (doc) {
244 should.exist(doc._rev);
245 delete doc._rev;
246 });
247 res.should.deep.equal({
248 "docs": [
249 {
250 "_id": "b1",
251 "foo": "b",
252 "bar": "1"
253 }
254 ]
255 });
256 });
257 });
258
259 it('sort error, not an array', function () {
260 var db = context.db;
261
262 return db.createIndex({
263 index: {
264 fields: ['foo']
265 }
266 }).then(function () {
267 return db.bulkDocs([
268 {_id: '1', foo: 1},
269 {_id: '2', foo: 2},
270 {_id: '3', foo: 3},
271 {_id: '4', foo: 4}
272 ]);
273 }).then(function () {
274 return db.find({
275 selector: {foo: {$eq: 1}},
276 sort: {}
277 }).then(function () {
278 throw new Error('expected an error');
279 }, function (err) {
280 should.exist(err);
281 });
282 });
283 });
284
285 });
286};