Skip to content

Instantly share code, notes, and snippets.

@petebarber
Created September 2, 2017 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petebarber/1bef78b9a6e98c9132a248a422922948 to your computer and use it in GitHub Desktop.
Save petebarber/1bef78b9a6e98c9132a248a422922948 to your computer and use it in GitHub Desktop.
'use strict';
const assert = require("assert");
const sinon = require("sinon");
const aws = require("aws-sdk");
const spawn = require("child_process").spawn;
aws.config.update({region: "us-east-1" });
describe("AWSLambdaToTest", function()
{
this.timeout(5000);
const testLambdaFns = require("../src/TestLambda.js");
let dynamoInstance;
let db;
before(function()
{
require("sleep").sleep(1);
dynamoInstance = spawn('/usr/local/bin/dynamodb-local', ['-inMemory']);
db = new aws.DynamoDB({ endpoint: "http://localhost:8000" });
});
after(() => dynamoInstance.kill());
const createTable = function()
{
var params =
{
TableName: "TestTable",
KeySchema: [{ AttributeName: "emailAddress", KeyType: "HASH" }],
AttributeDefinitions:
[
{ AttributeName: "emailAddress", AttributeType: "S" },
],
ProvisionedThroughput:
{
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
return db.createTable(params).promise();
}
const dropTable = function()
{
return db.deleteTable({ TableName: "TestTable"}).promise();
}
beforeEach(function()
{
return createTable()
.then(() => db.waitFor("tableExists", { TableName: "TestTable"}));
});
afterEach(function()
{
return dropTable()
.then(() => db.waitFor("tableNotExists", { TableName: "TestTable"}))
});
const ARBITRARY_EMAIL_ADDRESS = "fred@bloggs.com";
describe("When querying an empty db for a user", function()
{
it("Returns {}", function()
{
return testLambdaFns._test.findUserByEmailAddress(ARBITRARY_EMAIL_ADDRESS)
.then(res =>
{
assert(JSON.stringify(res) === "{}");
})
});
});
describe("When adding a user to an empty db", function()
{
it("Doesn't error - it can return nothing", function()
{
return testLambdaFns._test.addUser(ARBITRARY_EMAIL_ADDRESS);
});
});
describe("When finding a user", function()
{
it("Can be updated", function()
{
return testLambdaFns._test.addUser(ARBITRARY_EMAIL_ADDRESS)
.then(() => testLambdaFns._test.findUserByEmailAddress(ARBITRARY_EMAIL_ADDRESS))
.then(res =>
{
const info =
{
Name: "Fred",
FavouriteProgrammingBook: "The C Programming Language"
}
return testLambdaFns._test.updateUserInfo(res.Item, info)
})
.then(updatedUser =>
{
assert(updatedUser.version == 1);
});
});
});
describe("When valid request sent", function()
{
describe("ONE", function()
{
it("ONE", function() { assert(true); });
});
describe("for non-added user", function()
{
it("succeeds", function()
{
const req =
{
rpcVersion:1,
emailAddress:ARBITRARY_EMAIL_ADDRESS,
info:
{
Name:"Fred Blogs",
FavouriteProgrammingBook:"Modern C++ Design"
}
};
return testLambdaFns._test.updateOrAddUser(req)
.then(res =>
{
assert(res.emailAddress == ARBITRARY_EMAIL_ADDRESS);
});
});
});
describe("TWO", function()
{
it("TWO", function() { });
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment