-
-
Save rclark/ff22c330f8d6791cb0bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var test = require('tape'); | |
var AWS = require('aws-sdk'); | |
var Dynalite = require('dynalite'); | |
var crypto = require('crypto'); | |
var dynalite = Dynalite({ | |
createTableMs: 0, | |
updateTableMs: 0, | |
deleteTableMs: 0 | |
}); | |
var dynamo = new AWS.DynamoDB({ | |
region: 'fake', | |
table: 'rclark-test', | |
endpoint: 'http://localhost:4567', | |
accessKeyId: 'fake', | |
secretAccessKey: 'fake' | |
}); | |
// Running the same test against a live table does not replicate the problem | |
// var dynamo = new AWS.DynamoDB({ | |
// region: 'us-east-1' | |
// }); | |
test('start dynalite', function(assert) { | |
dynalite.listen(4567, function(err) { | |
if (err) throw err; | |
assert.end(); | |
}); | |
}); | |
test('create table', function(assert) { | |
dynamo.createTable({ | |
TableName: 'rclark-test', | |
AttributeDefinitions: [ | |
{ AttributeName: 'dataset', AttributeType: 'S' }, | |
{ AttributeName: 'id', AttributeType: 'S' } | |
], | |
KeySchema: [ | |
{ AttributeName: 'dataset', KeyType: 'HASH' }, | |
{ AttributeName: 'id', KeyType: 'RANGE' } | |
], | |
ProvisionedThroughput: { | |
ReadCapacityUnits: 10, | |
WriteCapacityUnits: 10 | |
} | |
}, function(err) { | |
if (err) throw err; | |
setTimeout(function() { | |
assert.end(); | |
}, 60000); | |
}) | |
}); | |
test('insert features', function(assert) { | |
function randomItem() { | |
return { | |
dataset: { S: 'dataset' }, | |
id: { S: 'id!' + crypto.randomBytes(16).toString('hex') }, | |
data: { B: crypto.randomBytes(1200) } | |
}; | |
} | |
var features = []; | |
for (var i = 0; i < 1000; i++) features.push(randomItem()); | |
(function batch() { | |
var items = features.splice(0, 25).map(function(item) { | |
return { PutRequest: { Item: item } }; | |
}); | |
var params = { RequestItems: { 'rclark-test': items } }; | |
dynamo.batchWriteItem(params, function(err, data) { | |
if (err) throw err; | |
if (features.length) return batch(); | |
assert.end(); | |
}); | |
})(); | |
}); | |
test('scan', function(assert) { | |
var items = 0; | |
(function scan(start) { | |
var params = { TableName: 'rclark-test' }; | |
if (start) params.ExclusiveStartKey = start; | |
dynamo.scan(params, function(err, data) { | |
if (err) throw err; | |
items += data.Items.length; | |
if (data.LastEvaluatedKey) return scan(data.LastEvaluatedKey); | |
assert.equal(items, 1000, 'scan includes all results'); | |
assert.end(); | |
}); | |
})(); | |
}); | |
test('query', function(assert) { | |
var query = { dataset: { EQ: 'dataset' }, id: { BEGINS_WITH: 'id!' } }; | |
var opts = { pages: 0 }; | |
var items = 0; | |
(function query(start) { | |
var params = { | |
TableName: 'rclark-test', | |
KeyConditions: { | |
dataset: { | |
ComparisonOperator: 'EQ', | |
AttributeValueList: [{ S: 'dataset' }] | |
}, | |
id: { | |
ComparisonOperator: 'BEGINS_WITH', | |
AttributeValueList: [{ S: 'id!' }] | |
} | |
} | |
}; | |
if (start) params.ExclusiveStartKey = start; | |
dynamo.query(params, function(err, data) { | |
if (err) throw err; | |
items += data.Items.length; | |
if (items < 1000) assert.ok(data.LastEvaluatedKey, 'page contains LastEvaluatedKey'); | |
if (data.LastEvaluatedKey) return query(data.LastEvaluatedKey); | |
assert.equal(items, 1000, 'query includes all results'); | |
assert.end(); | |
}); | |
})(); | |
}); | |
test('close dynalite', function(assert) { | |
dynalite.close(function(err) { | |
if (err) throw err; | |
assert.end(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment