I have a go project where I am having a query method called "queryMyTable". Here is the code for the method.
func queryMyTable(ctx context.Context, dynamodbClient *dynamodb.Client, tableName string, ids []string) (*dynamodb.QueryOutput, error) {
// The filterExpression will be constructed based on the ids. Ex: "id IN (:id1,:id2)"
filterExpression := constructFilterExpression(ids)
// The expressionAttributeValues will be constructed based on the ids.
// It is of type map[string]types.AttributeValue
// expressionAttributeValues = map[string]types.AttributeValue{
// "my-key": &types.AttributeValueS(Value: "my-key-value"),
// "id1": &types.AttributeValueS(Value: "111"),
// "id2": &types.AttributeValueS(Value: "222"),
// }
expressionAttributeValues := constructExpressionAttributeValues(ids)
// Construct the query
queryInput := &dynamodb.QueryInput{
TableName: aws.String("my-ddb-table"),
IndexName: aws.String("my-key-index"),
KeyConditionExpression: aws.String("my-key = :my-key"),
FilterExpression: aws.String(filterExpression),
ExpressionAttributeValues: expressionAttributeValues,
ProjectionExpression: aws.String("id,list"),
}
// Execute the query
result, err := dynamodbClient.Query(ctx, queryInput)
if err != nil {
return nil, err
}
// Return the results
return result, nil
}
The sample data for the input parameters are: The filterExpression will be constructed based on the ids.
filterExpression = "id IN (:id1,:id2)"
The expressionAttributeValues will be constructed based on the ids.
expressionAttributeValues = map[string]types.AttributeValue{
"my-key": &types.AttributeValueS(Value: "my-key-value"),
"id1": &types.AttributeValueS(Value: "111"),
"id2": &types.AttributeValueS(Value: "222"),
}
The code is working fine and I have written tests for constructFilterExpression
and constructExpressionAttributeValues
.
Now, I want to write unit test cases for this method. I am using "aws-sdk-go-v2" and I am not sure how to mock a dynamodb client and setup data for my tests and execute the query method against the mock data.
I tried with this test code
// MockDynamoDBClient is a mock implementation of the DynamoDB client
type MockDynamoDBClient struct {
dynamodb.Client
QueryFunc func(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error)
QueryRequestInput *dynamodb.QueryInput
QueryResponseData *dynamodb.QueryOutput
QueryResponseError error
}
// Query mocks the Query method of the DynamoDB client
func (m *MockDynamoDBClient) Query(ctx context.Context, params *dynamodb.QueryInput, optFns ...func(*dynamodb.Options)) (*dynamodb.QueryOutput, error) {
if m.QueryFunc != nil {
return m.QueryFunc(ctx, params, optFns...)
}
// Store the executed query parameters for assertions in the test
m.QueryRequestInput = params
// Return the stored response data or error
return m.QueryResponseData, m.QueryResponseError
}
But when I executed my query method inside the test method it gave me error obviously
output, err := queryVPNConfigurations(context.Background(), &mockClient.Client, "test-table-name", []string{"111","222"})
Error: operation error DynamoDB: Query, expected endpoint resolver to not be nil
.
Then I tried setup and endpointresolver and failed again. I am not sure, if this is the way it has to be done.
Any help is appreciated.