Skip to content

Instantly share code, notes, and snippets.

@sansmischevia
Created January 8, 2013 20:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sansmischevia/4487787 to your computer and use it in GitHub Desktop.
Save sansmischevia/4487787 to your computer and use it in GitHub Desktop.
A mock dynamo client that helps unit testing.
var DATABASE = {};
/**
* Start awk-sdk mock
*/
exports.getItem = function(params, cb) {
if (params.AttributesToGet) {
var ret = {
Item: {}
};
if (DATABASE[params.Key.HashKeyElement.S]) {
for (var a in params.AttributesToGet) {
var p = params.AttributesToGet[a];
ret.Item[p] = DATABASE[params.Key.HashKeyElement.S][p];
}
cb(null, ret);
}
else {
return cb(null, null);
}
}
else {
cb(null, { Item: DATABASE[params.Key.HashKeyElement.S]});
}
};
exports.putItem = function(params, cb) {
DATABASE[params.Item.isu.S] = params.Item;
cb(null, {});
};
exports.deleteItem = function(params, cb) {
delete DATABASE[params.Key.HashKeyElement.S];
cb(null, {});
};
exports.updateItem = function(params, cb) {
var isu = params.Key.HashKeyElement.S;
for (var i in params.AttributeUpdates) {
if (params.AttributeUpdates[i].Action === "ADD") {
// Add to existing value
if (!DATABASE[isu]) {
DATABASE[isu] = {
isu: { S: isu }
};
}
var incBy = parseInt(params.AttributeUpdates[i].Value.N, 10);
if (!DATABASE[isu][i]) {
DATABASE[isu][i] = { "N" : "0" };
}
var old = parseInt(DATABASE[isu][i].N, 10);
DATABASE[isu][i] = { "N": (old + incBy).toString()};
}
else if (params.AttributeUpdates[i].Action === "PUT") {
// Overwrite
if (!DATABASE[isu]) {
DATABASE[isu] = {
isu: { S: isu }
};
}
DATABASE[isu][i] = params.AttributeUpdates[i].Value;
}
}
cb(null, {ConsumedCapacityUnits: 1});
};
/**
* Start jed dynamo library mock
*/
exports.get = function get(table, obj) {
};
exports.DATABASE = DATABASE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment