Skip to content

Commit 615a59c

Browse files
author
Michael Wittwer
committed
feat(aws-sdk-v3): fix tests
1 parent 1a0507a commit 615a59c

19 files changed

+148
-150
lines changed

src/aws-sdk-v2.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ export type BatchWriteItemRequestMap = { [key: string]: WriteRequest[] }
1010
export type ExpressionAttributeNameMap = { [key: string]: string /* was: AttributeName */ }
1111
export type ExpressionAttributeValueMap = { [key: string]: AttributeValue /* was: AttributeValue */ }
1212

13+
export type AttributeMap = { [key: string]: AttributeValue }
14+
1315
export type Key = { [key: string]: AttributeValue }

src/dynamo/batchget/batch-get.request.spec.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// tslint:disable:no-non-null-assertion
22
import * as DynamoDB from '@aws-sdk/client-dynamodb'
3+
import { ReturnConsumedCapacity } from '@aws-sdk/client-dynamodb'
34
import { Organization, SimpleWithCompositePartitionKeyModel, SimpleWithPartitionKeyModel } from '../../../test/models'
45
import { toDb } from '../../mapper/mapper'
56
import { Attributes } from '../../mapper/type/attribute.type'
@@ -12,17 +13,17 @@ describe('batch get', () => {
1213

1314
describe('constructor', () => {
1415
it('use provided DynamoDB instance', () => {
15-
const dynamoDB = new DynamoDB.default()
16+
const dynamoDB = new DynamoDB.DynamoDB({})
1617
const batchGetRequest = new BatchGetRequest(dynamoDB)
1718
expect(batchGetRequest.dynamoDB).toBe(dynamoDB)
1819

19-
const batchGetRequest2 = new BatchGetRequest()
20+
const batchGetRequest2 = new BatchGetRequest(new DynamoDB.DynamoDB({}))
2021
expect(batchGetRequest2.dynamoDB).not.toBe(dynamoDB)
2122
})
2223
})
2324

2425
describe('params', () => {
25-
beforeEach(() => (request = new BatchGetRequest()))
26+
beforeEach(() => (request = new BatchGetRequest(new DynamoDB.DynamoDB({}))))
2627

2728
it('base params', () => {
2829
const params = request.params
@@ -48,13 +49,13 @@ describe('batch get', () => {
4849
})
4950

5051
it('returnConsumedCapacity', () => {
51-
request.returnConsumedCapacity('TOTAL')
52+
request.returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
5253
expect(request.params.ReturnConsumedCapacity).toBe('TOTAL')
5354
})
5455
})
5556

5657
describe('forModel', () => {
57-
beforeEach(() => (request = new BatchGetRequest()))
58+
beforeEach(() => (request = new BatchGetRequest(new DynamoDB.DynamoDB({}))))
5859

5960
it('should throw when same table is used 2 times', () => {
6061
request.forModel(SimpleWithPartitionKeyModel, [{ id: 'idVal' }])
@@ -132,7 +133,7 @@ describe('batch get', () => {
132133
const generatorMock = () => <any>{ next: nextSpyFn }
133134

134135
beforeEach(() => {
135-
request = new BatchGetRequest()
136+
request = new BatchGetRequest(new DynamoDB.DynamoDB({}))
136137
request.forModel(SimpleWithPartitionKeyModel, [jsItem1, jsItem2])
137138

138139
batchGetItemsMock = jest
@@ -205,7 +206,7 @@ describe('batch get', () => {
205206
beforeEach(() => {
206207
batchGetItemsMock = jest.fn().mockReturnValueOnce(Promise.resolve(sampleResponse))
207208
const dynamoDBWrapper: DynamoDbWrapper = <any>{ batchGetItems: batchGetItemsMock }
208-
request = new BatchGetRequest()
209+
request = new BatchGetRequest(new DynamoDB.DynamoDB({}))
209210
Object.assign(request, { dynamoDBWrapper })
210211
request.forModel(SimpleWithPartitionKeyModel, [{ id: 'idVal' }])
211212
})

src/dynamo/batchwrite/batch-write.request.spec.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as DynamoDB from '@aws-sdk/client-dynamodb'
2+
import { ReturnConsumedCapacity, ReturnItemCollectionMetrics } from '@aws-sdk/client-dynamodb'
23
import { ComplexModel, SimpleWithPartitionKeyModel } from '../../../test/models'
34
import { getTableName } from '../get-table-name.function'
45
import { BatchWriteRequest } from './batch-write.request'
@@ -8,31 +9,33 @@ describe('batchWriteRequest', () => {
89

910
describe('constructor', () => {
1011
it('should initialize params', () => {
11-
req = new BatchWriteRequest()
12+
req = new BatchWriteRequest(new DynamoDB.DynamoDB({}))
1213
expect(req.params.RequestItems).toBeDefined()
1314
expect(req.params.RequestItems).toEqual({})
1415
})
1516

1617
describe('use provided DynamoDB instance', () => {
17-
const dynamoDB = new DynamoDB.default()
18+
const dynamoDB = new DynamoDB.DynamoDB({})
1819
const batchWriteRequest = new BatchWriteRequest(dynamoDB)
1920
expect(batchWriteRequest.dynamoDB).toBe(dynamoDB)
2021

21-
const batchWriteRequest2 = new BatchWriteRequest()
22+
const batchWriteRequest2 = new BatchWriteRequest(new DynamoDB.DynamoDB({}))
2223
expect(batchWriteRequest2.dynamoDB).not.toBe(dynamoDB)
2324
})
2425
})
2526

2627
describe('returnConsumedCapacity', () => {
2728
it('should set params', () => {
28-
req = new BatchWriteRequest().returnConsumedCapacity('TOTAL')
29+
req = new BatchWriteRequest(new DynamoDB.DynamoDB({})).returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
2930
expect(req.params.ReturnConsumedCapacity).toBe('TOTAL')
3031
})
3132
})
3233

3334
describe('returnItemCollectionMetrics', () => {
3435
it('should set params', () => {
35-
req = new BatchWriteRequest().returnItemCollectionMetrics('SIZE')
36+
req = new BatchWriteRequest(new DynamoDB.DynamoDB({})).returnItemCollectionMetrics(
37+
ReturnItemCollectionMetrics.SIZE,
38+
)
3639
expect(req.params.ReturnItemCollectionMetrics).toBe('SIZE')
3740
})
3841
})
@@ -41,7 +44,7 @@ describe('batchWriteRequest', () => {
4144
const now = new Date()
4245

4346
it('should set params', () => {
44-
req = new BatchWriteRequest()
47+
req = new BatchWriteRequest(new DynamoDB.DynamoDB({}))
4548
.delete(SimpleWithPartitionKeyModel, [{ id: 'myId1' }])
4649
.delete(ComplexModel, [{ id: 'myId2', creationDate: now }])
4750

@@ -69,15 +72,17 @@ describe('batchWriteRequest', () => {
6972
const items: Array<Partial<SimpleWithPartitionKeyModel>> = [...new Array(30)].map((_, ix) => ({
7073
id: `mydId-${ix}`,
7174
}))
72-
expect(() => new BatchWriteRequest().delete(SimpleWithPartitionKeyModel, items)).toThrow()
75+
expect(() =>
76+
new BatchWriteRequest(new DynamoDB.DynamoDB({})).delete(SimpleWithPartitionKeyModel, items),
77+
).toThrow()
7378
})
7479
})
7580

7681
describe('put', () => {
7782
const now = new Date()
7883

7984
it('should set params', () => {
80-
req = new BatchWriteRequest()
85+
req = new BatchWriteRequest(new DynamoDB.DynamoDB({}))
8186
.put(SimpleWithPartitionKeyModel, [<any>{ id: 'myId1' }])
8287
.put(ComplexModel, [<any>{ id: 'myId2', creationDate: now }])
8388

@@ -98,7 +103,7 @@ describe('batchWriteRequest', () => {
98103

99104
it('should throw when too many items', () => {
100105
const items: SimpleWithPartitionKeyModel[] = [...new Array(30)].map((_, ix) => ({ id: `mydId-${ix}`, age: ix }))
101-
expect(() => new BatchWriteRequest().put(SimpleWithPartitionKeyModel, items)).toThrow()
106+
expect(() => new BatchWriteRequest(new DynamoDB.DynamoDB({})).put(SimpleWithPartitionKeyModel, items)).toThrow()
102107
})
103108
})
104109

@@ -116,7 +121,7 @@ describe('batchWriteRequest', () => {
116121
(_, ix) => <ComplexModel>{ id: `myId-${ix}`, creationDate: now },
117122
)
118123

119-
beforeEach(() => (req = new BatchWriteRequest()))
124+
beforeEach(() => (req = new BatchWriteRequest(new DynamoDB.DynamoDB({}))))
120125

121126
it('should add correct request items', () => {
122127
req.put(ComplexModel, complexItems).delete(SimpleWithPartitionKeyModel, [simpleItem])
@@ -144,7 +149,7 @@ describe('batchWriteRequest', () => {
144149
}
145150
batchWriteItemMock = jest.fn().mockReturnValueOnce(Promise.resolve(output))
146151
const dynamoDBWrapper = <any>{ batchWriteItem: batchWriteItemMock }
147-
req = new BatchWriteRequest()
152+
req = new BatchWriteRequest(new DynamoDB.DynamoDB({}))
148153
Object.assign(req, { dynamoDBWrapper })
149154
})
150155

src/dynamo/dynamo-db-wrapper.spec.ts

Lines changed: 36 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('dynamo rx', () => {
1717
pseudoParams = { TableName: 'tableName', KeyConditionExpression: 'blub' }
1818
sessionValidityEnsurerMock = jest.fn().mockReturnValueOnce(Promise.resolve())
1919
updateDynamoEasyConfig({ sessionValidityEnsurer: sessionValidityEnsurerMock })
20-
dynamoDBWrapper = new DynamoDbWrapper()
20+
dynamoDBWrapper = new DynamoDbWrapper(new DynamoDB.DynamoDB({}))
2121
})
2222

2323
afterEach(() => {
@@ -98,55 +98,48 @@ describe('dynamo rx', () => {
9898
})
9999
})
100100

101-
describe('makeRequest', () => {
102-
let dynamoDBWrapper: DynamoDbWrapper
103-
let sessionValidityEnsurerMock: jest.Mock
104-
let dynamoDBMock: jest.SpyInstance
105-
let pseudoParams: any
106-
107-
beforeEach(() => {
108-
pseudoParams = { TableName: 'tableName', KeyConditionExpression: 'blub' }
109-
sessionValidityEnsurerMock = jest.fn().mockReturnValueOnce(Promise.resolve(true))
110-
updateDynamoEasyConfig({ sessionValidityEnsurer: sessionValidityEnsurerMock })
111-
dynamoDBWrapper = new DynamoDbWrapper()
112-
})
113-
114-
it('should call the validity ensurer before each call and call the correct dynamoDB method', async () => {
115-
dynamoDBMock = jest
116-
.spyOn(dynamoDBWrapper.dynamoDB, 'makeRequest')
117-
.mockReturnValueOnce(<any>{ promise: () => Promise.resolve() })
118-
await dynamoDBWrapper.makeRequest('pseudoOperation', pseudoParams)
119-
expect(sessionValidityEnsurerMock).toHaveBeenCalled()
120-
expect(dynamoDBMock).toHaveBeenCalledTimes(1)
121-
expect(dynamoDBMock).toHaveBeenCalledWith('pseudoOperation', pseudoParams)
122-
})
123-
})
101+
// TODO v3: remove when decision is made if DynamoDbWrapper.makeRequest comes back or not
102+
// describe('makeRequest', () => {
103+
// let dynamoDBWrapper: DynamoDbWrapper
104+
// let sessionValidityEnsurerMock: jest.Mock
105+
// let dynamoDBMock: jest.SpyInstance
106+
// let pseudoParams: any
107+
//
108+
// beforeEach(() => {
109+
// pseudoParams = { TableName: 'tableName', KeyConditionExpression: 'blub' }
110+
// sessionValidityEnsurerMock = jest.fn().mockReturnValueOnce(Promise.resolve(true))
111+
// updateDynamoEasyConfig({ sessionValidityEnsurer: sessionValidityEnsurerMock })
112+
// dynamoDBWrapper = new DynamoDbWrapper(new DynamoDB.DynamoDB({}))
113+
// })
114+
//
115+
// it('should call the validity ensurer before each call and call the correct dynamoDB method', async () => {
116+
// dynamoDBMock = jest
117+
// .spyOn(dynamoDBWrapper.dynamoDB, 'makeRequest')
118+
// .mockReturnValueOnce(<any>{ promise: () => Promise.resolve() })
119+
// await dynamoDBWrapper.makeRequest('pseudoOperation', pseudoParams)
120+
// expect(sessionValidityEnsurerMock).toHaveBeenCalled()
121+
// expect(dynamoDBMock).toHaveBeenCalledTimes(1)
122+
// expect(dynamoDBMock).toHaveBeenCalledWith('pseudoOperation', pseudoParams)
123+
// })
124+
// })
124125

125126
describe('query', () => {
126127
beforeEach(() => {})
127128
it('should throw when no KeyConditionExpression was given', () => {
128-
const dynamoDBWrapper = new DynamoDbWrapper()
129+
const dynamoDBWrapper = new DynamoDbWrapper(new DynamoDB.DynamoDB({}))
129130
updateDynamoEasyConfig({ sessionValidityEnsurer: jest.fn().mockReturnValue(Promise.resolve()) })
130131
expect(() => dynamoDBWrapper.query({ TableName: 'tableName' })).toThrow()
131132
})
132133
})
133134

134-
it('should call makeRequest with the given params', async () => {
135-
const dynamoDBWrapper = new DynamoDbWrapper()
136-
const makeRequestMock = jest.fn().mockReturnValue({ promise: (args: any) => Promise.resolve(args) })
137-
Object.assign(dynamoDBWrapper, { dynamoDB: { makeRequest: makeRequestMock } })
138-
139-
await dynamoDBWrapper.makeRequest(<any>{ ok: true })
140-
expect(makeRequestMock).toHaveBeenCalled()
141-
expect(makeRequestMock.mock.calls.slice(-1)[0][0]).toEqual({ ok: true })
142-
})
143-
144-
it('should use given dynamoDB client', () => {
145-
const dynamoDB = new DynamoDB.default()
146-
const dynamoDBWrapper = new DynamoDbWrapper(dynamoDB)
147-
expect(dynamoDBWrapper.dynamoDB).toBe(dynamoDB)
148-
149-
const dynamoDBWrapper2 = new DynamoDbWrapper()
150-
expect(dynamoDBWrapper2.dynamoDB).not.toBe(dynamoDB)
151-
})
135+
// TODO v3: remove when decision is made if DynamoDbWrapper.makeRequest comes back or not
136+
// it('should call makeRequest with the given params', async () => {
137+
// const dynamoDBWrapper = new DynamoDbWrapper(new DynamoDB.DynamoDB({}))
138+
// const makeRequestMock = jest.fn().mockReturnValue({ promise: (args: any) => Promise.resolve(args) })
139+
// Object.assign(dynamoDBWrapper, { dynamoDB: { makeRequest: makeRequestMock } })
140+
//
141+
// await dynamoDBWrapper.makeRequest(<any>{ ok: true })
142+
// expect(makeRequestMock).toHaveBeenCalled()
143+
// expect(makeRequestMock.mock.calls.slice(-1)[0][0]).toEqual({ ok: true })
144+
// })
152145
})

src/dynamo/dynamo-store.spec.ts

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,15 @@ class DynamoStoreModel {}
2424
class DynamoStoreModel2 {}
2525

2626
describe('dynamo store', () => {
27+
let dynamoDB: DynamoDB.DynamoDB
28+
29+
beforeEach(() => {
30+
dynamoDB = new DynamoDB.DynamoDB({})
31+
})
32+
2733
describe('table name', () => {
2834
it('correct table name', () => {
29-
const store = new DynamoStore(DynamoStoreModel2)
35+
const store = new DynamoStore(DynamoStoreModel2, dynamoDB)
3036
expect(store.tableName).toBe('myTableName')
3137
})
3238
})
@@ -43,7 +49,7 @@ describe('dynamo store', () => {
4349
afterEach(resetDynamoEasyConfig)
4450

4551
it('custom session validity ensurer is used', async () => {
46-
const store = new DynamoStore(DynamoStoreModel)
52+
const store = new DynamoStore(DynamoStoreModel, dynamoDB)
4753
try {
4854
await store.scan().exec()
4955
} catch (error) {
@@ -60,7 +66,7 @@ describe('dynamo store', () => {
6066
updateDynamoEasyConfig({ logReceiver: logReceiverMock })
6167
})
6268
it('logs when instance was created', () => {
63-
new DynamoStore(DynamoStoreModel)
69+
new DynamoStore(DynamoStoreModel, dynamoDB)
6470
expect(logReceiverMock).toHaveBeenCalled()
6571
})
6672
})
@@ -69,7 +75,7 @@ describe('dynamo store', () => {
6975
let store: DynamoStore<SimpleWithPartitionKeyModel>
7076

7177
beforeEach(() => {
72-
store = new DynamoStore(SimpleWithPartitionKeyModel)
78+
store = new DynamoStore(SimpleWithPartitionKeyModel, dynamoDB)
7379
})
7480

7581
it('put', () => expect(store.put({ id: 'id', age: 0 }) instanceof PutRequest).toBeTruthy())
@@ -84,25 +90,17 @@ describe('dynamo store', () => {
8490
expect(store.transactGet([{ id: 'myId' }]) instanceof TransactGetSingleTableRequest).toBeTruthy())
8591
})
8692

87-
describe('should enable custom requests', () => {
88-
const makeRequestSpy = jest.fn().mockReturnValue(Promise.resolve())
89-
const store = new DynamoStore(SimpleWithPartitionKeyModel)
90-
Object.assign(store, { dynamoDBWrapper: { makeRequest: makeRequestSpy } })
91-
store.makeRequest('updateTimeToLive', {})
92-
expect(makeRequestSpy).toBeCalled()
93-
})
93+
// TODO v3: possibly remove when we decided on how to proceed with DynamoDbWrapper.makeRequest
94+
// xdescribe('should enable custom requests', () => {
95+
// const makeRequestSpy = jest.fn().mockReturnValue(Promise.resolve())
96+
// const store = new DynamoStore(SimpleWithPartitionKeyModel, dynamoDB)
97+
// Object.assign(store, { dynamoDBWrapper: { makeRequest: makeRequestSpy } })
98+
// store.makeRequest('updateTimeToLive', {})
99+
// expect(makeRequestSpy).toBeCalled()
100+
// })
94101

95102
describe('allow to get dynamoDB instance', () => {
96-
const store = new DynamoStore(SimpleWithPartitionKeyModel)
103+
const store = new DynamoStore(SimpleWithPartitionKeyModel, new DynamoDB.DynamoDB({}))
97104
expect(store.dynamoDB).toBeDefined()
98105
})
99-
100-
describe('use provided dynamoDB instance', () => {
101-
const dynamoDB = new DynamoDB.default()
102-
const store = new DynamoStore(SimpleWithPartitionKeyModel, dynamoDB)
103-
expect(store.dynamoDB).toBe(dynamoDB)
104-
105-
const store2 = new DynamoStore(SimpleWithPartitionKeyModel)
106-
expect(store2.dynamoDB).not.toBe(dynamoDB)
107-
})
108106
})

src/dynamo/request/base.request.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// tslint:disable:max-classes-per-file
2+
import { ReturnConsumedCapacity } from '@aws-sdk/client-dynamodb'
23
import { SimpleWithPartitionKeyModel } from '../../../test/models'
34
import { ModelConstructor } from '../../model/model-constructor'
45
import { BaseRequest } from './base.request'
@@ -57,11 +58,11 @@ describe('base request', () => {
5758
request = new TestRequest(SimpleWithPartitionKeyModel)
5859
})
5960
it('should set param', () => {
60-
request.returnConsumedCapacity('TOTAL')
61+
request.returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
6162
expect(request.params.ReturnConsumedCapacity).toBe('TOTAL')
6263
})
6364
it('should return request instance', () => {
64-
expect(request.returnConsumedCapacity('TOTAL')).toBe(request)
65+
expect(request.returnConsumedCapacity(ReturnConsumedCapacity.TOTAL)).toBe(request)
6566
})
6667
})
6768
})

0 commit comments

Comments
 (0)