Skip to content

Commit

Permalink
test(core): add DynamoDBDocumentClient pagination mocking test
Browse files Browse the repository at this point in the history
  • Loading branch information
m-radzikowski committed Dec 31, 2023
1 parent beb1278 commit 06254dd
Showing 1 changed file with 76 additions and 43 deletions.
119 changes: 76 additions & 43 deletions packages/aws-sdk-client-mock/test/mockDynamoDB.test.ts
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'});
});
});

0 comments on commit 06254dd

Please sign in to comment.