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
4 changes: 1 addition & 3 deletions dist/kuzzle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/kuzzle.js.map

Large diffs are not rendered by default.

8,666 changes: 6,187 additions & 2,479 deletions package-lock.json

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@
],
"scripts": {
"test": "npm run --silent lint && npm run unit-testing",
"unit-testing": "istanbul cover _mocha -- --recursive",
"unit-testing": "nyc --reporter=text-summary --reporter=lcov mocha",
"lint": "eslint --max-warnings=0 ./src ./test",
"build": "node build.js"
},
"browser": "src/Kuzzle.js",
"main": "index.js",
"license": "Apache-2.0",
"dependencies": {
"bluebird": "3.5.0",
"eslint-loader": "^1.8.0",
"uuid": "3.1.0",
"ws": "3.3.1"
"bluebird": "3.5.1",
"nyc": "^12.0.2",
"uuid": "3.3.2",
"ws": "6.0.0"
},
"peerDependencies": {
"bufferutil": "^2.0.1",
"utf-8-validate": "^3.0.1"
},
"devDependencies": {
"codecov": "^2.2.0",
"eslint": "^4.7.0",
"eslint-friendly-formatter": "^3.0.0",
"istanbul": "0.4.5",
"codecov": "^3.0.4",
"eslint": "^5.2.0",
"eslint-friendly-formatter": "^4.0.1",
"eslint-loader": "^2.1.0",
"istanbul-middleware": "0.2.2",
"mocha": "3.4.2",
"ora": "^1.3.0",
"proxyquire": "^1.8.0",
"rewire": "^2.5.2",
"should": "11.2.1",
"should-sinon": "0.0.5",
"sinon": "^2.3.5",
"webpack": "^1.13.1"
"mocha": "5.2.0",
"ora": "^3.0.0",
"proxyquire": "^2.0.1",
"rewire": "^4.0.1",
"should": "13.2.1",
"should-sinon": "0.0.6",
"sinon": "^6.1.4",
"webpack": "^4.16.2"
},
"engines": {
"node": ">= 6.9.1"
Expand Down
39 changes: 34 additions & 5 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,49 @@ Collection.prototype.count = function (filters, options, cb) {
* Kuzzle automatically creates data collections when storing documents, but there are cases where we
* want to create and prepare data collections before storing documents in it.
*
* @param {object} [mappings] - Optional collection mappings
* @param {object} [options] - Optional parameters
* @param {responseCallback} [cb] - returns Kuzzle's response
* @returns {*} this
*/
Collection.prototype.create = function (options, cb) {
var data = {},
Collection.prototype.create = function () {
var
i,
data = {},
mapping = null,
options = null,
cb = null,
self = this;

if (!cb && typeof options === 'function') {
cb = options;
options = null;
for (i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'function') {
if (i < arguments.length - 1) {
throw new Error('Invalid argument: ' + arguments[i+1]);
}

cb = arguments[i];
} else if (typeof arguments[i] === 'object' && !Array.isArray(arguments[i])) {
if (mapping === null) {
mapping = arguments[i];
} else if (options === null) {
options = arguments[i];
} else {
throw new Error('Too many objects arguments');
}
} else {
throw new Error('Invalid argument: ' + arguments[i]);
}
}

if (mapping === null || (options === null && typeof mapping.queuable === 'boolean')) {
options = mapping;
data = {};
} else {
data = {body: mapping};
}

data = this.kuzzle.addHeaders(data, this.headers);

this.kuzzle.query(this.buildQueryArgs('collection', 'create'), data, options, function(err) {
cb(err, err ? undefined : self);
});
Expand Down
68 changes: 59 additions & 9 deletions test/Collection/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,75 @@ describe('Collection methods', function () {
it('should handle the callback argument correctly', function () {
var
cb1 = sinon.stub(),
cb2 = sinon.stub();
cb2 = sinon.stub(),
cb3 = sinon.stub(),
cb4 = sinon.stub();

collection.create(cb1);
collection.create({}, cb2);
should(kuzzle.query).be.calledTwice();
should(collection.create(cb1)).eql(collection);
should(collection.create({mapping: 'gnippam'}, cb2)).eql(collection);
should(collection.create({mapping: 'foobar'}, {queuable: true}, cb3)).eql(collection);
should(collection.create({queuable: true}, cb4)).eql(collection);
should(kuzzle.query.callCount).eql(4);

kuzzle.query.yield(null, result);
should(cb1).be.calledOnce();
should(cb2).be.calledOnce();
should(cb1).calledOnce();
should(cb2).calledOnce();
should(cb3).calledOnce();
should(cb4).calledOnce();

should(kuzzle.query).calledWithMatch({
controller: 'collection',
action: 'create',
collection: 'foo',
index: 'bar'
}, {}, null, sinon.match.func);

should(kuzzle.query).calledWithMatch({
controller: 'collection',
action: 'create',
collection: 'foo',
index: 'bar'
}, {body: {mapping: 'gnippam'}}, null, sinon.match.func);

should(kuzzle.query).calledWithMatch({
controller: 'collection',
action: 'create',
collection: 'foo',
index: 'bar'
}, {body: {mapping: 'foobar'}}, {queuable: true}, sinon.match.func);

should(kuzzle.query).calledWithMatch({
controller: 'collection',
action: 'create',
collection: 'foo',
index: 'bar'
}, {}, {queuable: true}, sinon.match.func);
});

it('should throw if invalid arguments are provided', function () {
should(function () { collection.create(function() {}, 'foobar'); })
.throw(Error, {message: 'Invalid argument: foobar'});

should(function () { collection.create('foobar', function() {}); })
.throw(Error, {message: 'Invalid argument: foobar'});

should(function () { collection.create({}, {}, {}, function () {}); })
.throw(Error, {message: 'Too many objects arguments'});
});

it('should call the callback with an error if one occurs', function (done) {
this.timeout(50);

collection.create(function (err, res) {
should(err).be.exactly('foobar');
should(res).be.undefined();
done();
try {
should(err).be.exactly('foobar');
should(res).be.undefined();
done();
} catch (e) {
done(e);
}
});

kuzzle.query.yield('foobar');
});
});
Expand Down
4 changes: 4 additions & 0 deletions test/Room/methods.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe('Room methods', function () {
collection = kuzzle.collection('foo', 'bar');
});

afterEach(function () {
kuzzle.disconnect();
});

describe('#count', function () {
var room;

Expand Down
2 changes: 1 addition & 1 deletion test/SearchResult/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('SearchResult constructor', function () {
collection;

beforeEach(function () {
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar'});
kuzzle = new Kuzzle('foo', {defaultIndex: 'bar', connect: 'manual'});
searchOptions = {from:0, size: 1};
searchFilters = {};
collection = kuzzle.collection('foo');
Expand Down
21 changes: 13 additions & 8 deletions test/kuzzle/connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,24 @@ describe('Kuzzle connect', function () {
});
});

it('should invalidate the instance if autoReconnect is set to false', function () {
it('should invalidate the instance if autoReconnect is set to false', function (done) {
var kuzzle = new Kuzzle('somewhere', {connect: 'manual', autoReconnect: false}, function() {
kuzzle.network.disconnect();
});

kuzzle.connect();
process.nextTick(function() {
should(kuzzle.state).be.exactly('disconnected');
should(kuzzle.queuing).be.false();
should(function () {
kuzzle.isValid();
}).throw();
done();
try {
should(kuzzle.state).be.exactly('disconnected');
should(kuzzle.queuing).be.false();
should(function () {
kuzzle.isValid();
}).throw();
done();
}
catch (e) {
done(e);
}
});
});
});
Expand Down Expand Up @@ -304,7 +309,7 @@ describe('Kuzzle connect', function () {
kuzzle = new Kuzzle('somewhereagain', {connect: 'manual'}),
tokenExpiredStub = sinon.stub();

sinon.stub(kuzzle, 'checkToken', function (token, cb) {
sinon.stub(kuzzle, 'checkToken').callsFake(function (token, cb) {
should(token).be.eql(kuzzle.jwtToken);
cb(null, {valid: false});
});
Expand Down
2 changes: 1 addition & 1 deletion test/kuzzle/getStatistics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Kuzzle.getStatistics', function () {
emitted;

beforeEach(function () {
kuzzle = new Kuzzle('foo', 'this is not an index');
kuzzle = new Kuzzle('foo', {connect: 'manual'});
emitted = false;
});

Expand Down
15 changes: 7 additions & 8 deletions test/kuzzle/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var

describe('Kuzzle Login', function () {
var
sandbox = sinon.sandbox.create(),
loginCredentials = {username: 'foo', password: 'bar'},
kuzzle;

Expand All @@ -14,14 +13,14 @@ describe('Kuzzle Login', function () {
});

afterEach(function() {
sandbox.restore();
sinon.restore();
});

describe('# with callback', function () {
var queryStub;

beforeEach(function() {
queryStub = sandbox.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
queryStub = sinon.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
cb(null, {result: {jwt: 'test-toto'}});
});
});
Expand Down Expand Up @@ -79,7 +78,7 @@ describe('Kuzzle Login', function () {
var queryStub;

beforeEach(function() {
queryStub = sandbox.stub(kuzzle, 'query');
queryStub = sinon.stub(kuzzle, 'query');
});

it('should handle login with only the strategy', function () {
Expand Down Expand Up @@ -136,7 +135,7 @@ describe('Kuzzle Login', function () {
});

it('should send a failed loginAttempt event if logging in fails', function (done) {
sandbox.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
sinon.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
cb({message: 'foobar'});
});

Expand All @@ -151,7 +150,7 @@ describe('Kuzzle Login', function () {

it('should not forward an event if there is no JWT token in the response', function (done) {
var loginAttemptStub = sinon.stub();
sandbox.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
sinon.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
cb(null, {result: {}});
});

Expand All @@ -166,7 +165,7 @@ describe('Kuzzle Login', function () {
});

it('should give an error if login query fail to the login callback if is set', function (done) {
sandbox.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
sinon.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
cb(new Error());
});

Expand All @@ -186,7 +185,7 @@ describe('Kuzzle Login', function () {
});

it('should give an error if logout query fail to the logout callback if is set', function (done) {
sandbox.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
sinon.stub(kuzzle, 'query').callsFake(function(queryArgs, query, options, cb) {
cb(new Error());
});

Expand Down
Loading