Skip to content

Commit

Permalink
cover waiting for table creation
Browse files Browse the repository at this point in the history
  • Loading branch information
autopulated committed Feb 6, 2024
1 parent 8bd3014 commit b668fe1
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions test/table.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const t = require('tap');
const { DescribeTableCommand } = require('@aws-sdk/client-dynamodb');

const clientOptions = {
endpoint: 'http://localhost:8000'
Expand Down Expand Up @@ -65,6 +66,34 @@ t.test('table initialisation', async t => {
});
});

t.test('waiting for table creation', async t => {
const table = DynamoDM.Table({ name: 'test-table-slow-creation', clientOptions});
table.model(DynamoDM.Schema('emptySchema'));

const originalSend = table.client.send;
let callNumber = 0;

// to test slow table creation we have to mock the client send command,
// because dynamodb-local always creates tables instantly.
const commandSendResults = t.capture(table.docClient, 'send', async function(command){
if (command instanceof DescribeTableCommand) {
callNumber += 1;
// return a dummy 'CREATING' response for the first DescribeTableCommand call
if (callNumber < 2) {
return {
Table: { TableStatus: 'CREATING' }
};
}
}
// eslint-disable-next-line
return originalSend.apply(this, arguments);
});

await table.ready();

t.equal(commandSendResults().length, 3, 'Should wait for success.');
});

t.test('table consistency:', async t => {
t.test('throws on inconsistent id field names', async t => {
const table = DynamoDM.Table({ name: 'test-table-errors'});
Expand Down Expand Up @@ -173,11 +202,11 @@ t.test('table consistency:', async t => {
t.test('throws on invalid table name', async t => {
t.throws(() => {
DynamoDM.Table({ name: '1'});
}, {message:`Invalid table name "1": Must be between 3 and 255 characters long, and may contain only the characters a-z, A-Z, 0-9, '_', '-', and '.'.`}, 'invalid table name as option');
}, {message:'Invalid table name "1": Must be between 3 and 255 characters long, and may contain only the characters a-z, A-Z, 0-9, \'_\', \'-\', and \'.\'.'}, 'invalid table name as option');

t.throws(() => {
DynamoDM.Table('1');
}, {message:`Invalid table name "1": Must be between 3 and 255 characters long, and may contain only the characters a-z, A-Z, 0-9, '_', '-', and '.'.`}, 'invalid table name as argument');
}, {message:'Invalid table name "1": Must be between 3 and 255 characters long, and may contain only the characters a-z, A-Z, 0-9, \'_\', \'-\', and \'.\'.'}, 'invalid table name as argument');

t.throws(() => {
DynamoDM.Table();
Expand Down

0 comments on commit b668fe1

Please sign in to comment.