Skip to content

Commit 4de844b

Browse files
test(request): add tests for Put- and TransactGetSingleTableRequest
1 parent 6a833af commit 4de844b

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/dynamo/request/put/put.request.spec.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import { PutRequest } from './put.request'
66

77
describe('put request', () => {
88
describe('params', () => {
9-
const request = new PutRequest(<any>null, SimpleWithPartitionKeyModel, {
10-
id: 'myId',
11-
age: 45,
9+
let request: PutRequest<SimpleWithPartitionKeyModel>
10+
11+
beforeEach(() => {
12+
request = new PutRequest(<any>null, SimpleWithPartitionKeyModel, {
13+
id: 'myId',
14+
age: 45,
15+
})
1216
})
1317

1418
it('constructor', () => {
@@ -28,6 +32,16 @@ describe('put request', () => {
2832
expect(params.ExpressionAttributeValues).toBeUndefined()
2933
})
3034

35+
it('ifNotExists with false does add the predicate', () => {
36+
// but it also does not remove it. it actually does nothing with false but returning the request instance
37+
request.ifNotExists(false)
38+
39+
const params: DynamoDB.PutItemInput = request.params
40+
expect(params.ConditionExpression).toBeUndefined()
41+
expect(params.ExpressionAttributeNames).toBeUndefined()
42+
expect(params.ExpressionAttributeValues).toBeUndefined()
43+
})
44+
3145
it('returnValues', () => {
3246
const req = request.returnValues('ALL_OLD')
3347
expect(req.params.ReturnValues).toEqual('ALL_OLD')

src/dynamo/request/put/put.request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class PutRequest<T> extends WriteRequest<T, DynamoDB.PutItemInput, Dynamo
3030

3131
/**
3232
* Adds a condition expression to the request, which makes sure the item will only be saved if the id does not exist
33+
* @param predicate if false is provided nothing happens (it does NOT remove the condition)
3334
*/
3435
ifNotExists(predicate: boolean = true): PutRequest<T> {
3536
if (predicate) {

src/dynamo/request/transactgetsingletable/transact-get-single-table.request.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// tslint:disable:no-non-null-assertion
12
import * as DynamoDB from 'aws-sdk/clients/dynamodb'
23
import { SimpleWithPartitionKeyModel } from '../../../../test/models'
34
import { metadataForModel } from '../../../decorator/metadata/metadata-for-model.function'
@@ -49,6 +50,10 @@ describe('TransactGetSingleTableRequest', () => {
4950
})
5051
})
5152

53+
it('execNoMap should return the raw value', async () => {
54+
expect(await req.execNoMap()).toEqual(response)
55+
})
56+
5257
it('execFullResponse should map items and potentially return consumed capacity', async () => {
5358
const resp = await req.execFullResponse()
5459
expect(resp).toBeDefined()
@@ -61,4 +66,25 @@ describe('TransactGetSingleTableRequest', () => {
6166
})
6267
})
6368
})
69+
70+
describe('with empty response', () => {
71+
const response: DynamoDB.TransactGetItemsOutput = {}
72+
const transactGetItemsSpy = jasmine.createSpy().and.returnValue(Promise.resolve(response))
73+
beforeEach(() => {
74+
const dynamoDBWrapperMock: DynamoDbWrapper = <any>{ transactGetItems: transactGetItemsSpy }
75+
req = new TransactGetSingleTableRequest(dynamoDBWrapperMock, SimpleWithPartitionKeyModel, [])
76+
})
77+
it('exec returns empty array', async () => {
78+
expect(await req.exec()).toEqual([])
79+
})
80+
it('execNoMap returns original response (empty object)', async () => {
81+
expect(await req.execNoMap()).toEqual({})
82+
})
83+
it('execFullResponse returns the response with empty items array', async () => {
84+
expect(await req.execFullResponse()).toEqual({
85+
ConsumedCapacity: undefined,
86+
Items: [],
87+
})
88+
})
89+
})
6490
})

0 commit comments

Comments
 (0)