UNPKG

regard

Version:

Sugar-interface to access multiple data sources.

83 lines (69 loc) 2.42 kB
var _ = require('lodash'), Chai = require('chai'), Connector = require('../../lib/connector'), Q = require('q'), MongodbConnector = require('../../lib/connectors/mongodb'); var assert = Chai.assert, expect = Chai.expect; var clazz = MongodbConnector, foo, bar; function createPromise(func, param) { var processing = Q.defer(); func(param, processing.resolve, processing.reject); return processing.promise; } describe('MongodbConnector', function () { it('should must have key equals to its filename ("mongodb")', function () { foo = clazz(); expect(foo.key).to.eql(Connector.generateKey('../../lib/connectors/mongodb')); }); it('should have method "find" in context', function () { foo = clazz(); expect(foo.context.method).to.eql('find'); }); it('should accept path starts with "mongodb://"', function () { foo = clazz(); expect(foo.checkPath('mongo://')).to.not.be.ok; expect(foo.checkPath('mongodb://')).to.be.ok; }); it('should accept monk methods '+MongodbConnector.Methods, function () { foo = clazz(); _.forEach(MongodbConnector.Methods, function (value) { expect(foo.checkMethod(value)).to.be.ok; }); }); it('should have beforeRequest ( [method] [args...] ) method', function () { foo = clazz(); bar = { args: ['foo'], path: 'databaseName', context: { method: 'find' } }; var func = foo.handlers.get('default').before, req; req = _.partial(func, bar).apply(foo, bar.args); expect(req.context.method).to.eql('find'); expect(req.context.collection).to.eql('foo'); bar.args = ['insert', 'bar']; req = _.partial(func, bar).apply(foo, bar.args); expect(req.context.method).to.eql('insert'); expect(req.context.collection).to.eql('bar'); bar.context.collection = undefined; bar.args = ['id']; req = _.partial(func, bar).apply(foo, bar.args); expect(req.context.method).to.eql('id'); bar.args = ['baz']; expect(req.context.collection).to.not.eql('baz'); req = _.partial(func, bar).apply(foo, bar.args); expect(req.context.collection).to.eql('baz'); }); it('should have helper for each monk methods', function () { foo = clazz(); _.forEach(_.remove(MongodbConnector.Methods, 'HEAD'), function (x) { var func = foo.handlers[x]; expect(func).is.a('function'); }); }); });