Skip to content

Commit 1142be8

Browse files
committed
fix(e2e): delete DynamoDB table in After call
1 parent d8e6a9a commit 1142be8

File tree

2 files changed

+42
-92
lines changed

2 files changed

+42
-92
lines changed
Lines changed: 42 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,44 @@
1+
const { After, Before, Given, Then, When } = require("@cucumber/cucumber");
12
const jmespath = require("jmespath");
23

3-
function waitForTableExistsCallback(world, callback) {
4-
const { waitForTableExists } = require("../../../clients/client-dynamodb");
5-
waitForTableExists({ client: world.service }, { TableName: world.tableName }).then(
6-
function (data) {
7-
callback();
8-
},
9-
function (err) {
10-
callback(err);
11-
}
12-
);
13-
}
14-
15-
function waitForTableNotExistsWithCallback(world, callback) {
16-
const { waitForTableNotExists } = require("../../../clients/client-dynamodb");
17-
waitForTableNotExists({ client: world.service }, { TableName: world.tableName }).then(
18-
function (data) {
19-
callback();
20-
},
21-
function (err) {
22-
callback(err);
23-
}
24-
);
25-
}
26-
27-
const { Before, Given, Then, When } = require("@cucumber/cucumber");
28-
29-
Before({ tags: "@dynamodb" }, function (scenario, next) {
4+
Before({ tags: "@dynamodb" }, function () {
305
const { DynamoDB } = require("../../../clients/client-dynamodb");
31-
this.service = new DynamoDB({
32-
maxRetries: 2,
33-
});
34-
next();
6+
this.service = new DynamoDB({ maxRetries: 2 });
357
});
368

37-
function createTable(world, callback) {
9+
After({ tags: "@dynamodb" }, async function () {
10+
if (this.tableName) {
11+
const { waitUntilTableNotExists } = require("../../../clients/client-dynamodb");
12+
await this.service.deleteTable({ TableName: this.tableName });
13+
await waitUntilTableNotExists({ client: this.service }, { TableName: this.tableName });
14+
}
15+
});
16+
17+
async function createTable(dynamodbClient, tableName) {
3818
const params = {
39-
TableName: world.tableName,
19+
TableName: tableName,
4020
AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
4121
KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
4222
BillingMode: "PAY_PER_REQUEST",
4323
};
44-
45-
world.service.createTable(params, function (err, data) {
46-
if (err) {
47-
callback(err);
48-
return;
49-
}
50-
waitForTableExistsCallback(world, callback);
51-
});
24+
await dynamodbClient.createTable(params);
5225
}
5326

54-
Given("I have a table", function (callback) {
55-
const world = this;
56-
this.service.listTables({}, function (err, data) {
57-
for (let i = 0; i < data.TableNames.length; i++) {
58-
if (data.TableNames[i] == world.tableName) {
59-
callback();
60-
return;
61-
}
62-
}
63-
createTable(world, callback);
64-
});
27+
Given("I have a table", async function () {
28+
await this.service.describeTable({ TableName: this.tableName });
6529
});
6630

67-
When("I create a table", function (callback) {
68-
const world = this;
31+
When("I create a table", async function () {
6932
this.tableName = this.uniqueName("aws-sdk-js-integration");
70-
this.service.listTables({}, function (err, data) {
71-
for (let i = 0; i < data.TableNames.length; i++) {
72-
if (data.TableNames[i] == world.tableName) {
73-
callback();
74-
return;
75-
}
76-
}
77-
createTable(world, callback);
78-
});
33+
await createTable(this.service, this.tableName);
7934
});
8035

81-
When("I put the item:", function (string, next) {
36+
When("I put the item:", async function (string) {
8237
const params = { TableName: this.tableName, Item: JSON.parse(string) };
83-
this.request(null, "putItem", params, next);
38+
await this.service.putItem(params);
8439
});
8540

86-
When("I put a recursive item", function (next) {
41+
When("I put a recursive item", async function () {
8742
const params = {
8843
TableName: this.tableName,
8944
Item: {
@@ -100,30 +55,21 @@ When("I put a recursive item", function (next) {
10055
},
10156
},
10257
};
103-
this.request(null, "putItem", params, next);
58+
this.data = await this.service.putItem(params);
10459
});
10560

106-
Then("the item with id {string} should exist", function (key, next) {
61+
Then("the item with id {string} should exist", async function (key) {
10762
const params = { TableName: this.tableName, Key: { id: { S: key } } };
108-
this.request(null, "getItem", params, next);
63+
this.data = await this.service.getItem(params);
10964
});
11065

111-
Then("it should have attribute {string} containing {string}", function (attr, value, next) {
66+
Then("it should have attribute {string} containing {string}", async function (attr, value) {
11267
this.assert.equal(jmespath.search(this.data.Item, attr), value);
113-
next();
11468
});
11569

116-
When("I delete the table", function (next) {
117-
const params = { TableName: this.tableName };
118-
this.request(null, "deleteTable", params, next);
119-
});
120-
121-
Then("the table should eventually exist", function (callback) {
122-
waitForTableExistsCallback(this, callback);
123-
});
124-
125-
Then("the table should eventually not exist", function (callback) {
126-
waitForTableNotExistsWithCallback(this, callback);
70+
Then("the table should eventually exist", async function () {
71+
const { waitUntilTableExists } = require("../../../clients/client-dynamodb");
72+
await waitUntilTableExists({ client: this.service }, { TableName: this.tableName });
12773
});
12874

12975
Given("my first request is corrupted with CRC checking (ON|OFF)", function (toggle, callback) {
@@ -180,11 +126,19 @@ Then("the request should( not)? fail with a CRC checking error", function (faile
180126
callback();
181127
});
182128

183-
Given("I try to delete an item with key {string} from table {string}", function (key, table, callback) {
129+
Given("I try to delete an item with key {string} from table {string}", async function (key, table) {
184130
const params = { TableName: table, Key: { id: { S: key } } };
185-
this.request(null, "deleteItem", params, callback, false);
186-
});
187-
188-
Given("I try to delete a table with an empty table parameter", function (callback) {
189-
this.request(null, "deleteTable", { TableName: "" }, callback, false);
131+
try {
132+
this.data = await this.service.deleteItem(params);
133+
} catch (error) {
134+
this.error = error;
135+
}
136+
});
137+
138+
Given("I try to delete a table with an empty table parameter", async function () {
139+
try {
140+
this.data = await this.service.deleteTable({ TableName: "" });
141+
} catch (error) {
142+
this.error = error;
143+
}
190144
});

features/dynamodb/tables.feature

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ Feature: DynamoDB Tables
3737
And the item with id "fooRecursive" should exist
3838
And it should have attribute "data.M.attr1.L[1].L[0].M.attr12.S" containing "value2"
3939

40-
Scenario: Deleting the created table
41-
When I delete the table
42-
Then the table should eventually not exist
43-
4440
# @dynamodb @crc32
4541
# Feature: CRC32 response validation
4642
# Scenario: Retry on corrupted request

0 commit comments

Comments
 (0)