Skip to content

Commit 92ace0b

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

File tree

2 files changed

+46
-98
lines changed

2 files changed

+46
-98
lines changed
Lines changed: 45 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,43 @@
1+
const { AfterAll, Before, Given, Then, When } = require("@cucumber/cucumber");
2+
const { DynamoDB } = require("../../../clients/client-dynamodb");
13
const jmespath = require("jmespath");
24

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-
}
5+
let tableName;
266

27-
const { Before, Given, Then, When } = require("@cucumber/cucumber");
7+
Before({ tags: "@dynamodb" }, function () {
8+
this.service = new DynamoDB({ maxRetries: 2 });
9+
});
2810

29-
Before({ tags: "@dynamodb" }, function (scenario, next) {
30-
const { DynamoDB } = require("../../../clients/client-dynamodb");
31-
this.service = new DynamoDB({
32-
maxRetries: 2,
33-
});
34-
next();
11+
AfterAll({ tags: "@dynamodb" }, async function () {
12+
if (tableName) {
13+
const client = new DynamoDB({ maxRetries: 2 });
14+
await client.deleteTable({ TableName: tableName });
15+
tableName = undefined;
16+
}
3517
});
3618

37-
function createTable(world, callback) {
19+
Given("I have a table", async function () {
20+
await this.service.describeTable({ TableName: this.tableName });
21+
});
22+
23+
When("I create a table", async function () {
24+
tableName = this.uniqueName("aws-sdk-js-integration");
3825
const params = {
39-
TableName: world.tableName,
26+
TableName: tableName,
4027
AttributeDefinitions: [{ AttributeName: "id", AttributeType: "S" }],
4128
KeySchema: [{ AttributeName: "id", KeyType: "HASH" }],
4229
BillingMode: "PAY_PER_REQUEST",
4330
};
44-
45-
world.service.createTable(params, function (err, data) {
46-
if (err) {
47-
callback(err);
48-
return;
49-
}
50-
waitForTableExistsCallback(world, callback);
51-
});
52-
}
53-
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-
});
65-
});
66-
67-
When("I create a table", function (callback) {
68-
const world = this;
69-
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-
});
31+
this.tableName = tableName;
32+
await this.service.createTable(params);
7933
});
8034

81-
When("I put the item:", function (string, next) {
35+
When("I put the item:", async function (string) {
8236
const params = { TableName: this.tableName, Item: JSON.parse(string) };
83-
this.request(null, "putItem", params, next);
37+
await this.service.putItem(params);
8438
});
8539

86-
When("I put a recursive item", function (next) {
40+
When("I put a recursive item", async function () {
8741
const params = {
8842
TableName: this.tableName,
8943
Item: {
@@ -100,30 +54,21 @@ When("I put a recursive item", function (next) {
10054
},
10155
},
10256
};
103-
this.request(null, "putItem", params, next);
57+
this.data = await this.service.putItem(params);
10458
});
10559

106-
Then("the item with id {string} should exist", function (key, next) {
60+
Then("the item with id {string} should exist", async function (key) {
10761
const params = { TableName: this.tableName, Key: { id: { S: key } } };
108-
this.request(null, "getItem", params, next);
62+
this.data = await this.service.getItem(params);
10963
});
11064

111-
Then("it should have attribute {string} containing {string}", function (attr, value, next) {
65+
Then("it should have attribute {string} containing {string}", async function (attr, value) {
11266
this.assert.equal(jmespath.search(this.data.Item, attr), value);
113-
next();
11467
});
11568

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);
69+
Then("the table should eventually exist", async function () {
70+
const { waitUntilTableExists } = require("../../../clients/client-dynamodb");
71+
await waitUntilTableExists({ client: this.service }, { TableName: this.tableName });
12772
});
12873

12974
Given("my first request is corrupted with CRC checking (ON|OFF)", function (toggle, callback) {
@@ -180,11 +125,19 @@ Then("the request should( not)? fail with a CRC checking error", function (faile
180125
callback();
181126
});
182127

183-
Given("I try to delete an item with key {string} from table {string}", function (key, table, callback) {
128+
Given("I try to delete an item with key {string} from table {string}", async function (key, table) {
184129
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);
130+
try {
131+
this.data = await this.service.deleteItem(params);
132+
} catch (error) {
133+
this.error = error;
134+
}
135+
});
136+
137+
Given("I try to delete a table with an empty table parameter", async function () {
138+
try {
139+
this.data = await this.service.deleteTable({ TableName: "" });
140+
} catch (error) {
141+
this.error = error;
142+
}
190143
});

features/dynamodb/tables.feature

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ Feature: DynamoDB Tables
3333
Scenario: Recursive Attributes
3434
Given I have a table
3535
When I put a recursive item
36-
Then the request should be successful
37-
And the item with id "fooRecursive" should exist
36+
Then the item with id "fooRecursive" should exist
3837
And it should have attribute "data.M.attr1.L[1].L[0].M.attr12.S" containing "value2"
3938

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

0 commit comments

Comments
 (0)