Skip to content

Commit

Permalink
chore(tests): Add some tests for Dynamo updateItem command
Browse files Browse the repository at this point in the history
  • Loading branch information
DerDackel committed Mar 12, 2023
1 parent 93be847 commit 3a66efa
Showing 1 changed file with 158 additions and 2 deletions.
160 changes: 158 additions & 2 deletions test/dynamodb/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PrimaryEntity } from '../../src/dynamodb';
interface TestEntry extends PrimaryEntity<`Partition#${string}`, `Sort#${string}`> {
content: string;
}

describe('A dynamodb client', () => {
const testTableName = process.env.TABLE;

Expand Down Expand Up @@ -126,6 +127,161 @@ describe('A dynamodb client', () => {
});
});

//TODO Test item updates
test('UpdateItem (existing)', async () => {
client.on(commands.UpdateCommand).resolves({
Attributes: { content: 'updated' },
});

const result = await dynamo.updateExistingItem<TestEntry>('Partition#foo', 'Sort#bar', {
content: 'updated',
});

// Just making sure, we're actually returning what was passed from the dynamo client
expect(result).toBeDefined();
expect(result!.content).toEqual('updated');

expect(client).toHaveReceivedCommandWith(commands.UpdateCommand, {
TableName: testTableName,
Key: {
PK: 'Partition#foo',
SK: 'Sort#bar',
},
ConditionExpression: 'attribute_exists(PK) and attribute_exists(SK)',
ExpressionAttributeNames: { '#content': 'content' },
ExpressionAttributeValues: { ':content': 'updated' },
ReturnValues: 'ALL_NEW',
UpdateExpression: 'SET #content = :content ',
});
});

type TestSetup = {
testName: string;
given: {
Key: {
PK: string;
SK: string;
};
UpdateExpression?: string;
item: {[key: string]: any};
};
expected: commands.UpdateCommandInput;
}

test.each<TestSetup>([
{
testName: 'simple update',
given: {
Key: {
PK: 'Partition#simple',
SK: 'Sort#update',
},
item: {
content: 'updated',
},
},
expected: {
TableName: testTableName,
Key: {
PK: 'Partition#simple',
SK: 'Sort#update',
},
ExpressionAttributeNames: { '#content': 'content' },
ExpressionAttributeValues: { ':content': 'updated' },
UpdateExpression: 'SET #content = :content ',
},
},
{
testName: 'multiple values/types',
given: {
Key: {
PK: 'Partition#multiple',
SK: 'Sort#fields',
},
item: {
someValue: 'updated',
otherValue: 42,
},
},
expected: {
TableName: testTableName,
Key: {
PK: 'Partition#multiple',
SK: 'Sort#fields',
},
ExpressionAttributeNames: {
'#otherValue': 'otherValue',
'#someValue': 'someValue',
},
ExpressionAttributeValues: {
':otherValue': 42,
':someValue': 'updated',
},
UpdateExpression: 'SET #someValue = :someValue, #otherValue = :otherValue ',
},
},
{
testName: 'remove field',
given: {
Key: {
PK: 'Partition#remove',
SK: 'Sort#field',
},
item: {
content: 'stay',
removeMe: undefined,
},
},
expected: {
TableName: testTableName,
Key: {
PK: 'Partition#remove',
SK: 'Sort#field',
},
ExpressionAttributeNames: {
'#content': 'content',
'#removeMe': 'removeMe',
},
ExpressionAttributeValues: {
':content': 'stay',
},
UpdateExpression: 'SET #content = :content REMOVE #removeMe ',
},
},
{
testName: 'partial request',
given: {
Key: {
PK: 'Partition#partial',
SK: 'Sort#request',
},
UpdateExpression: 'I AM THE UPDATE EXPRESSION',
item: {
content: 'someContent',
},
},
expected: {
TableName: testTableName,
Key: {
PK: 'Partition#partial',
SK: 'Sort#request',
},
ExpressionAttributeNames: {
'#content': 'content',
},
ExpressionAttributeValues: {
':content': 'someContent',
},
UpdateExpression: 'SET #content = :content I AM THE UPDATE EXPRESSION',
},
},
])('createUpdate ($testName)', ({ given, expected }) => {
const result = dynamo.createUpdate<TestEntry>({
Key: given.Key,
UpdateExpression: given.UpdateExpression,
}, given.item);

expect(result.input).toEqual(expected);
});
});
});
})
;

0 comments on commit 3a66efa

Please sign in to comment.