Skip to content

Commit

Permalink
Merge pull request #20 from blazejkustra/fix/empty-batchGet
Browse files Browse the repository at this point in the history
Handle empty input for batch methods
  • Loading branch information
blazejkustra authored Jan 23, 2024
2 parents 84aaf86 + c33ee81 commit daa7523
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/entity/entityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ export default function EntityManager<M extends Metadata<E>, E extends typeof En
}

return (async () => {
if (primaryKeys.length === 0) {
return {
items: [],
unprocessedKeys: [],
};
}

const result = await Dynamode.ddb.get().batchGetItem(commandInput);

if (options?.return === 'output') {
Expand Down Expand Up @@ -382,6 +389,13 @@ export default function EntityManager<M extends Metadata<E>, E extends typeof En
}

return (async () => {
if (items.length === 0) {
return {
items: [],
unprocessedItems: [],
};
}

const result = await Dynamode.ddb.get().batchWriteItem(commandInput);

if (options?.return === 'output') {
Expand Down Expand Up @@ -438,6 +452,12 @@ export default function EntityManager<M extends Metadata<E>, E extends typeof En
}

return (async () => {
if (primaryKeys.length === 0) {
return {
unprocessedItems: [],
};
}

const result = await Dynamode.ddb.get().batchWriteItem(commandInput);

if (options?.return === 'output') {
Expand Down
8 changes: 8 additions & 0 deletions tests/e2e/entity/batchDelete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe('EntityManager.batchDelete', () => {
});

describe.sequential('MockEntityManager', () => {
test('Should return empty array if an empty array is passed', async () => {
// Act
const { unprocessedItems } = await MockEntityManager.batchDelete([]);

// Assert
expect(unprocessedItems).toEqual([]);
});

test('Should be able to delete multiple items', async () => {
// Arrange
const mock1 = mockEntityFactory({ partitionKey: 'PK1', sortKey: 'SK1' });
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/entity/batchGet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ describe('EntityManager.batchGet', () => {
});

describe.sequential('MockEntityManager', () => {
test('Should return empty arrays if an empty array is passed', async () => {
// Act
const { items: mocks, unprocessedKeys } = await MockEntityManager.batchGet([]);

// Assert
expect(mocks).toEqual([]);
expect(unprocessedKeys).toEqual([]);
});

test('Should be able to retrieve multiple items', async () => {
// Arrange
const mock1 = mockEntityFactory({ partitionKey: 'PK1', sortKey: 'SK1' });
Expand Down
9 changes: 9 additions & 0 deletions tests/e2e/entity/batchPut.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ describe('EntityManager.batchPut', () => {
});

describe.sequential('MockEntityManager', () => {
test('Should return empty arrays if an empty array is passed', async () => {
// Act
const { items: mocks, unprocessedItems } = await MockEntityManager.batchPut([]);

// Assert
expect(mocks).toEqual([]);
expect(unprocessedItems).toEqual([]);
});

test('Should be able to retrieve multiple items', async () => {
// Arrange
const mock1 = mockEntityFactory({ partitionKey: 'PK1', sortKey: 'SK1' });
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/entity/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,18 @@ describe('entityManager', () => {
expect(convertAttributeValuesToEntitySpy).not.toBeCalled();
});

test('Should return dynamode result of batch get with empty array input', async () => {
await expect(MockEntityManager.batchGet([])).resolves.toEqual({
items: [],
unprocessedKeys: [],
});

expect(buildGetProjectionExpressionSpy).toBeCalledTimes(1);
expect(convertPrimaryKeyToAttributeValuesSpy).toBeCalledTimes(0);
expect(batchGetItem).toBeCalledTimes(0);
expect(convertAttributeValuesToEntitySpy).toBeCalledTimes(0);
});

test('Should return dynamode result (all items found)', async () => {
buildGetProjectionExpressionSpy.mockReturnValue({});
batchGetItem.mockResolvedValue({
Expand Down Expand Up @@ -1073,6 +1085,17 @@ describe('entityManager', () => {
expect(convertAttributeValuesToEntitySpy).not.toBeCalled();
});

test('Should return dynamode result of batch put with empty array input', async () => {
await expect(MockEntityManager.batchPut([])).resolves.toEqual({
items: [],
unprocessedItems: [],
});

expect(convertEntityToAttributeValuesSpy).toBeCalledTimes(0);
expect(batchWriteMock).toBeCalledTimes(0);
expect(convertAttributeValuesToEntitySpy).toBeCalledTimes(0);
});

test('Should return dynamode result of batch put item with no unprocessed items', async () => {
batchWriteMock.mockResolvedValue({ UnprocessedItems: undefined });
convertAttributeValuesToEntitySpy.mockImplementation((_, item) => item as any);
Expand Down Expand Up @@ -1255,6 +1278,16 @@ describe('entityManager', () => {
expect(fromDynamoSpy).not.toBeCalled();
});

test('Should return dynamode result of batch delete with empty array input', async () => {
await expect(MockEntityManager.batchDelete([])).resolves.toEqual({
unprocessedItems: [],
});

expect(convertPrimaryKeyToAttributeValuesSpy).toBeCalledTimes(0);
expect(batchWriteMock).toBeCalledTimes(0);
expect(fromDynamoSpy).toBeCalledTimes(0);
});

test('Should return dynamode result of batch delete item with no unprocessed items', async () => {
batchWriteMock.mockResolvedValue({ UnprocessedItems: undefined });

Expand Down

0 comments on commit daa7523

Please sign in to comment.