Skip to content

Commit 06d448e

Browse files
committed
Added test for search.js
1 parent b9e14ca commit 06d448e

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

gulpfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ gulp.task('test', function() {
115115
'master_key.js',
116116
'status.js',
117117
'sms.js',
118+
'search.js'
118119
]))
119120
.pipe(mocha({
120121
timeout: 100000,

lib/search.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
* @return {String} the sort string.
8585
*/
8686
build: function() {
87-
return JSON.stringify(this._sortFields);
87+
return JSON.stringify(AV._encode(this._sortFields));
8888
}
8989
};
9090

@@ -170,12 +170,20 @@
170170
return this;
171171
},
172172

173-
_processResult: function(json){
173+
/**
174+
* Returns the number of objects that match this query.
175+
* @return {Number}
176+
*/
177+
hits: function() {
178+
return this._hits || 0;
179+
},
180+
181+
_processResult: function(json){
174182
delete json['className'];
175183
delete json['_app_url'];
176184
delete json['_deeplink'];
177185
return json;
178-
},
186+
},
179187

180188
/**
181189
* Retrieves a list of AVObjects that satisfy this query.
@@ -215,6 +223,9 @@
215223
toJSON: function(){
216224
var params = AV.SearchQuery.__super__.toJSON.call(this);
217225
delete params.where;
226+
if(this.className) {
227+
params.clazz = this.className;
228+
}
218229
if(this._sid) {
219230
params.sid = this._sid;
220231
}

lib/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@
385385
route !== "cloudQuery" &&
386386
route !== "qiniu" &&
387387
route !== "statuses" &&
388+
route !== 'search/select' &&
388389
route !== 'subscribe/statuses/count' &&
389390
route !== 'subscribe/statuses' &&
390391
!(/users\/[^\/]+\/updatePassword/.test(route)) &&

test/search.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
describe("App Searching", function() {
2+
describe("#AV.SearchSortBuilder", function(){
3+
it("should build sort string.", function(done){
4+
var builder = new AV.SearchSortBuilder();
5+
var s = builder.ascending('a').descending('b', 'max').whereNear(
6+
'location',
7+
new AV.GeoPoint(10,20),
8+
{
9+
order: 'desc'
10+
}
11+
).build();
12+
expect(s).to.be('[{"a":{"order":"asc","mode":"avg","missing":"_last"}},{"b":{"order":"desc","mode":"max","missing":"_last"}},{"_geo_distance":{"order":"desc","mode":"avg","unit":"km","location":{"lat":10,"lon":20}}}]')
13+
done();
14+
});
15+
});
16+
17+
describe('#AV.SearchQuery', function() {
18+
this.timeout(10000);
19+
it('should find something.', function(done) {
20+
var q = new AV.SearchQuery('Thread');
21+
q.queryString('*');
22+
q.find().then(function(results) {
23+
expect(q.hits()).to.be.greaterThan(0);
24+
expect(results[0]).to.be.an(AV.Object);
25+
done();
26+
});
27+
});
28+
29+
it('should sort by tid.', function(done) {
30+
var q = new AV.SearchQuery('Ticket');
31+
q.descending('tid');
32+
q.queryString('*');
33+
q.find().then(function(results) {
34+
expect(q.hits()).to.be.greaterThan(0);
35+
console.dir(results);
36+
expect(results[0].appURL).to.be.ok();
37+
expect(results[0].get('tid')).to.be.greaterThan(results[1].get('tid'));
38+
done();
39+
});
40+
});
41+
42+
it('should sort by tid with sort builder.', function(done) {
43+
var q = new AV.SearchQuery('Ticket');
44+
q.sortBy(new AV.SearchSortBuilder().descending('tid'));
45+
q.queryString('*');
46+
q.find().then(function(results) {
47+
expect(q.hits()).to.be.greaterThan(0);
48+
console.dir(results);
49+
expect(results[0].get('tid')).to.be.greaterThan(results[1].get('tid'));
50+
done();
51+
});
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)