-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): add DynamoDBDocumentClient pagination mocking test
- Loading branch information
1 parent
beb1278
commit 06254dd
Showing
1 changed file
with
76 additions
and
43 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,94 @@ | ||
import {mockClient} from '../src'; | ||
import {DynamoDBClient, paginateQuery, QueryCommand} from '@aws-sdk/client-dynamodb'; | ||
import {DynamoDBDocumentClient, QueryCommand as DocumentQueryCommand} from '@aws-sdk/lib-dynamodb'; | ||
import { | ||
DynamoDBDocumentClient, paginateQuery as documentPaginateQuery, QueryCommand as DocumentQueryCommand, | ||
} from '@aws-sdk/lib-dynamodb'; | ||
import {marshall} from '@aws-sdk/util-dynamodb'; | ||
|
||
it('mocks classic DynamoDB client', async () => { | ||
const mock = mockClient(DynamoDBClient); | ||
mock.on(QueryCommand).resolves({ | ||
Items: [marshall({pk: 'a', sk: 'b'})], | ||
describe('regular DynamoDB Client', () => { | ||
it('mocks client', async () => { | ||
const mock = mockClient(DynamoDBClient); | ||
mock.on(QueryCommand).resolves({ | ||
Items: [marshall({pk: 'a', sk: 'b'})], | ||
}); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const query = await dynamodb.send(new QueryCommand({ | ||
TableName: 'mock', | ||
})); | ||
|
||
expect(query.Items).toHaveLength(1); | ||
expect(query.Items?.[0]).toStrictEqual({pk: {S: 'a'}, sk: {S: 'b'}}); | ||
}); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const query = await dynamodb.send(new QueryCommand({ | ||
TableName: 'mock', | ||
})); | ||
it('mocks paginated operation', async () => { | ||
const mock = mockClient(DynamoDBClient); | ||
mock.on(QueryCommand, {TableName: 'mock', ExclusiveStartKey: undefined, Limit: 1}).resolves({ | ||
Items: [ | ||
marshall({pk: 'a', sk: 'b'}), | ||
], | ||
LastEvaluatedKey: marshall({pk: 'a', sk: 'b'}), | ||
}).on(QueryCommand, {TableName: 'mock', ExclusiveStartKey: marshall({pk: 'a', sk: 'b'}), Limit: 1}).resolves({ | ||
Items: [ | ||
marshall({pk: 'c', sk: 'd'}), | ||
], | ||
}); | ||
|
||
expect(query.Items).toHaveLength(1); | ||
expect(query.Items?.[0]).toStrictEqual({pk: {S: 'a'}, sk: {S: 'b'}}); | ||
}); | ||
const dynamodb = new DynamoDBClient({}); | ||
const paginator = paginateQuery({client: dynamodb, pageSize: 1}, {TableName: 'mock'}); | ||
|
||
it('mocks DynamoDB DocumentClient', async () => { | ||
const mock = mockClient(DynamoDBDocumentClient); | ||
mock.on(DocumentQueryCommand).resolves({ | ||
Items: [{pk: 'a', sk: 'b'}], | ||
const items = []; | ||
for await (const page of paginator) { | ||
items.push(...page.Items || []); | ||
} | ||
|
||
expect(items).toHaveLength(2); | ||
expect(items?.[1]).toStrictEqual({pk: {S: 'c'}, sk: {S: 'd'}}); | ||
}); | ||
}); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const ddb = DynamoDBDocumentClient.from(dynamodb); | ||
describe('DynamoDB DocumentClient', () => { | ||
it('mocks client', async () => { | ||
const mock = mockClient(DynamoDBDocumentClient); | ||
mock.on(DocumentQueryCommand).resolves({ | ||
Items: [{pk: 'a', sk: 'b'}], | ||
}); | ||
|
||
const query = await ddb.send(new DocumentQueryCommand({ | ||
TableName: 'mock', | ||
})); | ||
const dynamodb = new DynamoDBClient({}); | ||
const ddb = DynamoDBDocumentClient.from(dynamodb); | ||
|
||
expect(query.Items).toHaveLength(1); | ||
expect(query.Items?.[0]).toStrictEqual({pk: 'a', sk: 'b'}); | ||
}); | ||
const query = await ddb.send(new DocumentQueryCommand({ | ||
TableName: 'mock', | ||
})); | ||
|
||
it('mocks paginated operation', async () => { | ||
const mock = mockClient(DynamoDBClient); | ||
mock.on(QueryCommand, {TableName: 'mock', ExclusiveStartKey: undefined, Limit: 1}).resolves({ | ||
Items: [ | ||
marshall({pk: 'a', sk: 'b'}), | ||
], | ||
LastEvaluatedKey: marshall({pk: 'a', sk: 'b'}), | ||
}).on(QueryCommand, {TableName: 'mock', ExclusiveStartKey: marshall({pk: 'a', sk: 'b'}), Limit: 1}).resolves({ | ||
Items: [ | ||
marshall({pk: 'c', sk: 'd'}), | ||
], | ||
expect(query.Items).toHaveLength(1); | ||
expect(query.Items?.[0]).toStrictEqual({pk: 'a', sk: 'b'}); | ||
}); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const paginator = paginateQuery({client: dynamodb, pageSize: 1}, {TableName: 'mock'}); | ||
it('mocks paginated operation', async () => { | ||
const mock = mockClient(DynamoDBDocumentClient); | ||
mock.on(DocumentQueryCommand, {TableName: 'mock', ExclusiveStartKey: undefined, Limit: 1}).resolves({ | ||
Items: [ | ||
{pk: 'a', sk: 'b'}, | ||
], | ||
LastEvaluatedKey: {pk: 'a', sk: 'b'}, | ||
}).on(DocumentQueryCommand, {TableName: 'mock', ExclusiveStartKey: {pk: 'a', sk: 'b'}, Limit: 1}).resolves({ | ||
Items: [ | ||
{pk: 'c', sk: 'd'}, | ||
], | ||
}); | ||
|
||
const dynamodb = new DynamoDBClient({}); | ||
const ddb = DynamoDBDocumentClient.from(dynamodb); | ||
|
||
const items = []; | ||
for await (const page of paginator) { | ||
items.push(...page.Items || []); | ||
} | ||
const paginator = documentPaginateQuery({client: ddb, pageSize: 1}, {TableName: 'mock'}); | ||
|
||
expect(items).toHaveLength(2); | ||
expect(items?.[1]).toStrictEqual({pk: {S: 'c'}, sk: {S: 'd'}}); | ||
const items = []; | ||
for await (const page of paginator) { | ||
items.push(...page.Items || []); | ||
} | ||
|
||
expect(items).toHaveLength(2); | ||
expect(items?.[1]).toStrictEqual({pk: 'c', sk: 'd'}); | ||
}); | ||
}); |