2022-03-23 11:02:46 +00:00
|
|
|
package testdynamo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 21:17:52 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-23 11:02:46 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
2022-03-23 11:09:20 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials"
|
2022-03-23 11:02:46 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-05-18 23:55:15 +00:00
|
|
|
type TestData struct {
|
|
|
|
TableName string
|
|
|
|
Data []map[string]interface{}
|
|
|
|
}
|
2022-03-23 11:02:46 +00:00
|
|
|
|
2022-07-16 01:35:53 +00:00
|
|
|
func SetupTestTable(t *testing.T, testData []TestData) *dynamodb.Client {
|
2022-03-23 11:02:46 +00:00
|
|
|
t.Helper()
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-03-23 11:09:20 +00:00
|
|
|
cfg, err := config.LoadDefaultConfig(ctx,
|
|
|
|
config.WithRegion("ap-southeast-2"),
|
|
|
|
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("abc", "123", "")))
|
2022-03-23 11:02:46 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
dynamoClient := dynamodb.NewFromConfig(cfg,
|
2022-07-16 00:05:48 +00:00
|
|
|
dynamodb.WithEndpointResolver(dynamodb.EndpointResolverFromURL("http://localhost:4566")))
|
2022-03-23 11:02:46 +00:00
|
|
|
|
2022-05-18 23:55:15 +00:00
|
|
|
for _, table := range testData {
|
|
|
|
_, err = dynamoClient.CreateTable(ctx, &dynamodb.CreateTableInput{
|
|
|
|
TableName: aws.String(table.TableName),
|
|
|
|
KeySchema: []types.KeySchemaElement{
|
|
|
|
{AttributeName: aws.String("pk"), KeyType: types.KeyTypeHash},
|
|
|
|
{AttributeName: aws.String("sk"), KeyType: types.KeyTypeRange},
|
|
|
|
},
|
|
|
|
AttributeDefinitions: []types.AttributeDefinition{
|
|
|
|
{AttributeName: aws.String("pk"), AttributeType: types.ScalarAttributeTypeS},
|
|
|
|
{AttributeName: aws.String("sk"), AttributeType: types.ScalarAttributeTypeS},
|
|
|
|
},
|
|
|
|
ProvisionedThroughput: &types.ProvisionedThroughput{
|
|
|
|
ReadCapacityUnits: aws.Int64(100),
|
|
|
|
WriteCapacityUnits: aws.Int64(100),
|
|
|
|
},
|
2022-03-23 11:02:46 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2022-05-18 23:55:15 +00:00
|
|
|
|
|
|
|
for _, item := range table.Data {
|
|
|
|
m, err := attributevalue.MarshalMap(item)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = dynamoClient.PutItem(ctx, &dynamodb.PutItemInput{
|
|
|
|
TableName: aws.String(table.TableName),
|
|
|
|
Item: m,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-03-23 11:02:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 03:03:19 +00:00
|
|
|
t.Cleanup(func() {
|
2022-05-18 23:55:15 +00:00
|
|
|
for _, table := range testData {
|
|
|
|
dynamoClient.DeleteTable(ctx, &dynamodb.DeleteTableInput{
|
|
|
|
TableName: aws.String(table.TableName),
|
|
|
|
})
|
|
|
|
}
|
2022-07-06 03:03:19 +00:00
|
|
|
})
|
|
|
|
|
2022-07-16 01:35:53 +00:00
|
|
|
return dynamoClient
|
2022-03-23 11:02:46 +00:00
|
|
|
}
|