UNPKG

12.9 kBJavaScriptView Raw
1// Dependencies
2var nock = require('nock'),
3 solr = require('./../main'),
4 vows = require('vows'),
5 assert = require('assert'),
6 SolrError = require('./../lib/error/solr-error'),
7 mocks = require('./mocks')
8 fs = require('fs');
9
10// Load configuration file
11var config = JSON.parse(fs.readFileSync(__dirname + '/config.json'));
12
13if(config.mocked){
14 mocks.core(nock);
15}
16//nock.recorder.rec();
17
18// Suite Test
19
20var suite = vows.describe('Solr Client API Core');
21
22suite.addBatch({
23 'The creation of a Solr Client' : {
24 'with no options' : {
25 topic : function() {
26 var client = solr.createClient();
27 return client;
28 },
29 'should return a `Client`' : function(client){
30 assertClient(client);
31 }
32 },
33 'with custom options' : {
34 topic : function(){
35 var client = solr.createClient({
36 host: 'localhost',
37 port: 8983,
38 core: '',
39 path: '/solr'
40 });
41 return client;
42 },
43 'should return a `Client`' : function(client){
44 assertClient(client);
45 assert.equal(client.options.host,'localhost');
46 assert.equal(client.options.port,8983);
47 assert.equal(client.options.core,'');
48 assert.equal(client.options.path,'/solr');
49 }
50 },
51 'with custom host' : {
52 topic : function(){
53 var host = 'localhost';
54 var client = solr.createClient(host);
55 return client
56 },
57 'should return a `Client`' : function(client){
58 assertClient(client);
59 assert.equal(client.options.host,'localhost');
60 }
61 },
62 'with custom host, post, core and path' : {
63 topic : function(){
64 var host = 'localhost';
65 var port = 8983;
66 var core = '';
67 var path = '/solr';
68 var client = solr.createClient(host,port,core,path);
69 return client
70 },
71 'should return a `Client`' : function(client){
72 assertClient(client);
73 }
74 }
75 }
76}).addBatch({
77 'Adding' : {
78 topic : function(){
79 var client = solr.createClient();
80 return client;
81 },
82 'one document to the Solr DB': {
83 topic : function(client){
84 var doc = {
85 id : 1234567890,
86 title_t : 'Test title',
87 description_t : 'Test Description'
88 };
89 client.add(doc,this.callback);
90 },
91 'should be possible.' : function(err,res){
92 assertCorrectResponse(err,res);
93 }
94 },
95 'one document using unknown fields' : {
96 topic : function(client){
97 var doc = {
98 id : 1234567810,
99 unknownfield1 : 'Test title',
100 };
101 client.add(doc,this.callback);
102 },
103 'should return an SolrError' : function(err,res){
104 assertSolrError(err,res);
105 }
106 },
107 'a list of documents' : {
108 topic : function(client){
109 client.updateEach = 1; // default value when a client is created
110 var docs = [
111 { id : 1 , title_t : 'Hello'},
112 { id : 3 , title_t : 'Hola'},
113 { id : 5 , title_t : 'Bonjour'}
114 ];
115 client.add(docs,this.callback);
116 },
117 'should be possible' : function(err,res){
118 assertCorrectResponse(err,res);
119 }
120 }
121 }
122}).addBatch({
123 'Committing' : {
124 topic : function(){
125 var client = solr.createClient();
126 return client;
127 },
128 'manually' : {
129 topic : function(client){
130 client.autoCommit = false;
131 client.commit(this.callback);
132 },
133 'should be possible' : function(err,res){
134 assertCorrectResponse(err,res);
135 }
136 },
137 'automatically' : {
138 topic : function(client){
139 client.autoCommit = true;
140 return client;
141 },
142 'should be possible' : function(client){
143 assert.equal(client.autoCommit,true);
144 }
145 }
146 }
147}).addBatch({
148 'Deleting' : {
149 topic : function(){
150 var client = solr.createClient();
151 return client;
152 },
153 'a document by ID' : {
154 topic : function(client){
155 client.deleteByID(1234567890,this.callback);
156 },
157 'should be possible' : function(err,res){
158 assertCorrectResponse(err,res);
159 }
160 },
161 'a document where a field matchs a value' : {
162 topic : function(client){
163 client.delete('title_t' , 'Test title', this.callback);
164 },
165 'should be possible' : function(err,res){
166 assertCorrectResponse(err,res);
167 }
168 },
169 'documents matching a query' : {
170 topic : function(client){
171 var query = 'title_t:Test title';
172 client.deleteByQuery(query,this.callback);
173 },
174 'should be possible' : function(err,res){
175 assertCorrectResponse(err,res);
176 }
177 },
178 'a range of documents' : {
179 topic : function(client){
180 var start = new Date('2012-05-01T21:50:08.309Z');
181 var stop = new Date('2012-05-02T21:50:08.310Z');
182 client.deleteByRange('last_update',start,stop,this.callback);
183 },
184 'should be possible' : function(err,res){
185 assertCorrectResponse(err,res);
186 }
187 }
188 }
189}).addBatch({
190 'Optimizing' : {
191 topic : function(){
192 var client = solr.createClient();
193 return client;
194 },
195 'the Solr Database' : {
196 topic : function(client){
197 var options = {
198 waitFlush: true ,
199 waitSearcher: true
200 };
201 client.optimize(options,this.callback);
202 },
203 'should be possible' : function(err,res){
204 assertCorrectResponse(err,res);
205 }
206 }
207 }
208}).addBatch({
209 'Updating' : {
210 topic : function(){
211 var client = solr.createClient();
212 return client;
213 },
214 'the Solr Database with any object' : {
215 topic : function(client){
216 var data = { rollback : {} };
217 client.update(data,this.callback);
218 },
219 'should be possible' : function(err,res){
220 assertCorrectResponse(err,res);
221 }
222 }
223 }
224}).addBatch({
225 'Rolling back' : {
226 topic : function(){
227 var client = solr.createClient();
228 return client;
229 },
230 'all documents added or deleted to the index since the last commit' : {
231 topic : function(client){
232 client.rollback(this.callback);
233 },
234 'should be possible' : function(err,res){
235 assertCorrectResponse(err,res);
236 }
237 }
238 }
239}).addBatch({
240 'Ping the Solr server' : {
241 topic : function(){
242 var client = solr.createClient();
243 client.ping(this.callback);
244 },
245 'should be possible' : function(err,res){
246 assertCorrectResponse(err,res);
247 }
248 }
249}).addBatch({
250 'Make a query' : {
251 topic : function(){
252 var client = solr.createClient();
253 return client;
254 },
255 'using unknown fields' : {
256 topic : function(client){
257 var query = client.createQuery().q({titl : 'laptop'}).start(0).rows(10);
258 client.search(query,this.callback);
259 },
260 'should return an SolrError' : function(err,res){
261 assertSolrError(err,res);
262 }
263 },
264 'that will be handle by DisMaxParserPlugin' : {
265 topic : function(client){
266 var query = client.createQuery().q('laptop').dismax().qf({title : 0.2 , description : 3.3}).mm(2).start(0).rows(10);
267 client.search(query,this.callback);
268 },
269 'should be possible' : function(err,res){
270 assertCorrectResponse(err,res);
271 }
272 },
273 'that will be handle by DefaultRequestHandler' : {
274 topic : function(client){
275 var query = client.createQuery().q({title : 'laptop'}).start(0).rows(10);
276 client.search(query,this.callback);
277 },
278 'should be possible' : function(err,res) {
279 assertCorrectResponse(err,res);
280 }
281 },
282 'that will be handle by the RequestHandler given in parameter' : {
283 topic : function(client){
284 var query = client.createQuery().q({title : 'laptop'}).requestHandler('custom').start(0).rows(10);
285 client.search(query,this.callback);
286 },
287 'should be possible' : function(err,res){
288 assertCorrectResponse(err,res);
289 }
290 },
291 'that return a sorted result in ascending or descending order' : {
292 topic : function(client){
293 var query = client.createQuery().q('laptop').dismax().qf({title : 2 , description : 3}).start(0).rows(10).sort({score: 'desc',price: 'asc'});
294 client.search(query,this.callback);
295 },
296 'should be possible': function(err,res){
297 assertCorrectResponse(err,res);
298 }
299 },
300 'that return a result where one or more fields are on a range of values' : {
301 topic : function(client){
302 var query = client.createQuery().q('laptop').dismax().qf({title : 2 , description : 3}).start(0).rows(10).rangeFilter([{field: 'price', start : '10',end : '100' },{field: 'delievery_t', start : '10',end : '100' } ]);
303 client.search(query,this.callback);
304 },
305 'should be possible' : function(err,res){
306 assertCorrectResponse(err,res);
307 }
308 },
309 'that return a result where one or more fields match a particular value' : {
310 topic : function(client){
311 var query = client.createQuery().q('laptop').dismax().qf({title : 2 , description : 3}).start(0).rows(10).matchFilter('category','Electronics');
312 client.search(query,this.callback);
313 },
314 'should be possible' : function(err,res){
315 assertCorrectResponse(err,res);
316 }
317 },
318 'that return only a set of fields specified' : {
319 topic : function(client){
320 var query = client.createQuery().q('laptop').dismax().qf({title : 2 , description : 3}).start(0).rows(10).restrict(['title','description']);
321 client.search(query,this.callback);
322 },
323 'should be possible' : function(err,res){
324 assertCorrectResponse(err,res);
325 }
326 },
327 'that return a result within an expected timeout' : {
328 topic : function(client){
329 var query = client.createQuery().q('laptop').dismax().qf({title : 2 , description : 3}).start(0).rows(10).timeout(1000);
330 client.search(query,this.callback);
331 },
332 'should be possible' : function(err,res){
333 assertCorrectResponse(err,res);
334 }
335 },
336 'that autoconvert JS Date Object into a properly date format expected by Solr' : {
337 topic : function(client){
338 var start = new Date('2012-05-05T21:50:08.783Z');
339 var stop = new Date('2012-05-06T21:50:08.783Z');
340 var query = client.createQuery().q('laptop').dismax().qf({title : 2 , description : 3}).start(0).rows(10).rangeFilter([{field: 'last_update', start : start,end : stop },{field: 'price', start : '10',end : '100' } ]);
341 client.search(query,this.callback);
342 },
343 'should be possible' : function(err,res){
344 assertCorrectResponse(err,res);
345 }
346 },
347 'that use custom parameter in the query thanks to the set method' : {
348 topic : function(client){
349 var query = client.createQuery();
350 query.q('laptop')
351 .dismax()
352 .set('fl=description,score')
353 .set(encodeURIComponent('fq={!q.op=OR%20df=merchant_id_t}837338%208373873%2038738'));
354 client.search(query,this.callback);
355 },
356 'should be possible' : function(err,res){
357 assertCorrectResponse(err,res);
358 }
359 }
360 }
361}).export(module);
362
363// Macros
364
365function assertClient(client){
366 assert.isFunction(client.add);
367 assert.isFunction(client.commit);
368 assert.isFunction(client.delete);
369 assert.isFunction(client.deleteByID);
370 assert.isFunction(client.optimize);
371 assert.isFunction(client.update);
372 assert.isFunction(client.search);
373}
374
375function assertCorrectResponse(err,data){
376 assert.isNull(err);
377 assert.isObject(data);
378 assert.equal(data.responseHeader.status,0);
379}
380
381function assertSolrError(err,res){
382 assert.instanceOf(err,SolrError);
383 assert.equal(err.name,'SolrError');
384 assert.match(err.message,/^HTTP status [0-9]{3}\.Reason:[\s\S]+/)
385 assert.isNull(res);
386}