Skip to content

Commit

Permalink
Support DocumentClient loading from library as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Aug 29, 2024
1 parent f2606a6 commit 602420e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/dynamoDbSafe.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ class DynamoDB extends DynamoDbOriginal.DocumentClient {
}
}

// Allow reference to DynamoDb using the legacy DocumentClient
DynamoDB.DocumentClient = DynamoDB;

module.exports = { DynamoDB };
58 changes: 58 additions & 0 deletions tests/dynamoDbArmor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,64 @@ describe('dynamoDbArmor.js', () => {
}
});

it('Validate single SET using inner DocumentClient', async () => {
const testTable = 'Test-TableId';
const testHash = 'testHash';
const testRange = 'testRange';
const params = {
TableName: testTable,
Key: {
hash: testHash,
rang: testRange
},
UpdateExpression: 'SET #key = :value',
ConditionExpression: 'attribute_exists(hash)',
ExpressionAttributeNames: {
'#key': 'key'
},
ExpressionAttributeValues: {
':value': 'value'
}
};
try {
const dynamoDbOriginalMock = sandbox.mock(DynamoDbOriginal.DocumentClient.prototype);
dynamoDbOriginalMock.expects('update').once().returns({ promise() { return Promise.resolve(); } });
await new DynamoDB.DocumentClient().update(params);
dynamoDbOriginalMock.verify();
} catch (error) {
expect(error.message).to.eql(null, JSON.stringify(error.message, null, 2));
}
});

it('Validate single SET using inner DocumentClient and unnecessary .promise()', async () => {
const testTable = 'Test-TableId';
const testHash = 'testHash';
const testRange = 'testRange';
const params = {
TableName: testTable,
Key: {
hash: testHash,
rang: testRange
},
UpdateExpression: 'SET #key = :value',
ConditionExpression: 'attribute_exists(hash)',
ExpressionAttributeNames: {
'#key': 'key'
},
ExpressionAttributeValues: {
':value': 'value'
}
};
try {
const dynamoDbOriginalMock = sandbox.mock(DynamoDbOriginal.DocumentClient.prototype);
dynamoDbOriginalMock.expects('update').once().returns({ promise() { return Promise.resolve(); } });
await new DynamoDB.DocumentClient().update(params).promise();
dynamoDbOriginalMock.verify();
} catch (error) {
expect(error.message).to.eql(null, JSON.stringify(error.message, null, 2));
}
});

it('Validate multi SET', async () => {
const testTable = 'Test-TableId';
const testHash = 'testHash';
Expand Down

0 comments on commit 602420e

Please sign in to comment.