Skip to content

Commit ec5e1c3

Browse files
committed
Replace fromCallback with aws .promise() in dynamodb-streams-readable ♻️
1 parent 9b3daa6 commit ec5e1c3

File tree

1 file changed

+45
-53
lines changed
  • packages/dynamodb-streams-readable/test

1 file changed

+45
-53
lines changed

packages/dynamodb-streams-readable/test/index.js

Lines changed: 45 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,21 @@ const DynamoDB = require('aws-sdk/clients/dynamodb');
44
const DynamoDBStreams = require('aws-sdk/clients/dynamodbstreams');
55
const DynamoDBStreamReadable = require('..');
66

7-
const fromCallback = fun =>
8-
new Promise((resolve, reject) => {
9-
fun((err, data) => (err ? reject(err) : resolve(data)));
7+
const delay = timeout =>
8+
new Promise(resolve => {
9+
setTimeout(resolve, timeout);
1010
});
11-
const wait = duration => fromCallback(cb => setTimeout(cb, duration));
11+
1212
const batchWriteItem = (dynamodb, tableName, items) =>
13-
fromCallback(cb =>
14-
dynamodb.batchWriteItem(
15-
{
16-
RequestItems: {
17-
[tableName]: items.map(document => ({
18-
PutRequest: document
19-
}))
20-
}
21-
},
22-
cb
23-
)
24-
);
13+
dynamodb
14+
.batchWriteItem({
15+
RequestItems: {
16+
[tableName]: items.map(document => ({
17+
PutRequest: document
18+
}))
19+
}
20+
})
21+
.promise();
2522

2623
test.before(t => {
2724
t.context.dynamodb = new DynamoDB({
@@ -44,34 +41,32 @@ test.beforeEach(async t => {
4441
const tableName = uuid();
4542
t.context.tableName = tableName;
4643

47-
const table = await fromCallback(cb =>
48-
dynamodb.createTable(
49-
{
50-
TableName: tableName,
51-
AttributeDefinitions: [
52-
{
53-
AttributeName: 'Id',
54-
AttributeType: 'S'
55-
}
56-
],
57-
KeySchema: [
58-
{
59-
AttributeName: 'Id',
60-
KeyType: 'HASH'
61-
}
62-
],
63-
StreamSpecification: {
64-
StreamEnabled: true,
65-
StreamViewType: 'NEW_AND_OLD_IMAGES'
66-
},
67-
ProvisionedThroughput: {
68-
ReadCapacityUnits: 1,
69-
WriteCapacityUnits: 1
44+
const table = await dynamodb
45+
.createTable({
46+
TableName: tableName,
47+
AttributeDefinitions: [
48+
{
49+
AttributeName: 'Id',
50+
AttributeType: 'S'
7051
}
52+
],
53+
KeySchema: [
54+
{
55+
AttributeName: 'Id',
56+
KeyType: 'HASH'
57+
}
58+
],
59+
StreamSpecification: {
60+
StreamEnabled: true,
61+
StreamViewType: 'NEW_AND_OLD_IMAGES'
7162
},
72-
cb
73-
)
74-
);
63+
ProvisionedThroughput: {
64+
ReadCapacityUnits: 1,
65+
WriteCapacityUnits: 1
66+
}
67+
})
68+
.promise();
69+
7570
t.context.table = table;
7671
});
7772

@@ -96,15 +91,12 @@ test.serial('reads records that already exist', async t => {
9691
await batchWriteItem(dynamodb, tableName, documents);
9792

9893
t.deepEqual(
99-
await fromCallback(cb =>
100-
dynamodb.scan(
101-
{
102-
TableName: tableName,
103-
Select: 'COUNT'
104-
},
105-
cb
106-
)
107-
),
94+
await dynamodb
95+
.scan({
96+
TableName: tableName,
97+
Select: 'COUNT'
98+
})
99+
.promise(),
108100
{Count: documents.length, ScannedCount: documents.length}
109101
);
110102

@@ -173,7 +165,7 @@ test.serial('reads ongoing records', t => {
173165
reject(err);
174166
});
175167
}),
176-
wait(100).then(() => batchWriteItem(dynamodb, tableName, documents))
168+
delay(100).then(() => batchWriteItem(dynamodb, tableName, documents))
177169
]);
178170
});
179171

@@ -234,7 +226,7 @@ test.serial('reads latest records', async t => {
234226
reject(err);
235227
});
236228
}),
237-
wait(100).then(() => batchWriteItem(dynamodb, tableName, subsequentDocuments))
229+
delay(100).then(() => batchWriteItem(dynamodb, tableName, subsequentDocuments))
238230
]);
239231
});
240232

0 commit comments

Comments
 (0)