Skip to content

Commit 8f25db9

Browse files
committed
Merge pull request #20 from kuzzleio/unitTests
added dataCollection.putMapping + tests
2 parents 8050c30 + 3713614 commit 8f25db9

File tree

8 files changed

+210
-51
lines changed

8 files changed

+210
-51
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kuzzle-sdk",
3-
"version": "1.0.0-alpha.6",
3+
"version": "1.0.0-alpha.7",
44
"description": "Official Javascript SDK for Kuzzle",
55
"author": "The Kuzzle Team <[email protected]>",
66
"repository": {

src/kuzzle.js

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,8 @@ Kuzzle.prototype.getAllStatistics = function (options, cb) {
456456
* @returns {object} this
457457
*/
458458
Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
459+
var queryCB;
460+
459461
if (!cb) {
460462
if (arguments.length === 1) {
461463
cb = arguments[0];
@@ -473,45 +475,27 @@ Kuzzle.prototype.getStatistics = function (timestamp, options, cb) {
473475
}
474476
}
475477

476-
this.callbackRequired('Kuzzle.getStatistics', cb);
477-
478-
if (!timestamp) {
479-
this.query(null, 'admin', 'getStats', {}, options, function (err, res) {
480-
var frame;
481-
482-
if (err) {
483-
return cb(err);
484-
}
478+
queryCB = function (err, res) {
479+
var stats = [];
485480

486-
frame = res.statistics[Object.keys(res.statistics)[0]];
487-
frame.timestamp = Object.keys(res.statistics)[0];
488-
cb(null, frame);
489-
});
490-
} else {
491-
if (typeof timestamp !== 'number') {
492-
throw new Error('Timestamp number expected, received a ' + typeof timestamp);
481+
if (err) {
482+
return cb(err);
493483
}
494484

495-
this.query(null, 'admin', 'getAllStats', {}, function (err, res) {
496-
var
497-
stats = [],
498-
frames;
485+
Object.keys(res.statistics).forEach(function (frame) {
486+
res.statistics[frame].timestamp = frame;
487+
stats.push(res.statistics[frame]);
488+
});
499489

500-
if (err) {
501-
return cb(err);
502-
}
490+
cb(null, stats);
491+
};
503492

504-
frames = Object.keys(res.statistics).filter(function (element) {
505-
return (new Date(element).getTime()) >= timestamp;
506-
});
507-
508-
frames.forEach(function (frame) {
509-
res.statistics[frame].timestamp = frame;
510-
stats.push(res.statistics[frame]);
511-
});
493+
this.callbackRequired('Kuzzle.getStatistics', cb);
512494

513-
cb(null, stats);
514-
});
495+
if (!timestamp) {
496+
this.query(null, 'admin', 'getLastStats', {}, options, queryCB);
497+
} else {
498+
this.query(null, 'admin', 'getStats', { body: { startTime: timestamp } }, options, queryCB);
515499
}
516500

517501
return this;

src/kuzzleDataCollection.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,29 @@ KuzzleDataCollection.prototype.publish = function (document, options) {
385385
return this;
386386
};
387387

388+
/**
389+
* Update a new mapping to the data collection.
390+
* Note that you cannot delete an existing mapping, you can only add or update one.
391+
*
392+
* @param {object} mapping - mapping to apply
393+
* @param {object} [options] - optional arguments
394+
* @param {responseCallback} [cb] - Returns an instantiated KuzzleDataMapping object
395+
* @returns {*} this
396+
*/
397+
KuzzleDataCollection.prototype.putMapping = function (mapping, options, cb) {
398+
var dataMapping;
399+
400+
if (!cb && typeof options === 'function') {
401+
cb = options;
402+
options = null;
403+
}
404+
405+
dataMapping = new KuzzleDataMapping(this, mapping);
406+
dataMapping.apply(options, cb);
407+
408+
return this;
409+
};
410+
388411
/**
389412
* Replace an existing document with a new one.
390413
*
@@ -549,10 +572,11 @@ KuzzleDataCollection.prototype.roomFactory = function (options) {
549572
* Instantiate a new KuzzleDataMapping object. Workaround to the module.exports limitation, preventing multiple
550573
* constructors to be exposed without having to use a factory or a composed object.
551574
*
575+
* @param {object} [mapping] - mapping to instantiate the KuzzleDataMapping object with
552576
* @constructor
553577
*/
554-
KuzzleDataCollection.prototype.dataMappingFactory = function () {
555-
return new KuzzleDataMapping(this);
578+
KuzzleDataCollection.prototype.dataMappingFactory = function (mapping) {
579+
return new KuzzleDataMapping(this, mapping);
556580
};
557581

558582
/**

src/kuzzleDataMapping.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @param {object} kuzzleDataCollection - Instance of the inherited KuzzleDataCollection object
1919
* @constructor
2020
*/
21-
function KuzzleDataMapping(kuzzleDataCollection) {
21+
function KuzzleDataMapping(kuzzleDataCollection, mapping) {
2222
Object.defineProperties(this, {
2323
//read-only properties
2424
collection: {
@@ -36,7 +36,7 @@ function KuzzleDataMapping(kuzzleDataCollection) {
3636
writable: true
3737
},
3838
mapping: {
39-
value: {},
39+
value: mapping || {},
4040
enumerable: true,
4141
writable: true
4242
}

test/kuzzle/constructor.test.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ describe('Kuzzle constructor', () => {
2929
it('should expose the documented properties', () => {
3030
var kuzzle = new Kuzzle('nowhere');
3131

32-
kuzzle.should.have.enumerable('autoQueue');
33-
kuzzle.should.have.enumerable('autoReconnect');
34-
kuzzle.should.have.enumerable('autoReplay');
35-
kuzzle.should.have.enumerable('autoResubscribe');
36-
kuzzle.should.have.enumerable('offlineQueue');
37-
kuzzle.should.have.enumerable('queueFilter');
38-
kuzzle.should.have.enumerable('queueMaxSize');
39-
kuzzle.should.have.enumerable('queueTTL');
40-
kuzzle.should.have.enumerable('headers');
41-
kuzzle.should.have.enumerable('metadata');
42-
kuzzle.should.have.enumerable('replayInterval');
43-
kuzzle.should.have.enumerable('reconnectionDelay');
32+
should(kuzzle).have.propertyWithDescriptor('autoQueue', { enumerable: true, writable: true, configurable: false });
33+
should(kuzzle).have.propertyWithDescriptor('autoReconnect', { enumerable: true, writable: false, configurable: false });
34+
should(kuzzle).have.propertyWithDescriptor('autoReplay', { enumerable: true, writable: true, configurable: false });
35+
should(kuzzle).have.propertyWithDescriptor('autoResubscribe', { enumerable: true, writable: true, configurable: false });
36+
should(kuzzle).have.propertyWithDescriptor('offlineQueue', { enumerable: true, writable: true, configurable: false });
37+
should(kuzzle).have.propertyWithDescriptor('queueFilter', { enumerable: true, writable: true, configurable: false });
38+
should(kuzzle).have.propertyWithDescriptor('queueMaxSize', { enumerable: true, writable: true, configurable: false });
39+
should(kuzzle).have.propertyWithDescriptor('queueTTL', { enumerable: true, writable: true, configurable: false });
40+
should(kuzzle).have.propertyWithDescriptor('headers', { enumerable: true, writable: true, configurable: false });
41+
should(kuzzle).have.propertyWithDescriptor('metadata', { enumerable: true, writable: true, configurable: false });
42+
should(kuzzle).have.propertyWithDescriptor('replayInterval', { enumerable: true, writable: true, configurable: false });
43+
should(kuzzle).have.propertyWithDescriptor('reconnectionDelay', { enumerable: true, writable: false, configurable: false });
4444
});
4545

4646
it('should have properties with the documented default values', () => {

test/kuzzle/methods.test.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ describe('Kuzzle methods', function () {
1515
should(collection).be.null();
1616
should(controller).be.exactly(expectedQuery.controller);
1717
should(action).be.exactly(expectedQuery.action);
18-
should(Object.keys(query).length).be.exactly(0);
1918

2019
if (passedOptions) {
2120
should(options).match(passedOptions);
2221
}
2322

23+
if (expectedQuery.body) {
24+
should(query.body).match(expectedQuery.body);
25+
} else {
26+
should(Object.keys(query).length).be.exactly(0);
27+
}
28+
2429
cb(error, result);
2530
},
2631
emitted,
@@ -98,6 +103,85 @@ describe('Kuzzle methods', function () {
98103
});
99104
});
100105

106+
describe('#getStatistics', function () {
107+
beforeEach(function () {
108+
kuzzle = new Kuzzle('foo');
109+
kuzzle.query = queryStub;
110+
emitted = false;
111+
passedOptions = null;
112+
error = null;
113+
result = {statistics: {}};
114+
expectedQuery = {
115+
controller: 'admin',
116+
action: 'getLastStats'
117+
};
118+
});
119+
120+
it('should throw an error if no callback is provided', function () {
121+
should(function () { kuzzle.getStatistics(); }).throw(Error);
122+
should(function () { kuzzle.getStatistics(123456); }).throw(Error);
123+
should(function () { kuzzle.getStatistics({}); }).throw(Error);
124+
should(function () { kuzzle.getStatistics(123456, {}); }).throw(Error);
125+
});
126+
127+
it('should return the last statistics frame if no timestamp is provided', function () {
128+
should(kuzzle.getStatistics(function () {})).be.exactly(kuzzle);
129+
should(emitted).be.true();
130+
});
131+
132+
it('should return statistics frames starting from the given timestamp', function () {
133+
expectedQuery = {
134+
controller: 'admin',
135+
action: 'getStats',
136+
body: { startTime: 123 }
137+
};
138+
139+
result = {
140+
statistics: {
141+
123: {},
142+
456: {},
143+
789: {}
144+
}
145+
};
146+
147+
kuzzle.getStatistics(123, function () {});
148+
should(emitted).be.true();
149+
});
150+
151+
it('should execute the provided callback with an error argument if one occurs', function (done) {
152+
error = 'foobar';
153+
kuzzle.getStatistics(function (err, res) {
154+
should(emitted).be.true();
155+
should(err).be.exactly('foobar');
156+
should(res).be.undefined();
157+
done();
158+
});
159+
});
160+
161+
it('should handle arguments properly', function () {
162+
/*
163+
already tested by previous tests:
164+
getStatistics(callback)
165+
getStatistics(timestamp, callback)
166+
*/
167+
168+
// testing: getStatistics(options, callback)
169+
passedOptions = { foo: 'bar' };
170+
kuzzle.getStatistics(passedOptions, function () {});
171+
should(emitted).be.true();
172+
173+
// testing: getStatistics(timestamp, options callback);
174+
emitted = false;
175+
expectedQuery = {
176+
controller: 'admin',
177+
action: 'getStats',
178+
body: { startTime: 123 }
179+
};
180+
kuzzle.getStatistics(123, passedOptions, function () {});
181+
should(emitted).be.true();
182+
});
183+
});
184+
101185
describe('#dataCollectionFactory', function () {
102186
beforeEach(function () {
103187
kuzzle = new Kuzzle('foo');
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var
2+
should = require('should'),
3+
rewire = require('rewire'),
4+
bluebird = require('bluebird'),
5+
Kuzzle = rewire('../../src/kuzzle'),
6+
KuzzleDataCollection = rewire('../../src/kuzzleDataCollection');
7+
8+
describe('KuzzleDataCollection constructor', function () {
9+
it('should initialize properties and return a valid KuzzleDataCollection object', function () {
10+
var
11+
kuzzle = new Kuzzle('foo'),
12+
collection = 'foobar',
13+
c;
14+
15+
kuzzle.headers.some = 'headers';
16+
c = new KuzzleDataCollection(kuzzle, collection);
17+
18+
// the collection "headers" should be a hard copy of the kuzzle ones
19+
kuzzle.headers = { someother: 'headers' };
20+
21+
should(c).be.instanceof(KuzzleDataCollection);
22+
should(c).have.propertyWithDescriptor('collection', { enumerable: true, writable: false, configurable: false });
23+
should(c).have.propertyWithDescriptor('kuzzle', { enumerable: true, writable: false, configurable: false });
24+
should(c).have.propertyWithDescriptor('headers', { enumerable: true, writable: true, configurable: false });
25+
should(c.collection).be.exactly(collection);
26+
should(c.kuzzle).be.exactly(kuzzle);
27+
should(c.headers.some).be.exactly('headers');
28+
should(c.headers.someother).be.undefined();
29+
});
30+
31+
it('should promisify the right functions', () => {
32+
var
33+
kuzzle,
34+
dataCollection;
35+
36+
Kuzzle.prototype.bluebird = bluebird;
37+
kuzzle = new Kuzzle('foo');
38+
dataCollection = new KuzzleDataCollection(kuzzle, 'foo');
39+
40+
should.exist(dataCollection.advancedSearchPromise);
41+
should.exist(dataCollection.countPromise);
42+
should.exist(dataCollection.createPromise);
43+
should.exist(dataCollection.createDocumentPromise);
44+
should.exist(dataCollection.deletePromise);
45+
should.exist(dataCollection.deleteDocumentPromise);
46+
should.exist(dataCollection.fetchDocumentPromise);
47+
should.exist(dataCollection.fetchAllDocumentsPromise);
48+
should.exist(dataCollection.getMappingPromise);
49+
should.not.exist(dataCollection.publishPromise);
50+
should.exist(dataCollection.replaceDocumentPromise);
51+
should.not.exist(dataCollection.setHeadersPromise);
52+
should.not.exist(dataCollection.subscribePromise);
53+
should.exist(dataCollection.truncatePromise);
54+
should.exist(dataCollection.updateDocumentPromise);
55+
});
56+
57+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var
2+
should = require('should'),
3+
rewire = require('rewire'),
4+
bluebird = require('bluebird'),
5+
Kuzzle = rewire('../../src/kuzzle'),
6+
KuzzleDataCollection = rewire('../../src/kuzzleDataCollection');
7+
8+
describe('KuzzleDataCollection methods', function () {
9+
10+
});

0 commit comments

Comments
 (0)