1
+ const { AfterAll, Before, Given, Then, When } = require ( "@cucumber/cucumber" ) ;
2
+ const { DynamoDB } = require ( "../../../clients/client-dynamodb" ) ;
1
3
const jmespath = require ( "jmespath" ) ;
2
4
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 ;
26
6
27
- const { Before, Given, Then, When } = require ( "@cucumber/cucumber" ) ;
7
+ Before ( { tags : "@dynamodb" } , function ( ) {
8
+ this . service = new DynamoDB ( { maxRetries : 2 } ) ;
9
+ } ) ;
28
10
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
+ }
35
17
} ) ;
36
18
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" ) ;
38
25
const params = {
39
- TableName : world . tableName ,
26
+ TableName : tableName ,
40
27
AttributeDefinitions : [ { AttributeName : "id" , AttributeType : "S" } ] ,
41
28
KeySchema : [ { AttributeName : "id" , KeyType : "HASH" } ] ,
42
29
BillingMode : "PAY_PER_REQUEST" ,
43
30
} ;
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 ) ;
79
33
} ) ;
80
34
81
- When ( "I put the item:" , function ( string , next ) {
35
+ When ( "I put the item:" , async function ( string ) {
82
36
const params = { TableName : this . tableName , Item : JSON . parse ( string ) } ;
83
- this . request ( null , " putItem" , params , next ) ;
37
+ await this . service . putItem ( params ) ;
84
38
} ) ;
85
39
86
- When ( "I put a recursive item" , function ( next ) {
40
+ When ( "I put a recursive item" , async function ( ) {
87
41
const params = {
88
42
TableName : this . tableName ,
89
43
Item : {
@@ -100,30 +54,21 @@ When("I put a recursive item", function (next) {
100
54
} ,
101
55
} ,
102
56
} ;
103
- this . request ( null , "putItem" , params , next ) ;
57
+ this . data = await this . service . putItem ( params ) ;
104
58
} ) ;
105
59
106
- Then ( "the item with id {string} should exist" , function ( key , next ) {
60
+ Then ( "the item with id {string} should exist" , async function ( key ) {
107
61
const params = { TableName : this . tableName , Key : { id : { S : key } } } ;
108
- this . request ( null , "getItem" , params , next ) ;
62
+ this . data = await this . service . getItem ( params ) ;
109
63
} ) ;
110
64
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 ) {
112
66
this . assert . equal ( jmespath . search ( this . data . Item , attr ) , value ) ;
113
- next ( ) ;
114
67
} ) ;
115
68
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 } ) ;
127
72
} ) ;
128
73
129
74
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
180
125
callback ( ) ;
181
126
} ) ;
182
127
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 ) {
184
129
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
+ }
190
143
} ) ;
0 commit comments