-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamo_test.go
90 lines (80 loc) · 2.19 KB
/
dynamo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package dynamods
import (
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
ds "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)
func TestSuite(t *testing.T) {
// run docker-compose up in this repo in order to get a local
// s3 running on port 4572
config := Config{
RegionEndpoint: "http://localhost:4566",
TableName: "localTable",
Region: "us-east-1",
AccessKey: "localonlyac",
SecretKey: "localonlysk",
}
dynds, err := NewDynamoDatastore(config)
if err != nil {
t.Error(err)
}
err = devMakeTable(dynds.DynamoDB, config.TableName)
if err != nil {
t.Fatal(err)
}
t.Run("basic operations", func(t *testing.T) {
dstest.SubtestBasicPutGet(t, dynds)
})
t.Run("not found operations", func(t *testing.T) {
dstest.SubtestNotFounds(t, dynds)
})
t.Run("many puts and gets, query", func(t *testing.T) {
dstest.SubtestManyKeysAndQuery(t, dynds)
})
t.Run("ttl works", func(t *testing.T) {
k := ds.NewKey("/test/key")
dur := 10 * time.Second
expires := time.Now().UTC().Add(dur)
err := dynds.PutWithTTL(k, []byte("hi"), dur)
if err != nil {
t.Fatal(err)
}
ttl, err := dynds.GetExpiration(k)
if err != nil {
t.Fatal(err)
}
if ttl.Unix() != expires.Unix() {
t.Errorf("%v != %v", ttl.Unix(), expires.Unix())
}
err = dynds.SetTTL(k, dur)
if err != nil {
t.Fatal(err)
}
})
}
func devMakeTable(dynds *dynamodb.DynamoDB, tableName string) error {
_, err := dynds.CreateTable(&dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{AttributeName: aws.String(keyKey), AttributeType: aws.String("S")},
},
BillingMode: aws.String("PAY_PER_REQUEST"),
KeySchema: []*dynamodb.KeySchemaElement{
{KeyType: aws.String("HASH"), AttributeName: aws.String(keyKey)},
},
TableName: aws.String(tableName),
})
_, err = dynds.UpdateTimeToLive(&dynamodb.UpdateTimeToLiveInput{
TableName: aws.String(tableName),
TimeToLiveSpecification: &dynamodb.TimeToLiveSpecification{
AttributeName: aws.String(ttlKey),
Enabled: aws.Bool(true),
},
})
if err == nil {
time.Sleep(2 * time.Second) // give it time to go
}
return err
}