UNPKG

3.18 kBJavaScriptView Raw
1// Dependencies
2var nock = require('nock'),
3 solr = require('./../main'),
4 vows = require('vows'),
5 assert = require('assert'),
6 fs = require('fs'),
7 mocks = require('./mocks');
8
9// Load configuration file
10var config = JSON.parse(fs.readFileSync(__dirname + '/config.json'));
11
12if(config.mocked){
13 //nock.recorder.rec();
14 mocks.deleteByRange(nock);
15}
16
17// Suite Test
18
19/**
20 * Check that the deleteByRange command works with range of date
21 *
22 * Description of test:
23 * - Add 20 documents to the solr database with `last_update_dt` = new Date() - 2 days for 10 documents
24 * and for last 10 documents `last_update_dt` = new Date(). All documents have the field `title_t` === 'test'
25 * - Delete all document between yesterday and now
26 * - Commit deletion
27 * - Query to find how many documents are in the database with `title_t` === 'test'
28 */
29
30
31var suite = vows.describe('Solr Client API: deleteByRange command');
32
33// Dates
34var today = new Date('2012-05-07T21:50:08.309Z');
35var yesterday = new Date('2012-05-06T21:50:08.309Z');
36var beforeYesterday = new Date('2012-05-05T21:50:08.309Z');
37
38suite.addBatch({
39 'Add 20 documents' : {
40 'with fields last_update_dt and title_t' : {
41 topic : function(){
42 var client = solr.createClient();
43 client.autoCommit = true;
44 var date = today;
45 var docs = new Array(20);
46 for( var i = 0; i < 20 ; i++){
47 if( i >= 10){
48 date = beforeYesterday;
49 }
50 docs[i] = {
51 id : i,
52 title_t : 'test',
53 last_update_dt : date
54 }
55 }
56 client.add(docs,this.callback);
57 },
58 'should works' : function(err,res){
59 assertCorrectResponse(err,res);
60 }
61 }
62 }
63}).addBatch({
64 'Delete documents' : {
65 'between yesterday and today' : {
66 topic : function(){
67 var client = solr.createClient();
68 client.deleteByRange('last_update_dt',yesterday,today,this.callback);
69 },
70 'should works' : function(err,res){
71 assertCorrectResponse(err,res);
72 }
73 }
74 }
75}).addBatch({
76 'Commit change' : {
77 'to see effect of the deleteByRange command' : {
78 topic : function(){
79 var client = solr.createClient();
80 client.commit(this.callback);
81 },
82 'should works' : function(err,res){
83 assertCorrectResponse(err,res);
84 }
85 }
86 }
87}).addBatch({
88 'Query all documents' : {
89 'where title_t === `test`' : {
90 topic : function(){
91 var client = solr.createClient();
92 var query = client.createQuery().q({title_t : 'test'}).start(0).rows(10);
93 client.search(query,this.callback);
94 },
95 'should find 10 documents' : function(err,data){
96 assert.isNull(err);
97 assert.equal(data.response.numFound,10);
98 }
99 }
100 }
101}).export(module);
102
103// Macro
104
105function assertCorrectResponse(err,data){
106 assert.isNull(err);
107 assert.isObject(data);
108 assert.equal(data.responseHeader.status,0);
109}