diff --git a/.travis.yml b/.travis.yml index 77b0c2f..afdc1d6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,6 @@ +services: + - docker + sudo: false language: go @@ -6,11 +9,13 @@ go: before_script: - go get golang.org/x/lint/golint + - docker pull amazon/dynamodb-local:latest + - docker run -d -p 18000:8000 amazon/dynamodb-local:latest script: - go build ./... - golint ./... - - go test -short -v ./... + - AWS_REGION=local go test -v ./... after_script: - F=$(go fmt ./...); if [ ! -z "$F" ]; then echo "Please gofmt for"$'\n'"$F"; exit 1; fi diff --git a/README.md b/README.md index 86757fe..16e6701 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,9 @@ If you want to run the tests, you *SHOULD* use a dedicated DynamoDB table for th You can specify the table name in an environment variable. ```sh +$ docker pull amazon/dynamodb-local:latest +$ docker run --name aws-go-dynamodb -d -p 18000:8000 amazon/dynamodb-local:latest $ cd table -$ export TEST_DYNAMODB_TABLE_NAME=aws-go-dynamodb-test $ go test -v +$ docker rm -f aws-go-dynamodb ``` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4d76f42 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/nabeken/aws-go-dynamodb + +go 1.15 + +require ( + github.com/aws/aws-sdk-go v1.35.0 + github.com/stretchr/testify v1.6.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2891e0e --- /dev/null +++ b/go.sum @@ -0,0 +1,22 @@ +github.com/aws/aws-sdk-go v1.35.0 h1:Pxqn1MWNfBCNcX7jrXCCTfsKpg5ms2IMUMmmcGtYJuo= +github.com/aws/aws-sdk-go v1.35.0/go.mod h1:H7NKnBqNVzoTJpGfLrQkkD+ytBA93eiDYi/+8rV9s48= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= +github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= +github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/table/table_test.go b/table/table_test.go index 1bebc46..9c67dca 100644 --- a/table/table_test.go +++ b/table/table_test.go @@ -3,18 +3,21 @@ package table import ( "crypto/sha256" "encoding/base64" - "os" + "fmt" "sort" "testing" "time" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/dynamodb" "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" "github.com/nabeken/aws-go-dynamodb/attributes" "github.com/nabeken/aws-go-dynamodb/table/option" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type TestItem struct { @@ -100,28 +103,70 @@ func (i TestItem) MarshalItem() (map[string]*dynamodb.AttributeValue, error) { return itemMapped, nil } +func newDynamoDBLocalClient() *dynamodb.DynamoDB { + conf := aws.NewConfig(). + WithCredentials(credentials.NewStaticCredentials("AWS_GO_DDB_TESTING", "dummy", "")). + WithEndpoint("http://127.0.0.1:18000") + return dynamodb.New(session.New(conf)) +} + func TestTable(t *testing.T) { - name := os.Getenv("TEST_DYNAMODB_TABLE_NAME") - if len(name) == 0 { - t.Skip("TEST_DYNAMODB_TABLE_NAME must be set") - } + var tableName = fmt.Sprintf("aws-go-dynamodb-testing-%d", time.Now().Unix()) assert := assert.New(t) + require := require.New(t) + + ddbc := newDynamoDBLocalClient() + + t.Logf("Creating a table '%s' on DynamoDB Local...", tableName) + + _, err := ddbc.CreateTable(&dynamodb.CreateTableInput{ + TableName: &tableName, + ProvisionedThroughput: &dynamodb.ProvisionedThroughput{ + ReadCapacityUnits: aws.Int64(1), + WriteCapacityUnits: aws.Int64(1), + }, + AttributeDefinitions: []*dynamodb.AttributeDefinition{ + { + AttributeName: aws.String("user_id"), + AttributeType: aws.String("S"), + }, + { + AttributeName: aws.String("date"), + AttributeType: aws.String("N"), + }, + }, + KeySchema: []*dynamodb.KeySchemaElement{ + { + AttributeName: aws.String("user_id"), + KeyType: aws.String("HASH"), + }, + { + AttributeName: aws.String("date"), + KeyType: aws.String("RAANGE"), + }, + }, + }) + require.NoError(err) + + require.NoError(ddbc.WaitUntilTableExists(&dynamodb.DescribeTableInput{ + TableName: &tableName, + })) - dtable := New(dynamodb.New(session.New()), name). + dtable := New(ddbc, tableName). WithHashKey("user_id", "S"). WithRangeKey("date", "N") now := time.Now() items := []TestItem{ - TestItem{ + { UserID: "foobar-1", Date: now.Unix(), Status: "waiting", Password: "hogehoge", }, - TestItem{ + { UserID: "foobar-1", Date: now.Add(1 * time.Minute).Unix(), Status: "waiting",