UNPKG

regard

Version:

Sugar-interface to access multiple data sources.

158 lines (134 loc) 4.52 kB
var _ = require('lodash'), Chai = require('chai'), Connector = require('../../lib/connector'), FsConnector = require('../../lib/connectors/fs'), Q = require('q'); var assert = Chai.assert, expect = Chai.expect; var clazz = FsConnector, foo, bar; function createPromise(func, param) { var processing = Q.defer(); func(param, processing.resolve, processing.reject); return processing.promise; } describe('FsConnector', function () { it('should have key equals to its filename ("fs")', function () { foo = clazz(); expect(foo.key).to.eql(Connector.generateKey('../../lib/connectors/fs')); }); it('should have method "read" in context', function () { foo = clazz(); expect(foo.context.method).to.eql(FsConnector.Methods.READ); }); it('should accept path starts with "." or "/"', function () { foo = clazz(); expect(foo.checkPath('.')).to.be.ok; expect(foo.checkPath('/')).to.be.ok; expect(foo.checkPath('foo')).to.not.be.ok; expect(foo.checkPath()).to.not.be.ok; expect(foo.checkPath('')).to.not.be.ok; }); it('should accept methods "read" and "write"', function () { foo = clazz(); expect(foo.checkMethod('read')).to.be.ok; expect(foo.checkMethod('write')).to.be.ok; expect(foo.checkMethod('foo')).to.not.be.ok; }); it('should have beforeRequest ( [method] [path] [obj] [options] ) method', function () { foo = clazz(); bar = { path: 'foo', context: {} }; var func = foo.handlers.get('default').before; func(bar, 'bar'); expect(bar.path).to.eql('foo/bar'); func(bar, 'read'); expect(bar.context.method).to.eql(FsConnector.Methods.READ); func(bar, 'write'); expect(bar.context.method).to.eql(FsConnector.Methods.WRITE); func(bar, {foo:'bar'}); expect(bar.context.options).to.eql({foo:'bar'}); func(bar, FsConnector.Methods.WRITE, {foo:'bar'}, {foo:'bax'}); expect(bar.context.obj).to.eql({foo:'bar'}); expect(bar.context.options).to.eql({foo:'bax'}); func(bar, FsConnector.Methods.READ, {foo:'bar'}); expect(bar.context.method).to.eql(FsConnector.Methods.READ); expect(bar.context.obj).to.eql({foo:'bar'}); func = foo.handlers.get(FsConnector.Methods.READ).before; func(bar, {foo:'bar'}); expect(bar.context.options).to.eql({foo:'bar'}); func = foo.handlers.get(FsConnector.Methods.WRITE).before; func(bar, {foo:'bar'}, {foo:'bax'}); expect(bar.context.obj).to.eql({foo:'bar'}); expect(bar.context.options).to.eql({foo:'bax'}); }); it('should failed when read bad file', function () { foo = clazz(); bar = { path: __dirname+'/../../', context: { method: FsConnector.Methods.READ } }; var func = _.get(foo.handlers.get(FsConnector.Methods.READ), 'process'), promise = createPromise(func, bar); return promise.then(function () { assert().fail(); }, function (err) { expect(err.code).to.eql('EISDIR'); }); }); it('should succeed when read good file', function () { foo = clazz(); bar = { path: __dirname+'/../../package.json', context: { method: FsConnector.Methods.READ } }; var func = _.get(foo.handlers.get(FsConnector.Methods.READ), 'process'), promise = createPromise(func, bar); return promise.then(function (res) { expect(res.name).to.eql('regard'); }, function (err) { assert().fail(); }); }); it('should failed when write bad file', function () { foo = clazz(); bar = { path: __dirname+'/../../', context: { method: FsConnector.Methods.WRITE, obj: {foo:'bar'} } }; var func = _.get(foo.handlers.get(FsConnector.Methods.WRITE), 'process'), promise = createPromise(func, bar); return promise.then(function () { assert().fail(); }, function (err) { expect(err.code).to.eql('EISDIR'); }); }); it('should succeed when write good file', function () { foo = clazz(); bar = { path: __dirname+'/../../sample.json', context: { method: FsConnector.Methods.WRITE, obj: {foo:'bar'} } }; var func = _.get(foo.handlers.get(FsConnector.Methods.WRITE), 'process'), promise = createPromise(func, bar); return promise.then(function (res) { expect(res.foo).to.eql('bar'); expect(require(bar.path).foo).to.eql('bar'); }, function (err) { assert().fail(); }); }); });