-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamodb_test.go
93 lines (71 loc) · 1.9 KB
/
dynamodb_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
91
92
93
package dynalock
import (
"strconv"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/require"
)
type Person struct {
Name string
}
func TestMarshalStruct(t *testing.T) {
assert := require.New(t)
want := &dynamodb.AttributeValue{
M: map[string]*dynamodb.AttributeValue{
"Name": {S: aws.String("Mark Wolfe")},
},
}
got, err := MarshalStruct(&Person{Name: "Mark Wolfe"})
assert.NoError(err)
assert.Equal(want, got)
}
func TestUnmarshalStruct(t *testing.T) {
assert := require.New(t)
want := &Person{Name: "Mark Wolfe"}
in := &dynamodb.AttributeValue{
M: map[string]*dynamodb.AttributeValue{
"Name": {S: aws.String("Mark Wolfe")},
},
}
val := new(Person)
err := UnmarshalStruct(in, val)
assert.NoError(err)
assert.Equal(want, val)
}
func Test_buildUpdateItemInput(t *testing.T) {
assert := require.New(t)
ddl := &Dynalock{partition: "test", tableName: "testTable"}
update := ddl.buildUpdateItemInput("abc123", &WriteOptions{ttl: 300 * time.Second})
expectedUpdate := dynamodb.UpdateItemInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":inc": {
N: aws.String("1"),
},
":ttl": {
N: aws.String("1575725615"),
},
},
Key: map[string]*dynamodb.AttributeValue{
"id": {
S: aws.String("test"),
},
"name": {
S: aws.String("abc123"),
},
},
ReturnValues: aws.String("ALL_NEW"),
TableName: aws.String("testTable"),
UpdateExpression: aws.String("ADD version :inc SET expires = :ttl"),
}
assert.Equal(expectedUpdate.Key, update.Key)
ttlTime := convertTTL(update.ExpressionAttributeValues[":ttl"].N)
// ensure the TTL has been applied
assert.True(time.Now().Add(200 * time.Second).Before(ttlTime))
}
func convertTTL(val *string) time.Time {
ttl := aws.StringValue(val)
tval, _ := strconv.ParseInt(ttl, 10, 64)
return time.Unix(tval, 0)
}