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