Skip to content

Commit 6045a21

Browse files
committed
chore: fix integration test
1 parent cbcaf53 commit 6045a21

File tree

5 files changed

+96
-91
lines changed

5 files changed

+96
-91
lines changed

__tests__/integration/dynamodb/multiple-integrations/service/serverless.yml

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,43 @@ plugins:
1212
custom:
1313
apiGatewayServiceProxies:
1414
- dynamodb:
15-
path: /dynamodb/{id}
15+
path: /dynamodb/{id}/{sort}
1616
method: put
1717
tableName:
1818
Ref: MyMuTestTable
1919
hashKey:
2020
pathParam: id
2121
attributeType: S
22+
rangeKey:
23+
pathParam: sort
24+
attributeType: S
25+
action: PutItem
2226
cors: true
2327
- dynamodb:
2428
path: /dynamodb
2529
method: get
2630
tableName:
2731
Ref: MyMuTestTable
32+
action: GetItem
2833
hashKey:
2934
queryStringParam: id
3035
attributeType: S
31-
cors: true
32-
- dynamodb:
33-
path: /dynamodb/{id}
34-
method: post
35-
action: UpdateItem
36-
tableName:
37-
Ref: MyMuTestTable
38-
hashKey:
39-
pathParam: id
36+
rangeKey:
37+
queryStringParam: sort
4038
attributeType: S
4139
cors: true
4240
- dynamodb:
4341
path: /dynamodb/{id}
4442
method: delete
4543
tableName:
4644
Ref: MyMuTestTable
45+
action: DeleteItem
4746
hashKey:
4847
pathParam: id
4948
attributeType: S
49+
rangeKey:
50+
queryStringParam: sort
51+
attributeType: S
5052
cors: true
5153

5254
resources:
@@ -56,13 +58,15 @@ resources:
5658
Properties:
5759
TableName: MyMuTestTable
5860
AttributeDefinitions:
59-
-
60-
AttributeName: id
61+
- AttributeName: id
62+
AttributeType: S
63+
- AttributeName: sort
6164
AttributeType: S
6265
KeySchema:
63-
-
64-
AttributeName: id
66+
- AttributeName: id
6567
KeyType: HASH
68+
- AttributeName: sort
69+
KeyType: RANGE
6670
ProvisionedThroughput:
6771
ReadCapacityUnits: 1
6872
WriteCapacityUnits: 1

__tests__/integration/dynamodb/multiple-integrations/tests.js

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const _ = require('lodash')
66
const {
77
deployWithRandomStage,
88
removeService,
9-
getDynamodbItem,
9+
getDynamodbItemWithHashKeyAndRangeKey,
1010
putDynamodbItem,
1111
cleanUpDynamodbItems
1212
} = require('../../../utils')
@@ -16,7 +16,9 @@ describe('Multiple Dynamodb Proxies Integration Test', () => {
1616
let stage
1717
const tableName = 'MyMuTestTable'
1818
const hashKeyAttribute = 'id'
19+
const rangeKeyAttribute = 'sort'
1920
const hashKey = { S: 'myid' }
21+
const sortKey = { S: 'mykey' }
2022
const config = '__tests__/integration/dynamodb/multiple-integrations/service/serverless.yml'
2123

2224
beforeAll(async () => {
@@ -31,11 +33,11 @@ describe('Multiple Dynamodb Proxies Integration Test', () => {
3133
})
3234

3335
afterEach(async () => {
34-
await cleanUpDynamodbItems(tableName, hashKeyAttribute)
36+
await cleanUpDynamodbItems(tableName, hashKeyAttribute, rangeKeyAttribute)
3537
})
3638

3739
it('should get correct response from dynamodb PutItem action endpoint', async () => {
38-
const putEndpoint = `${endpoint}/dynamodb/${hashKey.S}`
40+
const putEndpoint = `${endpoint}/dynamodb/${hashKey.S}/${sortKey.S}`
3941

4042
const putResponse = await fetch(putEndpoint, {
4143
method: 'PUT',
@@ -45,10 +47,17 @@ describe('Multiple Dynamodb Proxies Integration Test', () => {
4547
expect(putResponse.headers.get('access-control-allow-origin')).to.deep.equal('*')
4648
expect(putResponse.status).to.be.equal(200)
4749

48-
const item = await getDynamodbItem(tableName, hashKeyAttribute, hashKey)
50+
const item = await getDynamodbItemWithHashKeyAndRangeKey(
51+
tableName,
52+
hashKeyAttribute,
53+
hashKey,
54+
rangeKeyAttribute,
55+
sortKey
56+
)
4957
expect(item).to.be.deep.equal({
5058
Item: {
5159
[hashKeyAttribute]: hashKey,
60+
[rangeKeyAttribute]: sortKey,
5261
message: { S: 'test' }
5362
}
5463
})
@@ -57,9 +66,13 @@ describe('Multiple Dynamodb Proxies Integration Test', () => {
5766
it('should get correct response from dynamodb GetItem action endpoint', async () => {
5867
await putDynamodbItem(
5968
tableName,
60-
_.merge({}, { [hashKeyAttribute]: hashKey }, { message: { S: 'testtest' } })
69+
_.merge(
70+
{},
71+
{ [hashKeyAttribute]: hashKey, [rangeKeyAttribute]: sortKey },
72+
{ message: { S: 'testtest' } }
73+
)
6174
)
62-
const getEndpoint = `${endpoint}/dynamodb?id=${hashKey.S}`
75+
const getEndpoint = `${endpoint}/dynamodb?id=${hashKey.S}&sort=${sortKey.S}`
6376

6477
const getResponse = await fetch(getEndpoint, {
6578
method: 'GET',
@@ -71,67 +84,21 @@ describe('Multiple Dynamodb Proxies Integration Test', () => {
7184
const item = await getResponse.json()
7285
expect(item).to.be.deep.equal({
7386
id: hashKey.S,
87+
sort: sortKey.S,
7488
message: 'testtest'
7589
})
7690
})
7791

78-
it('should get correct response from dynamodb UpdateItem action endpoint', async () => {
79-
await putDynamodbItem(
80-
tableName,
81-
_.merge({}, { [hashKeyAttribute]: hashKey }, { message: { S: 'testtesttest' } })
82-
)
83-
const updateEndpoint = `${endpoint}/dynamodb/${hashKey.S}`
84-
85-
const updateResponse1 = await fetch(updateEndpoint, {
86-
method: 'POST',
87-
headers: { 'Content-Type': 'application/json' },
88-
body: JSON.stringify({
89-
UpdateExpression: 'ADD QuantityOnHand :q',
90-
ExpressionAttributeValues: { ':q': { N: '5' } }
91-
})
92-
})
93-
expect(updateResponse1.headers.get('access-control-allow-origin')).to.deep.equal('*')
94-
expect(updateResponse1.status).to.be.equal(200)
95-
96-
const item1 = await getDynamodbItem(tableName, hashKeyAttribute, hashKey)
97-
expect(item1).to.be.deep.equal({
98-
Item: {
99-
[hashKeyAttribute]: hashKey,
100-
QuantityOnHand: { N: '5' },
101-
message: { S: 'testtesttest' }
102-
}
103-
})
104-
105-
const updateResponse2 = await fetch(updateEndpoint, {
106-
method: 'POST',
107-
headers: { 'Content-Type': 'application/json' },
108-
body: JSON.stringify({
109-
UpdateExpression: 'SET #n = :newName',
110-
ExpressionAttributeValues: { ':newName': { S: 'myname' } },
111-
ExpressionAttributeNames: {
112-
'#n': 'name'
113-
}
114-
})
115-
})
116-
expect(updateResponse2.headers.get('access-control-allow-origin')).to.deep.equal('*')
117-
expect(updateResponse2.status).to.be.equal(200)
118-
const item2 = await getDynamodbItem(tableName, hashKeyAttribute, hashKey)
119-
expect(item2).to.be.deep.equal({
120-
Item: {
121-
[hashKeyAttribute]: hashKey,
122-
QuantityOnHand: { N: '5' },
123-
name: { S: 'myname' },
124-
message: { S: 'testtesttest' }
125-
}
126-
})
127-
})
128-
12992
it('should get correct response from dynamodb DeleteItem action endpoint', async () => {
13093
await putDynamodbItem(
13194
tableName,
132-
_.merge({}, { [hashKeyAttribute]: hashKey }, { message: { S: 'test' } })
95+
_.merge(
96+
{},
97+
{ [hashKeyAttribute]: hashKey, [rangeKeyAttribute]: sortKey },
98+
{ message: { S: 'test' } }
99+
)
133100
)
134-
const deleteEndpoint = `${endpoint}/dynamodb/${hashKey.S}`
101+
const deleteEndpoint = `${endpoint}/dynamodb/${hashKey.S}?sort=${sortKey.S}`
135102

136103
const deleteResponse = await fetch(deleteEndpoint, {
137104
method: 'DELETE',
@@ -140,7 +107,13 @@ describe('Multiple Dynamodb Proxies Integration Test', () => {
140107
expect(deleteResponse.headers.get('access-control-allow-origin')).to.deep.equal('*')
141108
expect(deleteResponse.status).to.be.equal(200)
142109

143-
const item = await getDynamodbItem(tableName, hashKeyAttribute, hashKey)
110+
const item = await getDynamodbItemWithHashKeyAndRangeKey(
111+
tableName,
112+
hashKeyAttribute,
113+
hashKey,
114+
rangeKeyAttribute,
115+
sortKey
116+
)
144117
expect(item).to.be.empty
145118
})
146119
})

__tests__/integration/dynamodb/single-integration/service/serverless.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ plugins:
1212
custom:
1313
apiGatewayServiceProxies:
1414
- dynamodb:
15-
path: /dynamodb
15+
path: /dynamodb/{id}
1616
method: post
1717
tableName:
1818
Ref: MyTestTable
19-
hashKey: 'id'
19+
action: PutItem
20+
hashKey:
21+
pathParam: 'id'
22+
attributeType: 'S'
2023
cors: true
2124

2225
resources:
@@ -26,12 +29,10 @@ resources:
2629
Properties:
2730
TableName: MyTestTable
2831
AttributeDefinitions:
29-
-
30-
AttributeName: id
32+
- AttributeName: id
3133
AttributeType: S
3234
KeySchema:
33-
-
34-
AttributeName: id
35+
- AttributeName: id
3536
KeyType: HASH
3637
ProvisionedThroughput:
3738
ReadCapacityUnits: 1

__tests__/integration/dynamodb/single-integration/tests.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
const expect = require('chai').expect
44
const fetch = require('node-fetch')
5-
const { deployWithRandomStage, removeService, getDynamodbItem } = require('../../../utils')
5+
const {
6+
deployWithRandomStage,
7+
removeService,
8+
getDynamodbItemWithHashKey
9+
} = require('../../../utils')
610

711
describe('Single dynamodb Proxy Integration Test', () => {
812
let endpoint
@@ -23,7 +27,7 @@ describe('Single dynamodb Proxy Integration Test', () => {
2327
})
2428

2529
it('should get correct response from dynamodb proxy endpoint', async () => {
26-
const testEndpoint = `${endpoint}/dynamodb`
30+
const testEndpoint = `${endpoint}/dynamodb/id`
2731

2832
const response = await fetch(testEndpoint, {
2933
method: 'POST',
@@ -32,12 +36,11 @@ describe('Single dynamodb Proxy Integration Test', () => {
3236
})
3337
expect(response.headers.get('access-control-allow-origin')).to.deep.equal('*')
3438
expect(response.status).to.be.equal(200)
35-
const body = await response.json()
3639

37-
const item = await getDynamodbItem(tableName, hashKeyAttribute, { S: body.id })
40+
const item = await getDynamodbItemWithHashKey(tableName, hashKeyAttribute, { S: 'id' })
3841
expect(item).to.be.deep.equal({
3942
Item: {
40-
id: { S: body.id },
43+
id: { S: 'id' },
4144
item1: { S: 'item1' },
4245
item2: { S: 'item2' }
4346
}

__tests__/utils.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function getStackOutputs(stackName) {
2626
return _.zipObject(keys, values)
2727
}
2828

29-
async function getDynamodbItem(tableName, hashKeyAttribute, hashKey) {
29+
async function getDynamodbItemWithHashKey(tableName, hashKeyAttribute, hashKey) {
3030
return await dynamodb
3131
.getItem({
3232
Key: {
@@ -37,6 +37,24 @@ async function getDynamodbItem(tableName, hashKeyAttribute, hashKey) {
3737
.promise()
3838
}
3939

40+
async function getDynamodbItemWithHashKeyAndRangeKey(
41+
tableName,
42+
hashKeyAttribute,
43+
hashKey,
44+
rangeKeyAttribute,
45+
rangeKey
46+
) {
47+
return await dynamodb
48+
.getItem({
49+
Key: {
50+
[hashKeyAttribute]: hashKey,
51+
[rangeKeyAttribute]: rangeKey
52+
},
53+
TableName: tableName
54+
})
55+
.promise()
56+
}
57+
4058
async function putDynamodbItem(tableName, item) {
4159
await dynamodb
4260
.putItem({
@@ -46,16 +64,21 @@ async function putDynamodbItem(tableName, item) {
4664
.promise()
4765
}
4866

49-
async function cleanUpDynamodbItems(tableName, hashKeyAttribute) {
67+
async function cleanUpDynamodbItems(tableName, hashKeyAttribute, rangeKeyAttribute) {
5068
const items = await dynamodb.scan({ TableName: tableName }).promise()
5169
if (items.Count > 0) {
5270
await Promise.all(
5371
items.Items.map(async (item) => {
72+
const key = {
73+
[hashKeyAttribute]: item[hashKeyAttribute]
74+
}
75+
76+
if (rangeKeyAttribute) {
77+
key[rangeKeyAttribute] = item[rangeKeyAttribute]
78+
}
5479
await dynamodb
5580
.deleteItem({
56-
Key: {
57-
[hashKeyAttribute]: item[hashKeyAttribute]
58-
},
81+
Key: key,
5982
TableName: tableName
6083
})
6184
.promise()
@@ -117,7 +140,8 @@ module.exports = {
117140
deployWithRandomStage,
118141
getS3Object,
119142
deleteS3Object,
120-
getDynamodbItem,
143+
getDynamodbItemWithHashKey,
144+
getDynamodbItemWithHashKeyAndRangeKey,
121145
putDynamodbItem,
122146
cleanUpDynamodbItems
123147
}

0 commit comments

Comments
 (0)