Skip to content

Commit 562479d

Browse files
committed
Merge branch 'develop'
2 parents 0217ba0 + f23098a commit 562479d

File tree

5 files changed

+383
-17
lines changed

5 files changed

+383
-17
lines changed

scripts/test-node

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ set -e # exit when error
1313
[ -z $TRAVIS_PULL_REQUEST ] && TRAVIS_PULL_REQUEST='false'
1414
[ -z $TRAVIS_BUILD_NUMBER ] && TRAVIS_BUILD_NUMBER='false'
1515

16+
# nvm has a special setup on Travis where it might break installing other versions
17+
unset npm_config_prefix
1618
nvm install 7
1719
node test/run-node.js | tap-bail | tap-dot
1820

src/Index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,26 @@ Index.prototype.clearIndex = function(callback) {
604604
/*
605605
* Get settings of this index
606606
*
607+
* @param opts an object of options to add
608+
* @param opts.advanced get more settings like nbShards (useful for Enterprise)
607609
* @param callback (optional) the result callback called with two arguments
608610
* error: null or Error('message')
609611
* content: the settings object or the error message if a failure occured
610612
*/
611-
Index.prototype.getSettings = function(callback) {
612-
var indexObj = this;
613+
Index.prototype.getSettings = function(opts, callback) {
614+
if (arguments.length === 1 || typeof opts === 'function') {
615+
callback = opts;
616+
opts = {};
617+
}
618+
619+
var indexName = encodeURIComponent(this.indexName);
613620
return this.as._jsonRequest({
614621
method: 'GET',
615-
url: '/1/indexes/' + encodeURIComponent(indexObj.indexName) + '/settings?getVersion=2',
622+
url:
623+
'/1/indexes/' +
624+
indexName +
625+
'/settings?getVersion=2' +
626+
(opts.advanced ? '&advanced=' + opts.advanced : ''),
616627
hostType: 'read',
617628
callback: callback
618629
});
@@ -660,6 +671,7 @@ function exportData(method, _hitsPerPage, callback) {
660671
return search().then(function(data) {
661672
if (typeof callback === 'function') {
662673
callback(data);
674+
return undefined;
663675
}
664676
return data;
665677
});
@@ -671,7 +683,7 @@ function exportData(method, _hitsPerPage, callback) {
671683
* @param [function] callback will be called after all synonyms are retrieved
672684
*/
673685
Index.prototype.exportSynonyms = function(hitsPerPage, callback) {
674-
return exportData(this.searchSynonyms, hitsPerPage, callback);
686+
return exportData(this.searchSynonyms.bind(this), hitsPerPage, callback);
675687
};
676688

677689
Index.prototype.saveSynonym = function(synonym, opts, callback) {
@@ -788,7 +800,7 @@ Index.prototype.searchRules = function(params, callback) {
788800
* @param [function] callback will be called after all query rules are retrieved
789801
*/
790802
Index.prototype.exportRules = function(hitsPerPage, callback) {
791-
return exportData(this.searchRules, hitsPerPage, callback);
803+
return exportData(this.searchRules.bind(this), hitsPerPage, callback);
792804
};
793805

794806
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)