2018-12-02 13:27:31 -08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/dynamodb"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDynamo(t *testing.T) {
|
|
|
|
runStorageTests(t, createDynamo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// docker run -it --rm -p 8000:8000 amazon/dynamodb-local
|
|
|
|
// noinspection ALL
|
|
|
|
func createDynamo(t *testing.T) storage {
|
2018-12-03 22:58:39 -08:00
|
|
|
d, err := NewDynamo(&aws.Config{
|
|
|
|
Region: aws.String("us-east-1"),
|
|
|
|
Endpoint: aws.String("http://localhost:8000/"),
|
|
|
|
})
|
2018-12-02 13:27:31 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-12-08 14:26:50 -08:00
|
|
|
d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: d.PledgesTableName})
|
|
|
|
d.dynamo.DeleteTable(&dynamodb.DeleteTableInput{TableName: d.FeedsTableName})
|
2018-12-02 13:27:31 -08:00
|
|
|
|
|
|
|
// Create Pledges table
|
|
|
|
_, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{
|
2018-12-08 14:26:50 -08:00
|
|
|
TableName: d.PledgesTableName,
|
2018-12-02 13:27:31 -08:00
|
|
|
AttributeDefinitions: []*dynamodb.AttributeDefinition{
|
|
|
|
{
|
|
|
|
AttributeName: aws.String(pledgesPrimaryKey),
|
|
|
|
AttributeType: aws.String("N"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
KeySchema: []*dynamodb.KeySchemaElement{
|
|
|
|
{
|
|
|
|
AttributeName: aws.String(pledgesPrimaryKey),
|
|
|
|
KeyType: aws.String("HASH"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
|
|
|
|
ReadCapacityUnits: aws.Int64(1),
|
|
|
|
WriteCapacityUnits: aws.Int64(1),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create Feeds table
|
|
|
|
_, err = d.dynamo.CreateTable(&dynamodb.CreateTableInput{
|
2018-12-08 14:26:50 -08:00
|
|
|
TableName: d.FeedsTableName,
|
2018-12-02 13:27:31 -08:00
|
|
|
AttributeDefinitions: []*dynamodb.AttributeDefinition{
|
|
|
|
{
|
|
|
|
AttributeName: aws.String(feedsPrimaryKey),
|
|
|
|
AttributeType: aws.String("S"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AttributeName: aws.String("UserID"),
|
|
|
|
AttributeType: aws.String("S"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AttributeName: aws.String("CreatedAt"),
|
|
|
|
AttributeType: aws.String("N"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
KeySchema: []*dynamodb.KeySchemaElement{
|
|
|
|
{
|
|
|
|
AttributeName: aws.String(feedsPrimaryKey),
|
|
|
|
KeyType: aws.String("HASH"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
GlobalSecondaryIndexes: []*dynamodb.GlobalSecondaryIndex{
|
|
|
|
{
|
|
|
|
IndexName: feedDowngradeIndexName,
|
|
|
|
KeySchema: []*dynamodb.KeySchemaElement{
|
|
|
|
{
|
|
|
|
AttributeName: aws.String("UserID"),
|
|
|
|
KeyType: aws.String("HASH"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AttributeName: aws.String("CreatedAt"),
|
|
|
|
KeyType: aws.String("RANGE"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Projection: &dynamodb.Projection{
|
|
|
|
ProjectionType: aws.String("KEYS_ONLY"),
|
|
|
|
},
|
|
|
|
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
|
|
|
|
ReadCapacityUnits: aws.Int64(1),
|
|
|
|
WriteCapacityUnits: aws.Int64(1),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{
|
|
|
|
ReadCapacityUnits: aws.Int64(1),
|
|
|
|
WriteCapacityUnits: aws.Int64(1),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-12-08 14:26:50 -08:00
|
|
|
err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: d.PledgesTableName})
|
2018-12-02 13:27:31 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-12-08 14:26:50 -08:00
|
|
|
err = d.dynamo.WaitUntilTableExists(&dynamodb.DescribeTableInput{TableName: d.FeedsTableName})
|
2018-12-02 13:27:31 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = d.dynamo.UpdateTimeToLive(&dynamodb.UpdateTimeToLiveInput{
|
2018-12-08 14:26:50 -08:00
|
|
|
TableName: d.FeedsTableName,
|
2018-12-02 13:27:31 -08:00
|
|
|
TimeToLiveSpecification: &dynamodb.TimeToLiveSpecification{
|
|
|
|
AttributeName: feedTimeToLiveField,
|
|
|
|
Enabled: aws.Bool(true),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|