@@ -2,6 +2,7 @@ package dynamodb
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"strconv"
6
7
"testing"
7
8
"time"
@@ -60,13 +61,13 @@ func Test_Batch(t *testing.T) {
60
61
delete := []dynamodbKey {ddbKeyDelete }
61
62
62
63
ddbClientMock := & mockDynamodb {
63
- batchWriteItem : func (input * dynamodb.BatchWriteItemInput ) * dynamodb.BatchWriteItemOutput {
64
+ batchWriteItem : func (input * dynamodb.BatchWriteItemInput ) ( * dynamodb.BatchWriteItemOutput , error ) {
64
65
require .NotNil (t , input .RequestItems [tableName ])
65
- require .True (t , len (input .RequestItems [tableName ]) == 2 )
66
+ require .EqualValues (t , 2 , len (input .RequestItems [tableName ]))
66
67
require .True (t ,
67
68
(checkPutRequestForItem (input .RequestItems [tableName ][0 ], ddbKeyUpdate ) || checkPutRequestForItem (input .RequestItems [tableName ][1 ], ddbKeyUpdate )) &&
68
69
(checkDeleteRequestForItem (input .RequestItems [tableName ][0 ], ddbKeyDelete ) || checkDeleteRequestForItem (input .RequestItems [tableName ][1 ], ddbKeyDelete )))
69
- return & dynamodb.BatchWriteItemOutput {}
70
+ return & dynamodb.BatchWriteItemOutput {}, nil
70
71
},
71
72
}
72
73
@@ -84,7 +85,7 @@ func Test_EmptyBatch(t *testing.T) {
84
85
require .NoError (t , err )
85
86
}
86
87
87
- func Test_Batch_Error (t * testing.T ) {
88
+ func Test_Batch_UnprocessedItems (t * testing.T ) {
88
89
tableName := "TEST"
89
90
ddbKeyDelete := dynamodbKey {
90
91
primaryKey : "PKDelete" ,
@@ -93,14 +94,14 @@ func Test_Batch_Error(t *testing.T) {
93
94
delete := []dynamodbKey {ddbKeyDelete }
94
95
95
96
ddbClientMock := & mockDynamodb {
96
- batchWriteItem : func (input * dynamodb.BatchWriteItemInput ) * dynamodb.BatchWriteItemOutput {
97
+ batchWriteItem : func (input * dynamodb.BatchWriteItemInput ) ( * dynamodb.BatchWriteItemOutput , error ) {
97
98
return & dynamodb.BatchWriteItemOutput {
98
99
UnprocessedItems : map [string ][]* dynamodb.WriteRequest {
99
100
tableName : {& dynamodb.WriteRequest {
100
101
PutRequest : & dynamodb.PutRequest {Item : generateItemKey (ddbKeyDelete )}},
101
102
},
102
103
},
103
- }
104
+ }, nil
104
105
},
105
106
}
106
107
@@ -109,6 +110,25 @@ func Test_Batch_Error(t *testing.T) {
109
110
require .Errorf (t , err , "error processing batch dynamodb" )
110
111
}
111
112
113
+ func Test_Batch_Error (t * testing.T ) {
114
+ tableName := "TEST"
115
+ ddbKeyDelete := dynamodbKey {
116
+ primaryKey : "PKDelete" ,
117
+ sortKey : "SKDelete" ,
118
+ }
119
+ delete := []dynamodbKey {ddbKeyDelete }
120
+
121
+ ddbClientMock := & mockDynamodb {
122
+ batchWriteItem : func (input * dynamodb.BatchWriteItemInput ) (* dynamodb.BatchWriteItemOutput , error ) {
123
+ return & dynamodb.BatchWriteItemOutput {}, fmt .Errorf ("mocked error" )
124
+ },
125
+ }
126
+
127
+ ddb := newDynamodbClientMock (tableName , ddbClientMock , 5 * time .Hour )
128
+ err := ddb .Batch (context .TODO (), nil , delete )
129
+ require .Errorf (t , err , "mocked error" )
130
+ }
131
+
112
132
func checkPutRequestForItem (request * dynamodb.WriteRequest , key dynamodbKey ) bool {
113
133
return request .PutRequest != nil &&
114
134
request .PutRequest .Item [primaryKey ] != nil &&
@@ -127,7 +147,7 @@ func checkDeleteRequestForItem(request *dynamodb.WriteRequest, key dynamodbKey)
127
147
128
148
type mockDynamodb struct {
129
149
putItem func (input * dynamodb.PutItemInput ) * dynamodb.PutItemOutput
130
- batchWriteItem func (input * dynamodb.BatchWriteItemInput ) * dynamodb.BatchWriteItemOutput
150
+ batchWriteItem func (input * dynamodb.BatchWriteItemInput ) ( * dynamodb.BatchWriteItemOutput , error )
131
151
132
152
dynamodbiface.DynamoDBAPI
133
153
}
@@ -137,7 +157,7 @@ func (m *mockDynamodb) PutItemWithContext(_ context.Context, input *dynamodb.Put
137
157
}
138
158
139
159
func (m * mockDynamodb ) BatchWriteItemWithContext (_ context.Context , input * dynamodb.BatchWriteItemInput , _ ... request.Option ) (* dynamodb.BatchWriteItemOutput , error ) {
140
- return m .batchWriteItem (input ), nil
160
+ return m .batchWriteItem (input )
141
161
}
142
162
143
163
func newDynamodbClientMock (tableName string , mock * mockDynamodb , ttl time.Duration ) * dynamodbKV {
0 commit comments