sqs-browse: started working on tests
This commit is contained in:
parent
7526c095ee
commit
cff059e160
11 changed files with 516 additions and 16 deletions
59
test/testdynamo/client.go
Normal file
59
test/testdynamo/client.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package testdynamo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/config"
|
||||
"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"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type TestData []map[string]interface{}
|
||||
|
||||
func SetupTestTable(t *testing.T, tableName string, testData TestData) *dynamodb.Client {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
|
||||
cfg, err := config.LoadDefaultConfig(ctx)
|
||||
assert.NoError(t, err)
|
||||
|
||||
dynamoClient := dynamodb.NewFromConfig(cfg,
|
||||
dynamodb.WithEndpointResolver(dynamodb.EndpointResolverFromURL("http://localhost:8000")))
|
||||
|
||||
dynamoClient.DeleteTable(ctx, &dynamodb.DeleteTableInput{
|
||||
TableName: aws.String(tableName),
|
||||
})
|
||||
|
||||
_, err = dynamoClient.CreateTable(ctx, &dynamodb.CreateTableInput{
|
||||
TableName: aws.String(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),
|
||||
},
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
for _, item := range testData {
|
||||
m, err := attributevalue.MarshalMap(item)
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = dynamoClient.PutItem(ctx, &dynamodb.PutItemInput{
|
||||
TableName: aws.String(tableName),
|
||||
Item: m,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
return dynamoClient
|
||||
}
|
||||
15
test/testdynamo/helpers.go
Normal file
15
test/testdynamo/helpers.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package testdynamo
|
||||
|
||||
import (
|
||||
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
|
||||
"github.com/lmika/awstools/internal/dynamo-browse/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRecordAsItem(t *testing.T, item map[string]interface{}) models.Item {
|
||||
m, err := attributevalue.MarshalMap(item)
|
||||
assert.NoError(t, err)
|
||||
|
||||
return models.Item(m)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue