forked from ryandotsmith/ddbsync
-
Notifications
You must be signed in to change notification settings - Fork 5
/
db_test.go
88 lines (72 loc) · 1.95 KB
/
db_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
package ddbsync
import (
"errors"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/assert"
)
const (
DBValidTableName = "TestLockTable"
DBValidName = "db-name"
DBValidTTL = 30 * time.Second
)
type mockDynamo struct {
UpdateItemOutput *dynamodb.UpdateItemOutput
UpdateItemError error
DeleteItemOutput *dynamodb.DeleteItemOutput
DeleteItemError error
}
func (m mockDynamo) UpdateItem(*dynamodb.UpdateItemInput) (*dynamodb.UpdateItemOutput, error) {
return m.UpdateItemOutput, m.UpdateItemError
}
func (m mockDynamo) DeleteItem(*dynamodb.DeleteItemInput) (*dynamodb.DeleteItemOutput, error) {
return m.DeleteItemOutput, m.DeleteItemError
}
func newMockedClient(mocked mockDynamo) *Database {
return &Database{client: mocked, tableName: DBValidTableName}
}
func TestDBPut(t *testing.T) {
t.Parallel()
db := newMockedClient(mockDynamo{
UpdateItemOutput: &dynamodb.UpdateItemOutput{},
})
err := db.Acquire(DBValidName, DBValidTTL)
assert.NoError(t, err)
}
func TestDBPutError(t *testing.T) {
t.Parallel()
db := newMockedClient(mockDynamo{
UpdateItemError: errors.New("UpdateItem Error"),
})
err := db.Acquire(DBValidName, DBValidTTL)
assert.Error(t, err)
}
func TestDBPutErrorLocked(t *testing.T) {
t.Parallel()
db := newMockedClient(mockDynamo{
UpdateItemError: awserr.New(
dynamodb.ErrCodeConditionalCheckFailedException,
"condition check failed",
errors.New("failed")),
})
err := db.Acquire(DBValidName, DBValidTTL)
assert.Error(t, err)
}
func TestDBDelete(t *testing.T) {
t.Parallel()
db := newMockedClient(mockDynamo{
DeleteItemOutput: &dynamodb.DeleteItemOutput{},
})
err := db.Delete(DBValidName)
assert.NoError(t, err)
}
func TestDBDeleteError(t *testing.T) {
t.Parallel()
db := newMockedClient(mockDynamo{
DeleteItemError: errors.New("dynamodb DeleteItem error"),
})
err := db.Delete(DBValidName)
assert.Error(t, err)
}