Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,61 +306,6 @@ Collection.prototype.fetchDocument = function (documentId, options, cb) {
});
};

/**
* Retrieves all documents stored in this data collection
*
* @param {object} [options] - Optional parameters
* @param {responseCallback} cb - Handles the query response
*/
Collection.prototype.fetchAllDocuments = function (options, cb) {
var
warnEmitted = false,
documents = [],
filters = {};

if (!cb && typeof options === 'function') {
cb = options;
options = {};
}

// copying pagination options to the search filter
if (!options) {
options = {};
}

if (!options.from) {
options.from = 0;
}

if (!options.size) {
options.size = 1000;
}

this.kuzzle.callbackRequired('Collection.fetchAllDocuments', cb);

this.search(filters, options, function fetchNextDocuments (error, searchResult) {
if (error) {
return cb(error);
}

if (searchResult instanceof KuzzleSearchResult) {
if (searchResult.total > 10000 && !warnEmitted) {
warnEmitted = true;
console.warn('Collection.fetchAllDocuments may return extremely large amounts of documents, which may cause performance issues. Unless you know what you are doing, consider using Collection.search or Collection.scroll instead'); // eslint-disable-line no-console
}

searchResult.documents.forEach(function(document) {
documents.push(document);
});
searchResult.fetchNext(fetchNextDocuments);
}
else {
cb(null, documents);
}
});
};


/**
* Instantiates a CollectionMapping object containing the current mapping of this collection.
*
Expand Down
1 change: 0 additions & 1 deletion test/Collection/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('Collection constructor', function () {
should.exist(collection.deleteDocumentPromise);
should.exist(collection.documentExistsPromise);
should.exist(collection.fetchDocumentPromise);
should.exist(collection.fetchAllDocumentsPromise);
should.exist(collection.getMappingPromise);
should.exist(collection.mCreateDocumentPromise);
should.exist(collection.mCreateOrReplaceDocumentPromise);
Expand Down
65 changes: 0 additions & 65 deletions test/Collection/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,71 +594,6 @@ describe('Collection methods', function () {
});
});

describe('#fetchAllDocuments', function () {
beforeEach(function () {
collection.search = sinon.stub();
expectedQuery = {
index: 'bar',
collection: 'foo',
action: 'get',
controller: 'document'
};
});

it('should forward the query to the search method', function () {
var options = { queuable: false };

collection.fetchAllDocuments(options, sinon.stub());
should(collection.search).be.calledOnce();
should(collection.search).be.calledWith({}, options, sinon.match.func);
});

it('should raise an error if no callback is provided', function () {
should(function () { collection.fetchAllDocuments(); }).throw(Error);
should(function () { collection.fetchAllDocuments({}); }).throw(Error);
should(collection.search).not.be.called();
});

it('should handle the callback argument correctly', function () {
var
cb1 = sinon.stub(),
cb2 = sinon.stub();

collection.fetchAllDocuments(cb1);
collection.fetchAllDocuments({}, cb2);

should(collection.search).be.calledTwice();

collection.search.yield(null, 'foobar');
should(cb1).be.calledOnce();
should(cb2).be.calledOnce();
});

it('should handle the from and size options', function () {
collection.fetchAllDocuments({from: 123, size: 456}, sinon.stub());
should(collection.search).be.calledOnce();
should(collection.search).be.calledWith({}, {from: 123, size: 456}, sinon.match.func);
});

it('should handle the scroll options', function () {
collection.fetchAllDocuments({scroll: '30s'}, function () {});
should(collection.search).be.calledOnce();
should(collection.search).be.calledWith({}, {from: 0, size: 1000, scroll: '30s'}, sinon.match.func);
});

it('should transfer error if any', function (done) {
this.timeout(50);

collection.fetchAllDocuments(function (er) {
should(er).be.an.instanceOf(Error);
should(er.message).be.exactly('foobar');
done();
});

collection.search.yield(new Error('foobar'));
});
});

describe('#getMapping', function () {
beforeEach(function () {
result = { result: {'bar': { mappings: { foo: { properties: {}}}} }};
Expand Down
71 changes: 41 additions & 30 deletions test/kuzzle/offlineQueue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ var

describe('Offline queue management', function () {
var
clock,
now,
kuzzle;
kuzzle,
reset;

beforeEach(function () {
var pastTime = 60050;
Expand All @@ -16,12 +18,26 @@ describe('Offline queue management', function () {

// queuing a bunch of 7 requests from 1min ago to right now, 10s apart
now = Date.now();
clock = sinon.useFakeTimers(now);

reset = Kuzzle.__set__({
setTimeout: clock.setTimeout,
setInterval: clock.setInterval,
clearTimeout: clock.clearTimeout,
clearInterval: clock.clearInterval
});

while (pastTime >= 0) {
kuzzle.offlineQueue.push({ts: now - pastTime, query: {requestId: pastTime, action: 'foo', controller: 'bar'}, cb: function () {}});
pastTime -= 10000;
}
});

afterEach(function () {
clock.restore();
reset();
});

describe('#cleanQueue', function () {
var
cleanQueue = Kuzzle.__get__('cleanQueue');
Expand Down Expand Up @@ -92,31 +108,28 @@ describe('Offline queue management', function () {
emitRequestStub.reset();
});

it('should play all queued requests', function (done) {
it('should play all queued requests', function () {
var
numRequests = kuzzle.offlineQueue.length,
eventStub = sinon.stub();

this.timeout(200);
kuzzle.addListener('offlineQueuePop', eventStub);
dequeue.call(kuzzle);

setTimeout(function () {
should(emitRequestStub.callCount).be.exactly(numRequests);
should(kuzzle.offlineQueue).be.an.Array();
should(kuzzle.offlineQueue.length).be.exactly(0);
should(kuzzle.queuing).be.false();
should(eventStub.callCount).be.exactly(numRequests);
done();
}, numRequests * kuzzle.replayInterval + 50);
clock.tick(numRequests * kuzzle.replayInterval + 50);

should(emitRequestStub.callCount).be.exactly(numRequests);
should(kuzzle.offlineQueue).be.an.Array();
should(kuzzle.offlineQueue.length).be.exactly(0);
should(kuzzle.queuing).be.false();
should(eventStub.callCount).be.exactly(numRequests);
});

it('should also load the queue provided by the offlineQueueLoader property', function (done) {
it('should also load the queue provided by the offlineQueueLoader property', function () {
var
numRequests = kuzzle.offlineQueue.length,
eventStub = sinon.stub();

this.timeout(200);
kuzzle.offlineQueueLoader = function () {
return [
{query: {requestId: 'foo', action: 'action', controller: 'controller'}},
Expand All @@ -126,17 +139,16 @@ describe('Offline queue management', function () {
kuzzle.addListener('offlineQueuePop', eventStub);
dequeue.call(kuzzle);

setTimeout(function () {
should(emitRequestStub.callCount).be.exactly(numRequests + 2);
should(kuzzle.offlineQueue).be.an.Array();
should(kuzzle.offlineQueue.length).be.exactly(0);
should(kuzzle.queuing).be.false();
should(eventStub.callCount).be.exactly(numRequests + 2);
done();
}, (numRequests +2) * kuzzle.replayInterval + 50);
clock.tick((numRequests + 2) * kuzzle.replayInterval + 50);

should(emitRequestStub.callCount).be.exactly(numRequests + 2);
should(kuzzle.offlineQueue).be.an.Array();
should(kuzzle.offlineQueue.length).be.exactly(0);
should(kuzzle.queuing).be.false();
should(eventStub.callCount).be.exactly(numRequests + 2);
});

it('should filter duplicates from the offlineQueueLoader and the cached queue ', function (done) {
it('should filter duplicates from the offlineQueueLoader and the cached queue ', function () {
var
numRequests = kuzzle.offlineQueue.length,
eventStub = sinon.stub();
Expand All @@ -151,14 +163,13 @@ describe('Offline queue management', function () {
kuzzle.addListener('offlineQueuePop', eventStub);
dequeue.call(kuzzle);

setTimeout(function () {
should(emitRequestStub.callCount).be.exactly(numRequests + 1);
should(kuzzle.offlineQueue).be.an.Array();
should(kuzzle.offlineQueue.length).be.exactly(0);
should(kuzzle.queuing).be.false();
should(eventStub.callCount).be.exactly(numRequests + 1);
done();
}, (numRequests + 1) * kuzzle.replayInterval + 50);
clock.tick((numRequests + 1) * kuzzle.replayInterval + 50);

should(emitRequestStub.callCount).be.exactly(numRequests + 1);
should(kuzzle.offlineQueue).be.an.Array();
should(kuzzle.offlineQueue.length).be.exactly(0);
should(kuzzle.queuing).be.false();
should(eventStub.callCount).be.exactly(numRequests + 1);
});

it('should throw on erroneous queries returned by the offlineQueueLoader property', function () {
Expand Down