forked from brightcove/playback_ddbsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_test.go
More file actions
166 lines (125 loc) · 4.14 KB
/
db_test.go
File metadata and controls
166 lines (125 loc) · 4.14 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package ddbsync
import (
"errors"
"fmt"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/zencoder/ddbsync/mocks"
"github.com/zencoder/ddbsync/models"
)
const (
DB_VALID_TABLE_NAME string = "TestLockTable"
DB_VALID_REGION string = "us-west-2"
DB_VALID_NO_ENDPOINT string = ""
DB_VALID_DISABLE_SSL_NO bool = false
DB_VALID_NAME string = "db-name"
DB_VALID_CREATED int64 = 1424385592
DB_VALID_CREATED_STRING string = "1424385592"
)
type DBSuite struct {
suite.Suite
mock *mocks.AWSDynamoer
db DBer
}
func TestDBSuite(t *testing.T) {
suite.Run(t, new(DBSuite))
}
func (s *DBSuite) SetupTest() {
s.mock = new(mocks.AWSDynamoer)
s.db = NewDatabase(DB_VALID_TABLE_NAME, DB_VALID_REGION, DB_VALID_NO_ENDPOINT, DB_VALID_DISABLE_SSL_NO)
s.db.(*database).client = s.mock
}
func (s *DBSuite) TestPut() {
s.mock.On("PutItem", mock.AnythingOfType("*dynamodb.PutItemInput")).Return(&dynamodb.PutItemOutput{}, nil)
err := s.db.Put(DB_VALID_NAME, DB_VALID_CREATED)
assert.Nil(s.T(), err)
}
func (s *DBSuite) TestPutError() {
s.mock.On("PutItem", mock.AnythingOfType("*dynamodb.PutItemInput")).Return((*dynamodb.PutItemOutput)(nil), errors.New("PutItem Error"))
err := s.db.Put(DB_VALID_NAME, DB_VALID_CREATED)
assert.NotNil(s.T(), err)
}
func (s *DBSuite) TestGet() {
one := int64(1)
qo := &dynamodb.QueryOutput{
Count: &one,
Items: []map[string]*dynamodb.AttributeValue{
map[string]*dynamodb.AttributeValue{
"Name": &dynamodb.AttributeValue{
S: aws.String(DB_VALID_NAME),
},
"Created": &dynamodb.AttributeValue{
N: aws.String(DB_VALID_CREATED_STRING),
},
},
},
}
s.mock.On("Query", mock.AnythingOfType("*dynamodb.QueryInput")).Return(qo, nil)
i, err := s.db.Get(DB_VALID_NAME)
assert.NotNil(s.T(), i)
assert.Nil(s.T(), err)
assert.Equal(s.T(), &models.Item{Name: DB_VALID_NAME, Created: DB_VALID_CREATED}, i)
}
func (s *DBSuite) TestGetErrorNoQueryOutput() {
s.mock.On("Query", mock.AnythingOfType("*dynamodb.QueryInput")).Return((*dynamodb.QueryOutput)(nil), errors.New("Query Error"))
i, err := s.db.Get(DB_VALID_NAME)
assert.Nil(s.T(), i)
assert.NotNil(s.T(), err)
}
func (s *DBSuite) TestGetErrorNilCount() {
qo := &dynamodb.QueryOutput{
Count: nil,
}
s.mock.On("Query", mock.AnythingOfType("*dynamodb.QueryInput")).Return(qo, nil)
i, err := s.db.Get(DB_VALID_NAME)
assert.Nil(s.T(), i)
assert.NotNil(s.T(), err)
assert.Equal(s.T(), errors.New("Count not returned"), err)
}
func (s *DBSuite) TestGetErrorZeroCount() {
zero := int64(0)
qo := &dynamodb.QueryOutput{
Count: &zero,
}
s.mock.On("Query", mock.AnythingOfType("*dynamodb.QueryInput")).Return(qo, nil)
i, err := s.db.Get(DB_VALID_NAME)
assert.Nil(s.T(), i)
assert.NotNil(s.T(), err)
assert.Equal(s.T(), errors.New(fmt.Sprintf("No item for Name, %s", DB_VALID_NAME)), err)
}
func (s *DBSuite) TestGetErrorCountTooHigh() {
two := int64(2)
qo := &dynamodb.QueryOutput{
Count: &two,
}
s.mock.On("Query", mock.AnythingOfType("*dynamodb.QueryInput")).Return(qo, nil)
i, err := s.db.Get(DB_VALID_NAME)
assert.Nil(s.T(), i)
assert.NotNil(s.T(), err)
assert.Equal(s.T(), errors.New("Expected only 1 item returned from Dynamo, got 2"), err)
}
func (s *DBSuite) TestGetErrorCountSetNoItems() {
one := int64(1)
qo := &dynamodb.QueryOutput{
Count: &one,
}
s.mock.On("Query", mock.AnythingOfType("*dynamodb.QueryInput")).Return(qo, nil)
i, err := s.db.Get(DB_VALID_NAME)
assert.Nil(s.T(), i)
assert.NotNil(s.T(), err)
assert.Equal(s.T(), errors.New("No item returned, count is invalid."), err)
}
func (s *DBSuite) TestDelete() {
s.mock.On("DeleteItem", mock.AnythingOfType("*dynamodb.DeleteItemInput")).Return(&dynamodb.DeleteItemOutput{}, nil)
err := s.db.Delete(DB_VALID_NAME)
assert.Nil(s.T(), err)
}
func (s *DBSuite) TestDeleteError() {
s.mock.On("DeleteItem", mock.AnythingOfType("*dynamodb.DeleteItemInput")).Return((*dynamodb.DeleteItemOutput)(nil), errors.New("Delete Error"))
err := s.db.Delete(DB_VALID_NAME)
assert.NotNil(s.T(), err)
}