Skip to content

Commit f23098a

Browse files
Haroenvvvo
authored andcommitted
fix(exportXXX): bind the methods (algolia#663)
* fix(exportXXX): bind the methods These two errors were unnoticed, because the integration tests apparently bind everything. fixes algolia#660 * chore: correct fake synonyms * chore: add highlight check * fix: correct fake number of synonyms * fix: don't return the promise if there's a callback
1 parent 58a4b2d commit f23098a

File tree

3 files changed

+337
-3
lines changed

3 files changed

+337
-3
lines changed

src/Index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ Index.prototype.clearIndex = function(callback) {
604604
/*
605605
* Get settings of this index
606606
*
607-
* @param opts an object of options to add
607+
* @param opts an object of options to add
608608
* @param opts.advanced get more settings like nbShards (useful for Enterprise)
609609
* @param callback (optional) the result callback called with two arguments
610610
* error: null or Error('message')
@@ -671,6 +671,7 @@ function exportData(method, _hitsPerPage, callback) {
671671
return search().then(function(data) {
672672
if (typeof callback === 'function') {
673673
callback(data);
674+
return undefined;
674675
}
675676
return data;
676677
});
@@ -682,7 +683,7 @@ function exportData(method, _hitsPerPage, callback) {
682683
* @param [function] callback will be called after all synonyms are retrieved
683684
*/
684685
Index.prototype.exportSynonyms = function(hitsPerPage, callback) {
685-
return exportData(this.searchSynonyms, hitsPerPage, callback);
686+
return exportData(this.searchSynonyms.bind(this), hitsPerPage, callback);
686687
};
687688

688689
Index.prototype.saveSynonym = function(synonym, opts, callback) {
@@ -799,7 +800,7 @@ Index.prototype.searchRules = function(params, callback) {
799800
* @param [function] callback will be called after all query rules are retrieved
800801
*/
801802
Index.prototype.exportRules = function(hitsPerPage, callback) {
802-
return exportData(this.searchRules, hitsPerPage, callback);
803+
return exportData(this.searchRules.bind(this), hitsPerPage, callback);
803804
};
804805

805806
Index.prototype.saveRule = function(rule, opts, callback) {

test/spec/common/index/exportRules.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
var bind = require('lodash-compat/function/bind');
5+
var createFixture = require('../../../utils/create-fixture');
6+
var fauxJax = require('faux-jax');
7+
var parse = require('url-parse');
8+
var arrayFrom = require('array.from');
9+
10+
test('exportRules()', function(t) {
11+
t.plan(6);
12+
var fixture = createFixture();
13+
var index = fixture.index;
14+
fauxJax.install({gzip: true});
15+
fauxJax.once('request', firstBrowse);
16+
17+
index
18+
.exportRules()
19+
.then(function(data) {
20+
t.ok(data, 'got some rules back');
21+
t.equal(data.length, 200, 'got the right amount of rules back');
22+
t.notOk(data[0]._highlightResult, 'removed highlighting');
23+
})
24+
.catch(bind(t.fail, t));
25+
26+
function firstBrowse(req) {
27+
var parsedURL = parse(req.requestURL, true);
28+
29+
t.equal(
30+
parsedURL.pathname,
31+
'/1/indexes/' +
32+
encodeURIComponent(fixture.credentials.indexName) +
33+
'/rules/search',
34+
'pathname matches'
35+
);
36+
t.deepEqual(
37+
JSON.parse(req.requestBody),
38+
{page: 0, hitsPerPage: 100},
39+
'params match'
40+
);
41+
42+
req.respond(
43+
200,
44+
{},
45+
JSON.stringify({
46+
nbHits: 200,
47+
hits: arrayFrom({length: 100}, function(v, num) {
48+
return {
49+
objectID: 'some-qr-rule-' + num,
50+
condition: {pattern: 'hellomyfriendhowareyou??? ' + num, anchoring: 'is'},
51+
consequence: {params: {query: 'query-rule-integration-test'}},
52+
_highlightResult: {}
53+
};
54+
})
55+
})
56+
);
57+
58+
fauxJax.once('request', secondBrowse);
59+
}
60+
61+
function secondBrowse(req) {
62+
t.deepEqual(
63+
JSON.parse(req.requestBody),
64+
{page: 1, hitsPerPage: 100},
65+
'cursor matches'
66+
);
67+
68+
req.respond(
69+
200,
70+
{},
71+
JSON.stringify({
72+
nbHits: 200,
73+
hits: arrayFrom({length: 100}, function(v, num) {
74+
num += 100;
75+
return {
76+
objectID: 'some-qr-rule-' + num,
77+
condition: {pattern: 'hellomyfriendhowareyou??? ' + num, anchoring: 'is'},
78+
consequence: {params: {query: 'query-rule-integration-test'}},
79+
_highlightResult: {}
80+
};
81+
})
82+
})
83+
);
84+
85+
fauxJax.restore();
86+
}
87+
});
88+
89+
test('exportRules(hitsPerPage)', function(t) {
90+
t.plan(6);
91+
var fixture = createFixture();
92+
var index = fixture.index;
93+
fauxJax.install({gzip: true});
94+
fauxJax.once('request', firstBrowse);
95+
96+
index
97+
.exportRules(50)
98+
.then(function(data) {
99+
t.ok(data, 'got some rules back');
100+
t.equal(data.length, 100, 'got the right amount of rules back');
101+
t.notOk(data[0]._highlightResult, 'removed highlighting');
102+
})
103+
.catch(bind(t.fail, t));
104+
105+
function firstBrowse(req) {
106+
var parsedURL = parse(req.requestURL, true);
107+
108+
t.equal(
109+
parsedURL.pathname,
110+
'/1/indexes/' +
111+
encodeURIComponent(fixture.credentials.indexName) +
112+
'/rules/search',
113+
'pathname matches'
114+
);
115+
t.deepEqual(
116+
JSON.parse(req.requestBody),
117+
{page: 0, hitsPerPage: 50},
118+
'params match'
119+
);
120+
121+
req.respond(
122+
200,
123+
{},
124+
JSON.stringify({
125+
nbHits: 100,
126+
hits: arrayFrom({length: 50}, function(v, num) {
127+
return {
128+
objectID: 'some-qr-rule-' + num,
129+
condition: {pattern: 'hellomyfriendhowareyou??? ' + num, anchoring: 'is'},
130+
consequence: {params: {query: 'query-rule-integration-test'}},
131+
_highlightResult: {}
132+
};
133+
})
134+
})
135+
);
136+
137+
fauxJax.once('request', secondBrowse);
138+
}
139+
140+
function secondBrowse(req) {
141+
t.deepEqual(
142+
JSON.parse(req.requestBody),
143+
{page: 1, hitsPerPage: 50},
144+
'cursor matches'
145+
);
146+
147+
req.respond(
148+
200,
149+
{},
150+
JSON.stringify({
151+
nbHits: 100,
152+
hits: arrayFrom({length: 50}, function(v, num) {
153+
num += 100;
154+
return {
155+
objectID: 'some-qr-rule-' + num,
156+
condition: {pattern: 'hellomyfriendhowareyou??? ' + num, anchoring: 'is'},
157+
consequence: {params: {query: 'query-rule-integration-test'}},
158+
_highlightResult: {}
159+
};
160+
})
161+
})
162+
);
163+
164+
fauxJax.restore();
165+
}
166+
});
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
var bind = require('lodash-compat/function/bind');
5+
var createFixture = require('../../../utils/create-fixture');
6+
var fauxJax = require('faux-jax');
7+
var parse = require('url-parse');
8+
var arrayFrom = require('array.from');
9+
10+
test('exportSynonyms()', function(t) {
11+
t.plan(6);
12+
var fixture = createFixture();
13+
var index = fixture.index;
14+
fauxJax.install({gzip: true});
15+
fauxJax.once('request', firstBrowse);
16+
17+
index
18+
.exportSynonyms()
19+
.then(function(data) {
20+
t.ok(data, 'got some synonyms back');
21+
t.equal(data.length, 200, 'got the right amount of synonyms back');
22+
t.notOk(data[0]._highlightResult, 'removed highlighting');
23+
})
24+
.catch(bind(t.fail, t));
25+
26+
function firstBrowse(req) {
27+
var parsedURL = parse(req.requestURL, true);
28+
29+
t.equal(
30+
parsedURL.pathname,
31+
'/1/indexes/' +
32+
encodeURIComponent(fixture.credentials.indexName) +
33+
'/synonyms/search',
34+
'pathname matches'
35+
);
36+
t.deepEqual(
37+
JSON.parse(req.requestBody),
38+
{page: 0, hitsPerPage: 100},
39+
'params match'
40+
);
41+
42+
req.respond(
43+
200,
44+
{},
45+
JSON.stringify({
46+
nbHits: 200,
47+
hits: arrayFrom({length: 100}, function(v, num) {
48+
return {
49+
objectID: 'some-qr-rule-' + num,
50+
condition: {pattern: 'hellomyfriendhowareyou??? ' + num, anchoring: 'is'},
51+
consequence: {params: {query: 'query-rule-integration-test'}},
52+
_highlightResult: {}
53+
};
54+
})
55+
})
56+
);
57+
58+
fauxJax.once('request', secondBrowse);
59+
}
60+
61+
function secondBrowse(req) {
62+
t.deepEqual(
63+
JSON.parse(req.requestBody),
64+
{page: 1, hitsPerPage: 100},
65+
'cursor matches'
66+
);
67+
68+
req.respond(
69+
200,
70+
{},
71+
JSON.stringify({
72+
nbHits: 200,
73+
hits: arrayFrom({length: 100}, function(v, num) {
74+
num += 100;
75+
return {
76+
objectID: 'some-qr-rule-' + num,
77+
condition: {pattern: 'hellomyfriendhowareyou??? ' + num, anchoring: 'is'},
78+
consequence: {params: {query: 'query-rule-integration-test'}},
79+
_highlightResult: {}
80+
};
81+
})
82+
})
83+
);
84+
85+
fauxJax.restore();
86+
}
87+
});
88+
89+
test('exportSynonyms(hitsPerPage)', function(t) {
90+
t.plan(6);
91+
var fixture = createFixture();
92+
var index = fixture.index;
93+
fauxJax.install({gzip: true});
94+
fauxJax.once('request', firstBrowse);
95+
96+
index
97+
.exportSynonyms(50)
98+
.then(function(data) {
99+
t.ok(data, 'got some synonyms back');
100+
t.equal(data.length, 100, 'got the right amount of synonyms back');
101+
t.notOk(data[0]._highlightResult, 'removed highlighting');
102+
})
103+
.catch(bind(t.fail, t));
104+
105+
function firstBrowse(req) {
106+
var parsedURL = parse(req.requestURL, true);
107+
108+
t.equal(
109+
parsedURL.pathname,
110+
'/1/indexes/' +
111+
encodeURIComponent(fixture.credentials.indexName) +
112+
'/synonyms/search',
113+
'pathname matches'
114+
);
115+
t.deepEqual(
116+
JSON.parse(req.requestBody),
117+
{page: 0, hitsPerPage: 50},
118+
'params match'
119+
);
120+
121+
req.respond(
122+
200,
123+
{},
124+
JSON.stringify({
125+
nbHits: 100,
126+
hits: arrayFrom({length: 50}, function(v, num) {
127+
return {
128+
objectID: 'some-synonym-' + num,
129+
type: 'placeholder',
130+
placeholder: '<gotcha' + num + '>',
131+
replacements: ['replacement number ' + num]
132+
};
133+
})
134+
})
135+
);
136+
137+
fauxJax.once('request', secondBrowse);
138+
}
139+
140+
function secondBrowse(req) {
141+
t.deepEqual(
142+
JSON.parse(req.requestBody),
143+
{page: 1, hitsPerPage: 50},
144+
'cursor matches'
145+
);
146+
147+
req.respond(
148+
200,
149+
{},
150+
JSON.stringify({
151+
nbHits: 100,
152+
hits: arrayFrom({length: 50}, function(v, num) {
153+
num += 50;
154+
return {
155+
objectID: 'some-synonym-' + num,
156+
type: 'placeholder',
157+
placeholder: '<gotcha' + num + '>',
158+
replacements: ['replacement number ' + num],
159+
_highlightResult: {}
160+
};
161+
})
162+
})
163+
);
164+
165+
fauxJax.restore();
166+
}
167+
});

0 commit comments

Comments
 (0)