diff --git a/package-lock.json b/package-lock.json index da5b49ce..93905818 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "aws-lambda-stream", - "version": "1.0.30", + "version": "1.0.31", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "aws-lambda-stream", - "version": "1.0.28", + "version": "1.0.31", "license": "MIT", "dependencies": { "object-sizeof": "^2.6.0" diff --git a/package.json b/package.json index 58187c62..ab144178 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aws-lambda-stream", - "version": "1.0.30", + "version": "1.0.31", "description": "Create stream processors with AWS Lambda functions.", "keywords": [ "aws", diff --git a/src/utils/tags.js b/src/utils/tags.js index fc8d4785..239fb8fa 100644 --- a/src/utils/tags.js +++ b/src/utils/tags.js @@ -1,6 +1,6 @@ import { skipTag } from '../filters'; -export const adornStandardTags = (eventField) => (uow) => (!uow[eventField] ? uow : ({ +export const adornStandardTags = (eventField) => (uow) => ((!uow[eventField] || Object.keys(uow[eventField]).length === 0) ? uow : ({ ...uow, [eventField]: { ...uow[eventField], diff --git a/test/unit/utils/tags.test.js b/test/unit/utils/tags.test.js new file mode 100644 index 00000000..93873a05 --- /dev/null +++ b/test/unit/utils/tags.test.js @@ -0,0 +1,67 @@ +import 'mocha'; +import { expect } from 'chai'; +import sinon from 'sinon'; + +import { + adornStandardTags, +} from '../../../src/utils'; + +describe('utils/tags.js', () => { + afterEach(sinon.restore); + + it('should adorn tags, event field exists', () => { + process.env.ACCOUNT_NAME = 'account1'; + process.env.STAGE = 'dev'; + process.env.SERVICE = 'service1'; + process.env.AWS_LAMBDA_FUNCTION_NAME = 'lambda1'; + const uow = { + pipeline: 'thing-pipeline', + emit: { + type: 'thing-updated', + partitionKey: 'thing1', + timestamp: 1600144863435, + thing: { + id: '7588777d-19a0-49d5-856f-a657ed4b5034', + timestamp: 1600144863435, + }, + }, + }; + const event = adornStandardTags('emit')(uow); + delete process.env.ACCOUNT_NAME; + delete process.env.STAGE; + delete process.env.SERVICE; + delete process.env.AWS_LAMBDA_FUNCTION_NAME; + expect(event).to.deep.equal({ + ...uow, + emit: { + ...uow.emit, + tags: { + account: 'account1', + region: 'us-west-2', + stage: 'dev', + source: 'service1', + functionname: 'lambda1', + pipeline: 'thing-pipeline', + skip: true, + }, + }, + }); + }); + + it('should not adorn tags, event field does not exist', () => { + const uow = { + pipeline: 'thing-pipeline', + }; + const event = adornStandardTags('emit')(uow); + expect(event).to.deep.equal(uow); + }); + + it('should not adorn tags, event field is empty object', () => { + const uow = { + pipeline: 'thing-pipeline', + emit: {}, + }; + const event = adornStandardTags('emit')(uow); + expect(event).to.deep.equal(uow); + }); +});