For example, when I use gomock to test code that uses DynamoDB, like this:
mockDynamoDB := awsmock.NewMockDynamoDBAPI(ctrl)
mockDynamoDBClient.EXPECT().PutItemWithContext(gomock.Any(),
gomock.Any() // Here I should use a custom matcher for comparing PutItemInput
).Return(&dynamodb.PutItemOutput{...}, nil).Times(1)
// do something which will internally call PutItemWithContext
To avoid using gomock.Any()
everywhere, I think I should implement a matcher for PutItemInput
, however it seems a bit difficult as the PutItemInput
is complex with serialized Item map[string]*AttributeValue
, I need to compare one by one like this but I don’t like this implementation.
func (piim dynamodbPutItemInputEqualMatcher) Matches(x interface{}) bool {
input, ok := x.(*dynamodb.PutItemInput)
if !ok {
return false
}
if input.TableName != nil && piim.expectedInput.TableName != nil && *input.TableName != *pie.expectedInput.TableName {
return false
}
for attributeName, attributeValue := range input.Item {
exptectedAttributeValue, ok := piim.expectedInput.Item[attributeName]
if !ok {
return false
}
if attributeValue.N != nil && exptectedAttributeValue.N != nil && *attributeValue.N != *exptectedAttributeValue.N {
return false
}
if attributeValue.S != nil && exptectedAttributeValue.S != nil && *attributeValue.S != *exptectedAttributeValue.S {
return false
}
if attributeValue.BOOL != nil && exptectedAttributeValue.BOOL != nil && *attributeValue.BOOL != *exptectedAttributeValue.BOOL {
return false
}
if attributeValue.NULL != nil && exptectedAttributeValue.NULL != nil && *attributeValue.NULL != *exptectedAttributeValue.NULL {
return false
}
if attributeValue.B != nil || attributeValue.NS != nil || attributeValue.SS != nil || attributeValue.BS != nil || attributeValue.L != nil || attributeValue.M != nil {
return false
}
}
return true
}
Also DynamoDB has other APIs like GetItem
, ScanPages
, etc,. I need to implement custom matcher for every type of input struct.
Do we have a better way to use gomock with AWS? Thanks.