UNPKG

@incdevco/framework

Version:
314 lines (180 loc) 6.52 kB
var Expect = require('chai').expect; var Mock = require('../../mock'); var Sinon = require('sinon'); var Lambda = require('./index'); describe('lambda dynamodb-stream', function () { 'use strict'; var config, lambda, mock, sandbox; beforeEach(function () { config = { console: { log: function () { } } }; lambda = new Lambda(config); mock = new Mock(); sandbox = Sinon.sandbox.create(); sandbox.stub(lambda, 'log'); }); afterEach(function () { mock.restore(); sandbox.verifyAndRestore(); }); describe('attributeToData', function() { it('should convert boolean', function () { var attribute, result; attribute = { 'BOOL': true }; result = lambda.attributeToData(attribute); Expect(result).to.equal(true, 'result'); }); it('should convert list', function () { var attribute, result; attribute = { L: [ { BOOL: true } ] }; result = lambda.attributeToData(attribute); Expect(result).to.deep.equal([ true ], 'result'); }); it('should convert map', function () { var attribute, result; attribute = { M: { key: { BOOL: true } } }; result = lambda.attributeToData(attribute); Expect(result).to.deep.equal({ key: true }, 'result'); }); it('should convert number', function () { var attribute, result; attribute = { N: '1234567890' }; result = lambda.attributeToData(attribute); Expect(result).to.equal(1234567890, 'result'); }); it('should convert null', function () { var attribute, result; attribute = { NULL: true }; result = lambda.attributeToData(attribute); Expect(result).to.equal(null, 'result'); }); it('should convert string', function () { var attribute, result; attribute = { S: 'string' }; result = lambda.attributeToData(attribute); Expect(result).to.equal('string', 'result'); }); it('should convert undefined', function () { var attribute, result; attribute = {}; result = lambda.attributeToData(attribute); Expect(result).to.equal(undefined, 'result'); }); }); describe('handler', function() { it('should', function (done) { var context, event, expected = 3; event = require('./example-event.json'); context = { fail: done, succeed: function (result) { Expect(result).to.equal(expected, 'result'); mock.done(done); } }; lambda.register('blog-post', 'INSERT', function (event, record, keys, newImage, oldImage) { Expect(record.eventName).to.equal('INSERT', 'eventName'); return true; }); lambda.register('blog-post', 'MODIFY', function (event, record, keys, newImage, oldImage) { Expect(record.eventName).to.equal('MODIFY', 'eventName'); return true; }); lambda.register('blog-post', 'REMOVE', function (event, record, keys, newImage, oldImage) { Expect(record.eventName).to.equal('REMOVE', 'eventName'); return true; }); lambda.handler(event, context); }); it('should do nothing if no handler for table', function (done) { var context, event, expected = 3; event = require('./example-event.json'); context = { fail: done, succeed: function (result) { Expect(result).to.equal(expected, 'result'); mock.done(done); } }; lambda.handler(event, context); }); it('should do nothing if there is no handler for that event', function (done) { var context, event, expected = 3; event = require('./example-event.json'); context = { fail: done, succeed: function (result) { Expect(result).to.equal(expected, 'result'); mock.done(done); } }; lambda.register('blog-post', 'INSERT', function (event, record, keys, newImage, oldImage) { Expect(record.eventName).to.equal('INSERT', 'eventName'); return true; }); lambda.register('blog-post', 'MODIFY', function (event, record, keys, newImage, oldImage) { Expect(record.eventName).to.equal('MODIFY', 'eventName'); return true; }); lambda.handler(event, context); }); it('should catch any exception thrown by the handler', function (done) { var context, event, expected = new Error('expected'); event = require('./example-event.json'); context = { succeed: done, fail: function (exception) { Expect(exception).to.equal(expected, 'exception'); mock.done(done); } }; lambda.register('blog-post', 'INSERT', function (event, record, keys, newImage, oldImage) { Expect(record.eventName).to.equal('INSERT', 'eventName'); return true; }); lambda.register('blog-post', 'MODIFY', function (event, record, keys, newImage, oldImage) { throw expected; }); lambda.handler(event, context); }); }); describe('register', function() { it('should register many handlers', function () { var handlers = { 'INSERT': 'INSERT', 'MODIFY': 'MODIFY', 'REMOVE': 'REMOVE' }, table = 'table'; lambda.register(table, handlers); Expect(lambda.handlers[table]).to.deep.equal(handlers, 'handlers'); }); }); });