|
| 1 | +var |
| 2 | + should = require('should'), |
| 3 | + rewire = require('rewire'), |
| 4 | + Kuzzle = rewire('../../src/kuzzle'), |
| 5 | + KuzzleDataCollection = require('../../src/kuzzleDataCollection'); |
| 6 | + |
| 7 | +describe('Kuzzle methods', function () { |
| 8 | + var |
| 9 | + expectedQuery, |
| 10 | + passedOptions, |
| 11 | + error, |
| 12 | + result, |
| 13 | + queryStub = function (collection, controller, action, query, options, cb) { |
| 14 | + emitted = true; |
| 15 | + should(collection).be.null(); |
| 16 | + should(controller).be.exactly(expectedQuery.controller); |
| 17 | + should(action).be.exactly(expectedQuery.action); |
| 18 | + should(Object.keys(query).length).be.exactly(0); |
| 19 | + |
| 20 | + if (passedOptions) { |
| 21 | + should(options).match(passedOptions); |
| 22 | + } |
| 23 | + |
| 24 | + cb(error, result); |
| 25 | + }, |
| 26 | + emitted, |
| 27 | + kuzzle; |
| 28 | + |
| 29 | + describe('#getAllStatistics', function () { |
| 30 | + beforeEach(function () { |
| 31 | + kuzzle = new Kuzzle('foo'); |
| 32 | + kuzzle.query = queryStub; |
| 33 | + emitted = false; |
| 34 | + passedOptions = null; |
| 35 | + error = null; |
| 36 | + result = {statistics: {}}; |
| 37 | + expectedQuery = { |
| 38 | + controller: 'admin', |
| 39 | + action: 'getAllStats' |
| 40 | + }; |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return the kuzzle instance when called', function () { |
| 44 | + should(kuzzle.getAllStatistics(function () {})).be.exactly(kuzzle); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw an error if no callback is provided', function () { |
| 48 | + should(function () { kuzzle.getAllStatistics(); }).throw(Error); |
| 49 | + should(emitted).be.false(); |
| 50 | + should(function () { kuzzle.getAllStatistics({some: 'options'}); }).throw(Error); |
| 51 | + should(emitted).be.false(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should call the query function with the right arguments', function () { |
| 55 | + kuzzle.getAllStatistics(function () {}); |
| 56 | + should(emitted).be.true(); |
| 57 | + |
| 58 | + emitted = false; |
| 59 | + passedOptions = {queuable: true, metadata: {foo: 'bar'}}; |
| 60 | + kuzzle.getAllStatistics(passedOptions, function () {}); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should reformat the statistics frame according to the documentation', function (done) { |
| 64 | + this.timeout(50); |
| 65 | + |
| 66 | + result = { |
| 67 | + statistics: { |
| 68 | + 123: { some: 'stats' }, |
| 69 | + 456: { someother: 'stats'} |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + kuzzle.getAllStatistics(function (err, res) { |
| 74 | + should(err).be.null(); |
| 75 | + should(res).be.an.Array(); |
| 76 | + should(res.length).be.exactly(2); |
| 77 | + should(res[0]).be.an.Object(); |
| 78 | + should(Object.keys(res[0]).length).be.exactly(2); |
| 79 | + should(res[0].timestamp).be.exactly('123'); |
| 80 | + should(res[0].some).be.exactly('stats'); |
| 81 | + should(res[1]).be.an.Object(); |
| 82 | + should(Object.keys(res[1]).length).be.exactly(2); |
| 83 | + should(res[1].timestamp).be.exactly('456'); |
| 84 | + should(res[1].someother).be.exactly('stats'); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should execute the callback with an error if an error occurs', function (done) { |
| 90 | + this.timeout(50); |
| 91 | + error = 'foobar'; |
| 92 | + |
| 93 | + kuzzle.getAllStatistics(function (err, res) { |
| 94 | + should(err).be.exactly('foobar'); |
| 95 | + should(res).be.undefined(); |
| 96 | + done(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('#dataCollectionFactory', function () { |
| 102 | + beforeEach(function () { |
| 103 | + kuzzle = new Kuzzle('foo'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should throw an error if the kuzzle instance has been invalidated', function () { |
| 107 | + kuzzle.socket = null; |
| 108 | + should(function () { kuzzle.dataCollectionFactory('foo'); }).throw(Error); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should create and store the data collection instance if needed', function () { |
| 112 | + var collection = kuzzle.dataCollectionFactory('foo'); |
| 113 | + |
| 114 | + should(kuzzle.collections['foo']).not.be.undefined().and.be.instanceof(KuzzleDataCollection); |
| 115 | + should(collection).be.instanceof(KuzzleDataCollection); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should simply pull the collection from the collection history if reinvoked', function () { |
| 119 | + kuzzle.collections['foo'] = 'bar'; |
| 120 | + should(kuzzle.dataCollectionFactory('foo')).be.a.String().and.be.exactly('bar'); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('#listCollections', function () { |
| 125 | + beforeEach(function () { |
| 126 | + kuzzle = new Kuzzle('foo'); |
| 127 | + kuzzle.query = queryStub; |
| 128 | + emitted = false; |
| 129 | + passedOptions = null; |
| 130 | + error = null; |
| 131 | + result = {collections: []}; |
| 132 | + expectedQuery = { |
| 133 | + controller: 'read', |
| 134 | + action: 'listCollections' |
| 135 | + }; |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return the kuzzle instance when called', function () { |
| 139 | + should(kuzzle.listCollections(function () {})).be.exactly(kuzzle); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should throw an error if no callback has been provided', function () { |
| 143 | + should(function () { kuzzle.listCollections(); }).throw(Error); |
| 144 | + should(emitted).be.false(); |
| 145 | + should(function () { kuzzle.listCollections({}); }).throw(Error); |
| 146 | + should(emitted).be.false(); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should call query with the right arguments', function (done) { |
| 150 | + this.timeout(50); |
| 151 | + result = { collections: ['foo', 'bar', 'baz', 'qux'] }; |
| 152 | + |
| 153 | + kuzzle.listCollections(function (err, res) { |
| 154 | + should(err).be.null(); |
| 155 | + should(res).be.an.Array().and.match(result.collections); |
| 156 | + done(); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it('should execute the callback with an error if one occurs', function (done) { |
| 161 | + this.timeout(50); |
| 162 | + error = 'foobar'; |
| 163 | + |
| 164 | + kuzzle.listCollections(function (err, res) { |
| 165 | + should(err).be.exactly('foobar'); |
| 166 | + should(res).be.undefined(); |
| 167 | + done(); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe('#logout', function () { |
| 173 | + it('should clean up and invalidate the instance if called', function () { |
| 174 | + var kuzzle = new Kuzzle('foo'); |
| 175 | + |
| 176 | + kuzzle.collections = { foo: {}, bar: {}, baz: {} }; |
| 177 | + kuzzle.logout(); |
| 178 | + |
| 179 | + should(kuzzle.socket).be.null(); |
| 180 | + should(kuzzle.collections).be.empty(); |
| 181 | + should(function () { kuzzle.isValid(); }).throw(Error); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('#now', function () { |
| 186 | + beforeEach(function () { |
| 187 | + kuzzle = new Kuzzle('foo'); |
| 188 | + kuzzle.query = queryStub; |
| 189 | + emitted = false; |
| 190 | + passedOptions = null; |
| 191 | + error = null; |
| 192 | + result = {now: Date.now()}; |
| 193 | + expectedQuery = { |
| 194 | + controller: 'read', |
| 195 | + action: 'now' |
| 196 | + }; |
| 197 | + }); |
| 198 | + |
| 199 | + it('should return the kuzzle instance when called', function () { |
| 200 | + should(kuzzle.now(function () {})).be.exactly(kuzzle); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should throw an error if called without a callback', function () { |
| 204 | + should(function () { kuzzle.now(); }).throw(Error); |
| 205 | + should(emitted).be.false(); |
| 206 | + should(function () { kuzzle.now({}); }).throw(Error); |
| 207 | + should(emitted).be.false(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('should call query with the right arguments', function (done) { |
| 211 | + this.timeout(50); |
| 212 | + |
| 213 | + kuzzle.now(function (err, res) { |
| 214 | + should(err).be.null(); |
| 215 | + should(res).be.exactly(result.now); |
| 216 | + done(); |
| 217 | + }); |
| 218 | + }); |
| 219 | + |
| 220 | + it('should execute the callback with an error argument if one occurs', function (done) { |
| 221 | + this.timeout(50); |
| 222 | + error = 'foobar'; |
| 223 | + |
| 224 | + kuzzle.now(function (err, res) { |
| 225 | + should(err).be.exactly('foobar'); |
| 226 | + should(res).be.undefined(); |
| 227 | + done(); |
| 228 | + }); |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + describe('#setHeaders', function () { |
| 233 | + beforeEach(function () { |
| 234 | + kuzzle = new Kuzzle('foo'); |
| 235 | + }); |
| 236 | + |
| 237 | + it('should throw an error if an invalid content object is provided', function () { |
| 238 | + should(function () { kuzzle.setHeaders(); }).throw(Error); |
| 239 | + should(function () { kuzzle.setHeaders(123); }).throw(Error); |
| 240 | + should(function () { kuzzle.setHeaders('foo'); }).throw(Error); |
| 241 | + should(function () { kuzzle.setHeaders(['mama', 'mia']); }).throw(Error); |
| 242 | + }); |
| 243 | + |
| 244 | + it('should set headers properly', function () { |
| 245 | + should(kuzzle.setHeaders({foo: 'bar'})).be.exactly(kuzzle); |
| 246 | + kuzzle.setHeaders({bar: 'baz', baz: ['bar, baz, qux', 'foo']}); |
| 247 | + kuzzle.setHeaders({foo: { bar: 'baz'}}); |
| 248 | + |
| 249 | + should(kuzzle.headers).match({bar: 'baz', baz: ['bar, baz, qux', 'foo'], foo: { bar: 'baz'}}); |
| 250 | + }); |
| 251 | + |
| 252 | + it('should replace existing headers if asked to', function () { |
| 253 | + kuzzle.setHeaders({foo: 'bar'}); |
| 254 | + kuzzle.setHeaders({}, true); |
| 255 | + |
| 256 | + should(kuzzle.headers).be.empty(); |
| 257 | + }); |
| 258 | + }); |
| 259 | +}); |
0 commit comments