How to Test Dynamo DB code Node Js ( Mocha - chai ) - node.js

I'm trying to write a unit test using sinon promise support. I'm using DocumentClient.
My code looks like this:
const docClient = new AWS.DynamoDB.DocumentClient({ region: "us-east-2" });
const create = async (data, tableName, connectionData) => {
try {
const creditLine = {
...data,
id: uuidV4(),
};
const params = {
TableName: tableName,
Item: creditLine,
};
await docClient.put(params).promise();
return Responses._201(creditLine, connectionData);
} catch (e) {
return handleError(e, connectionData);
}
};
I cannot to prove the function create cause i can get into docCliente.put

Related

sinon stub for lambda function which is inside another lambda

Am writing unit test case for my code, as am calling another lambda function inside my lambda am not sure how to mock the inner lambda value, so because of this my test case is getting timed out. Attaching my code below
Test case file
"use strict";
const sinon = require("sinon");
const AWS = require("aws-sdk");
const expect = require("chai").expect;
const models = require("common-lib").models;
const { Organization } = models;
const DATA_CONSTANTS = require("./data/deleteOrganization");
const wrapper = require("../../admin/deleteOrganization");
const sandbox = sinon.createSandbox();
describe("Start Test updateOrganization", () => {
beforeEach(() => {
sandbox.stub(Organization, "update").resolves([1]);
});
afterEach(async () => {
sandbox.restore();
});
it("Test 03: Test to check success returned by handler", async () => {
const mLambda = {
invoke: sinon.stub().returnsThis(),
promise: sinon.stub(),
};
const response = await wrapper.handler(
DATA_CONSTANTS.API_REQUEST_OBJECT_FOR_200
);
console.log({ response });
expect(response.statusCode).to.be.equal(200);
const body = JSON.parse(response.body);
expect(body.message).to.be.equal("Updated successfully");
});
});
Code function
exports.handler = asyncHandler(async (event) => {
InitLambda("userService-deleteOrganization", event);
const { id } = event.pathParameters;
if (isEmpty(id)) {
return badRequest({
message: userMessages[1021],
});
}
try {
const orgrepo = getRepo(Organization);
const [rowsUpdated] = await orgrepo.update(
{ isDeleted: true },
{ org_id: id }
);
if (!rowsUpdated) {
return notFound({
message: userMessages[1022],
});
}
const lambda = new AWS.Lambda({
region: process.env.region,
});
await lambda
.invoke({
FunctionName:
"user-service-" + process.env.stage + "-deleteOrganizationDetail",
InvocationType: "Event",
Payload: JSON.stringify({
pathParameters: { id },
headers: event.headers,
}),
})
.promise();
return success({
message: userMessages[1023],
});
} catch (err) {
log.error(err);
return failure({
error: err,
message: err.message,
});
}
});
It seems that you are not properly stubbing the AWS.Lambda object.
try this,
const sinon = require("sinon");
const AWS = require("aws-sdk");
const expect = require("chai").expect;
const models = require("common-lib").models;
const { Organization } = models;
const DATA_CONSTANTS = require("./data/deleteOrganization");
const wrapper = require("../../admin/deleteOrganization");
const sandbox = sinon.createSandbox();
describe("Start Test updateOrganization", () => {
beforeEach(() => {
sandbox.stub(Organization, "update").resolves([1]);
});
afterEach(async () => {
sandbox.restore();
});
it("Test 03: Test to check success returned by handler", async () => {
const mLambda = { invoke: sinon.stub().returnsThis(), promise: sinon.stub() };
// you missed the below line
sinon.stub(AWS, 'Lambda').callsFake(() => mLambda);
const response = await wrapper.handler(
DATA_CONSTANTS.API_REQUEST_OBJECT_FOR_200
);
console.log({ response });
expect(response.statusCode).to.be.equal(200);
const body = JSON.parse(response.body);
expect(body.message).to.be.equal("Updated successfully");
sinon.assert.calledOnce(AWS.Lambda);
sinon.assert.calledWith(mLambda.invoke, {});
sinon.assert.calledOnce(mLambda.promise);
});
});
I can see that,
You are writing entire logic inside your handler function. This makes it less testable.
To overcome this you can write your code in such a way that is divided into small functions, which are easy to mock in test case files or testable independently. Handler function should only make call to those functions and return the result to the caller.
for Eg.
Lambda Handler:
exports.lambdaHandler = async (event) => {
// do some init work here
const lambdaInvokeResponse = await exports.invokeLambda(params);
}
exports.invokeLambda = async (params) {
const response = await lambda.invoke(params).promise();
return response;
}
test cases:
it('My Test Case - Success', async () => {
const result = await app.lambdaHandler(event);
const invikeLambdaResponse = {
// some dummy response
};
sinon.replace(app, 'invokeLambda', sinon.fake.returns(invikeLambdaResponse ));
});
This is now mocking the only lambda invoke part.
You can mock all the external calls like this (dynamodb, invoke, sns, etc.)
You can set spy and check if the called method is called as per desired arguments

Lambda Invoke not triggering second lambda

I have gone through similar threads to fix this issue but I have had no luck. Both lambdas can be trigger independently of one another, and I am able to invoke the second Lambda through the command line, but my code does not work.
'use strict'
/* eslint max-statements: ['error', 100, { 'ignoreTopLevelFunctions': true }] */
const RespHelper = require('../../lib/response')
const { uuid } = require('uuidv4')
const AWS = require('aws-sdk')
const DB = require('./dynamo')
const respHelper = new RespHelper()
const Dynamo = new DB()
const lambda = new AWS.Lambda({
region: 'us-west-2'
})
const secondLambda = async (lambdaData) => {
var params = {
LogType: 'Tail',
FunctionName: 'second_lambda_name',
InvocationType: 'RequestResponse',
Payload: JSON.stringify(lambdaData)
}
lambda.invoke(params, function (err, data) {
if (err) {
console.log(err)
} else {
console.log(`Success: ${data.Payload}`)
}
})
}
exports.handler = async event => {
const id = uuid()
let bodyData = {
uuid: id,
user: 'owner#email.com',
processingStatus: 'IN_PROGRESS'
}
let payloadData = {
uuid: id,
user: 'owner#email.com',
processingStatus: 'COMPLETE'
}
try {
await Dynamo.writeRecordToDB(bodyData)
await secondLambda(payloadData)
return respHelper.sendResponse(200, { message: bodyData })
} catch (err) {
console.log(`Failure: ${err}`)
return respHelper.sendResponse(400, { message: 'ERROR' })
}
}
I have double checked the lambda role and it has the Invoke Lambda and Invoke Asynchronous Invoke permission on all resources. Console outputs don't give me any indication of why this is not working. Any help is appreciated.
You're awaiting a callback when you need to await a promise
const secondLambda = async lambdaData =>
lambda
.invoke({
LogType: 'Tail',
FunctionName: 'second_lambda_name',
InvocationType: 'RequestResponse',
Payload: JSON.stringify(lambdaData),
})
.promise()

My Lambda ends before code is complete - node.js

I know this has been asked in various ways before, but I can't figure it out. I'm still very new to node.js and lambda. This code will work if I run the lambda twice, but never runs to completion the first time. This also works fine if I run this from a local IDE by adding
exports.handler();
to the end of the code block.
The code queries DynamoDB for results and then attempts to delete those records from Dynamo. The query part seems to work every time, but the deletion part fails to happen on the first invocation.
I can't seem to figure out what changes are necessary to for lambda to wait until all of my processes are complete.
Thanks in advance.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({ region: 'us-east-2' });
exports.handler = async (event) => {
// Create DynamoDB service object
const ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-east-2" });
const tablename = process.env.table_name;
let dynapromises = [];
let params = {
ExpressionAttributeValues: {
':offNum': { S: process.env.cost_center },
':s': { N: '2' }
},
ExpressionAttributeNames: {
"#notif_status": "status"
},
KeyConditionExpression: 'officeNumber = :offNum',
TableName: tablename,
IndexName: 'officeNumberIndex',
ProjectionExpression: "notificationNumber",
FilterExpression: '(attribute_not_exists(#notif_status) or #notif_status = :s) and attribute_not_exists(statusTimes)'
};
let qresults = await ddb.query(params).promise();
console.log("Count of notifs again " + qresults.Items.length);
qresults.Items.forEach(function(element, index, array) {
console.log(element.notificationNumber.S);
let delparams = {
TableName: tablename,
ReturnValues: "ALL_OLD",
Key: {
notificationNumber: {
S: element.notificationNumber.S
}
}
};
dynapromises.push(ddb.deleteItem(delparams).promise().then(function(data) {
console.log("Deleted Record:"+ JSON.stringify(data)); // successful response
}, function(error) {
console.log(error, error.stack); // an error occurred
}));
console.log("deletion parameters " + JSON.stringify(delparams));
});
Promise.all(dynapromises).then(res => {
console.log("All promises done");
});
return qresults.Items.length;
};
The issue is in that you are returning before all the promises are completed, you need to move the return qresults.Items.length; inside the last then.
try with this code:
** UPDATE: Change the snippet with the working code **
// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');
// Set the region
AWS.config.update({ region: 'us-east-2' });
exports.handler = async (event) => {
// Create DynamoDB service object
const ddb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
const documentClient = new AWS.DynamoDB.DocumentClient({ region: "us-east-2" });
const tablename = process.env.table_name;
let params = {
ExpressionAttributeValues: {
':offNum': { S: process.env.cost_center },
':s': { N: '2' }
},
ExpressionAttributeNames: {
"#notif_status": "status"
},
KeyConditionExpression: 'officeNumber = :offNum',
TableName: tablename,
IndexName: 'officeNumberIndex',
ProjectionExpression: "notificationNumber",
FilterExpression: '(attribute_not_exists(#notif_status) or #notif_status = :s) and attribute_not_exists(statusTimes)'
};
let qresults = await ddb.query(params).promise();
console.log("Count of notifs again " + qresults.Items.length);
const dynapromises = qresults.Items.map( async element => {
let delparams = {
TableName: tablename,
ReturnValues: "ALL_OLD",
Key: {
notificationNumber: {
S: element.notificationNumber.S
}
}
};
try {
console.log("deletion parameters " + JSON.stringify(delparams));
const data = await ddb.deleteItem(delparams).promise();
console.log( "Deleted Record:"+ JSON.stringify(data) );
} catch ( err ) {
console.log(error, error.stack); // an error occurred
}
} )
await Promise.all(dynapromises)
console.log("All promises done");
return qresults.Items.length;
};
The code that #pepo posted is performing the Dynamo deletions on the first invocation of the Lambda. Thanks for his work and the responses from everyone.

AWS Lambda using s3 getObject function and putItem function to insert it into DynamoDB but nothing happens

this is the node.js code:
'use strict';
const AWS = require("aws-sdk");
AWS.config.update({
region: 'eu-west-1'});
const docClient = new AWS.DynamoDB.DocumentClient();
const tableName = 'Fair';
const s3 = new AWS.S3();
exports.handler = async (event) => {
var getParams = {
Bucket: 'dataforfair', //s3 bucket name
Key: 'fairData.json' //s3 file location
}
const data = await s3.getObject(getParams).promise()
.then( (data) => {
//parse JSON
let fairInformations = JSON.parse(data.Body.toString());
fairInformations.forEach(function(fairInformationEntry) {
console.log(fairInformationEntry);
var params = {
TableName: tableName,
Item: {
"year": fairInformationEntry.year,
"fairName": fairInformationEntry.fairName,
"info": fairInformationEntry.info
}
};
docClient.put(params, function(err, data) {
console.log('*****test');
if (err) {
console.error("Unable to add fairInformation", fairInformationEntry.fairName, ". Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("PutItem succeeded:", fairInformationEntry.fairName);
}
});
});
})
.catch((err) => {
console.log(err);
});
const response = {
statusCode: 200,
body: JSON.stringify(data),
};
return response;
};
Hello everyone,
I want to put the data into the Dynamo DB after getting the JSON file from the s3 Bucket. Getting the JSON works and the console.log(fairInformationEntry); is also still triggered, but the docClient.put() never gets called. I am getting no error, nothing. I do not know what is wrong and why it is not working. I have the right IAM role and access to everything I need.
I hope you can help me!
The problem is mixup of promise, callback and async/await. You are also trying to do asynchronous operation inside foreach. The code should look something like this
"use strict";
const AWS = require("aws-sdk");
AWS.config.update({
region: "eu-west-1"
});
const docClient = new AWS.DynamoDB.DocumentClient();
const tableName = "Fair";
const s3 = new AWS.S3();
exports.handler = async event => {
var getParams = {
Bucket: "dataforfair", //s3 bucket name
Key: "fairData.json" //s3 file location
};
const data = await s3.getObject(getParams).promise();
//parse JSON
let fairInformations = JSON.parse(data.Body.toString());
await Promise.all(
fairInformations.map(fairInformationEntry => {
console.log(fairInformationEntry);
var params = {
TableName: tableName,
Item: {
year: fairInformationEntry.year,
fairName: fairInformationEntry.fairName,
info: fairInformationEntry.info
}
};
return docClient.put(params).promise();
})
);
const response = {
statusCode: 200,
body: JSON.stringify(data)
};
return response;
};
Hope this helps

Mocking serverless-mysql using sinon

I am trying to test an AWS lambda function (node.js) created using AWS SAM. My function uses the npm module serverless-mysql to connect to Aurora. The following are the relevant parts of my lambda function:
const connection = require('serverless-mysql')({
config: {
host : process.env.DB_HOST,
user : process.env.DB_USER,
password : process.env.DB_PASSWORD
}
});
exports.lambdaHandler = async (event, context) => {
try {
const name = event.pathParameters.name;
const rows = await connection.query('SELECT * FROM users WHERE name = ?', [name]);
await connection.end()
const user = rows[0];
return {
'statusCode': 200,
'body': JSON.stringify({
firstName: user.first_name,
lastName: user.last_name,
bk: user.bk,
team: user.current_team
})
}
} catch (err) {
console.log(err);
return err;
}
};
I am trying to tests this by mocking the serverless-mysql dependency, but I am currently unable to do so. My test looks like this:
const app = require('../../app.js');
const chai = require('chai');
const sinon = require('sinon');
const expect = chai.expect;
const event = {
pathParameters: {
name: 'johndoe'
}
}
var context;
var successConnectionObject = {
connect: function() {
return Promise.resolve();
},
query: function(sqlQuery, params) {
return Promise.resolve('');
},
end: function() {}
}
var mysql = require('serverless-mysql');
var stub = sinon.stub(mysql, 'connect').returns(successConnectionObject);
describe('Tests', function () {
it('verifies successful response', async () => {
const result = await app.lambdaHandler(event, context);
expect(result).to.be.an('object');
expect(result.statusCode).to.equal(200);
mock.verify();
mock.restore();
});
});
However, this returns the following error:
TypeError: Cannot stub non-existent own property connect
I believe this is because mysql is not instantiated. So I replaced the line var mysql = require('serverless-mysql'); with:
var mysql = require('serverless-mysql')();
Unfortunately, this results in the following error:
AssertionError: expected [Error: Error: self signed certificate in certificate chain] to be an object
So it seems the connect() method of the real serverless-mysql module is called.
How can I correctly mock serverless-mysql using sinon?
require('serverless-mysql') returns a function that returns a different value each time it is called, so mocking the properties on the result of one call won't affect the returned value of a different call.
That means you have to mock the function itself, which means mocking the entire module.
sinon doesn't provide a way to mock an entire module so you'll have to use something else for that part.
Here is a working test using proxyquire to mock the serverless-mysql module:
const chai = require('chai');
const sinon = require('sinon');
const expect = chai.expect;
const proxyquire = require('proxyquire');
const event = {
pathParameters: {
name: 'johndoe'
}
}
var context;
var successConnectionObject = {
connect: function () {
return Promise.resolve();
},
query: function (sqlQuery, params) {
return Promise.resolve([{
first_name: 'first',
last_name: 'last',
bk: 'bk',
current_team: 'team'
}]);
},
end: function () { }
}
const stub = sinon.stub().returns(successConnectionObject);
const app = proxyquire('../../app.js', { 'serverless-mysql': stub });
describe('Tests', function () {
it('verifies successful response', async () => {
const result = await app.lambdaHandler(event, context);
expect(result).to.be.an('object'); // Success!
expect(result.statusCode).to.equal(200); // Success!
sinon.assert.calledWithExactly(stub, {
config: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD
}
}); // Success!
});
});

Resources