@@ -183,6 +183,49 @@ func TestEncryptedClient_PutItem(t *testing.T) {
183
183
mockCMProvider .AssertExpectations (t )
184
184
}
185
185
186
+ func TestEncryptedClient_PutItem_Failure (t * testing.T ) {
187
+ mockDynamoDBClient := new (MockDynamoDBClient )
188
+ mockCMProvider := new (MockCryptographicMaterialsProvider )
189
+ encryptedClient := NewEncryptedClient (mockDynamoDBClient , mockCMProvider )
190
+
191
+ // Mock the DescribeTable call to simulate fetching table primary key schema.
192
+ mockDynamoDBClient .On ("DescribeTable" , mock .Anything , mock .AnythingOfType ("*dynamodb.DescribeTableInput" ), mock .Anything ).Return (& dynamodb.DescribeTableOutput {
193
+ Table : & types.TableDescription {
194
+ KeySchema : []types.KeySchemaElement {
195
+ {AttributeName : aws .String ("PK" ), KeyType : types .KeyTypeHash },
196
+ {AttributeName : aws .String ("SK" ), KeyType : types .KeyTypeRange },
197
+ },
198
+ },
199
+ }, nil )
200
+
201
+ // Mock the TableName call if your implementation requires it.
202
+ mockCMProvider .On ("TableName" ).Return ("materials-table" ).Maybe ()
203
+
204
+ mockCMProvider .On ("EncryptionMaterials" , mock .Anything , mock .Anything ).Return (materials .NewEncryptionMaterials (
205
+ map [string ]string {"mock" : "data" },
206
+ & MockDelegatedKey {},
207
+ nil ,
208
+ ), nil )
209
+
210
+ // Simulate a failure in PutItem operation
211
+ mockDynamoDBClient .On ("PutItem" , mock .Anything , mock .AnythingOfType ("*dynamodb.PutItemInput" ), mock .Anything ).Return (& dynamodb.PutItemOutput {}, fmt .Errorf ("failed to put item" ))
212
+
213
+ // Attempt to put an item, expecting failure
214
+ _ , err := encryptedClient .PutItem (context .Background (), & dynamodb.PutItemInput {
215
+ TableName : aws .String ("test-table" ),
216
+ Item : map [string ]types.AttributeValue {
217
+ "PK" : & types.AttributeValueMemberS {Value : "123" },
218
+ "SK" : & types.AttributeValueMemberS {Value : "TestFailure" },
219
+ },
220
+ })
221
+
222
+ // Check if error was as expected
223
+ assert .Error (t , err )
224
+ assert .Contains (t , err .Error (), "failed to put item" )
225
+ mockDynamoDBClient .AssertExpectations (t )
226
+ mockCMProvider .AssertExpectations (t )
227
+ }
228
+
186
229
func TestEncryptedClient_GetItem_Success (t * testing.T ) {
187
230
mockDynamoDBClient := new (MockDynamoDBClient )
188
231
mockCMProvider := new (MockCryptographicMaterialsProvider )
0 commit comments