-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrepository_query_test.go
149 lines (127 loc) · 3.95 KB
/
repository_query_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
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
package djoemo_test
import (
"github.com/golang/mock/gomock"
. "github.com/adjoeio/djoemo"
"github.com/adjoeio/djoemo/mock"
)
var _ = Describe("Repository", func() {
const (
UserTableName = "UserTable"
ProfileTableName = "ProfileTable"
)
var (
dMock mock.DynamoMock
repository RepositoryInterface
)
BeforeEach(func() {
mockCtrl := gomock.NewController(GinkgoT())
dAPIMock := mock.NewMockDynamoDBAPI(mockCtrl)
dMock = mock.NewDynamoMock(dAPIMock)
repository = NewRepository(dAPIMock)
})
Describe("Query Items", func() {
Describe("Query Items Invalid key ", func() {
It("should fail with table name is nil", func() {
query := Query().WithHashKeyName("UUID").WithHashKey("uuid")
user := &[]User{}
err := repository.Query(query, user)
Expect(err).To(BeEqualTo(ErrInvalidTableName))
})
It("should fail with hash key name is nil", func() {
query := Query().WithTableName(UserTableName).WithHashKey("uuid")
user := &[]User{}
err := repository.Query(query, user)
Expect(err).To(BeEqualTo(ErrInvalidHashKeyName))
})
It("should fail with hash key value is nil", func() {
query := Query().WithTableName(UserTableName).WithHashKeyName("UUID")
user := &[]User{}
err := repository.Query(query, user)
Expect(err).To(BeEqualTo(ErrInvalidHashKeyValue))
})
})
Describe("Query Items", func() {
It("should query items with Hash", func() {
q := Query().WithTableName(UserTableName).
WithHashKeyName("UUID").
WithHashKey("uuid")
userDBOutput := map[string]interface{}{
"UUID": "uuid",
}
dMock.Should().
Query(
dMock.WithTable(q.TableName()),
dMock.WithCondition(*q.HashKeyName(), q.HashKey(), string(Equal)),
dMock.WithQueryOutput(userDBOutput),
).Exec()
var users []User
err := repository.Query(q, &users)
Expect(err).To(BeNil())
Expect(users[0].UUID).To(BeEqualTo(userDBOutput["UUID"]))
})
It("should query items with Hash", func() {
q := Query().WithTableName(UserTableName).
WithHashKeyName("UUID").
WithHashKey("uuid").
WithLimit(2).
WithDescending()
userDBOutput := map[string]interface{}{
"UUID": "uuid",
}
dMock.Should().
Query(
dMock.WithTable(q.TableName()),
dMock.WithCondition(*q.HashKeyName(), q.HashKey(), string(Equal)),
dMock.WithQueryOutput(userDBOutput),
dMock.WithLimit(2),
dMock.WithDesc(true),
).Exec()
var users []User
err := repository.Query(q, &users)
Expect(err).To(BeNil())
Expect(users[0].UUID).To(BeEqualTo(userDBOutput["UUID"]))
})
It("should query items with hash and range", func() {
q := Query().WithTableName(ProfileTableName).
WithHashKeyName("UUID").
WithHashKey("uuid").
WithRangeKeyName("Email").
WithRangeKey("user").
WithRangeOp(BeginsWith)
profileDBOutput := []map[string]interface{}{
{
"UUID": "uuid1",
"Email": "[email protected]",
}, {
"UUID": "uuid2",
"Email": "[email protected]",
},
}
dMock.Should().
Query(
dMock.WithTable(q.TableName()),
dMock.WithCondition(*q.HashKeyName(), q.HashKey(), string(Equal)),
dMock.WithCondition(*q.RangeKeyName(), q.RangeKey(), string(BeginsWith)),
dMock.WithQueryOutput(profileDBOutput),
).Exec()
var profiles []Profile
err := repository.Query(q, &profiles)
Expect(err).To(BeNil())
Expect(len(profiles)).To(BeEqualTo(2))
Expect(profiles[0].UUID).To(BeEqualTo("uuid1"))
Expect(profiles[1].UUID).To(BeEqualTo("uuid2"))
})
It("should return error if output is not pointer to slice ", func() {
q := Query().WithTableName(ProfileTableName).
WithHashKeyName("UUID").
WithHashKey("uuid").
WithRangeKeyName("Email").
WithRangeKey("user").
WithRangeOp(BeginsWith)
var profile Profile
err := repository.Query(q, &profile)
Expect(err).To(BeEquivalentTo(ErrInvalidPointerSliceType))
})
})
})
})