forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscan_test.go
160 lines (144 loc) · 3.78 KB
/
scan_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
150
151
152
153
154
155
156
157
158
159
160
package dynamo
import (
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
)
func TestScan(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
table := testDB.Table(testTable)
// first, add an item to make sure there is at least one
item := widget{
UserID: 42,
Time: time.Now().UTC(),
Msg: "hello",
}
err := table.Put(item).Run()
if err != nil {
t.Error("unexpected error:", err)
}
// now check if get all and count return the same amount of items
var result []widget
var cc ConsumedCapacity
err = table.Scan().Filter("UserID = ?", 42).Consistent(true).ConsumedCapacity(&cc).All(&result)
if err != nil {
t.Error("unexpected error:", err)
}
ct, err := table.Get("UserID", 42).Consistent(true).Count()
if err != nil {
t.Error("unexpected error:", err)
}
if int(ct) != len(result) {
t.Errorf("count and scan don't match. count: %d, scan: %d", ct, len(result))
}
if cc.Total == 0 {
t.Error("bad consumed capacity", cc)
}
// check this against Scan's count, too
var cc2 ConsumedCapacity
scanCt, err := table.Scan().Filter("UserID = ?", 42).Consistent(true).ConsumedCapacity(&cc2).Count()
if err != nil {
t.Error("unexpected error:", err)
}
if scanCt != ct {
t.Errorf("scan count and get count don't match. scan count: %d, get count: %d", scanCt, ct)
}
if cc2.Total == 0 {
t.Error("bad consumed capacity", cc2)
}
// search for our inserted item
found := false
for _, w := range result {
if w.Time.Equal(item.Time) && reflect.DeepEqual(w, item) {
found = true
break
}
}
if !found {
t.Error("exact match of put item not found in scan")
}
}
func TestScanPaging(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
table := testDB.Table(testTable)
widgets := [10]widget{}
itr := table.Scan().SearchLimit(1).Iter()
for i := 0; i < len(widgets); i++ {
more := itr.Next(&widgets[i])
if itr.Err() != nil {
t.Error("unexpected error", itr.Err())
}
if !more {
break
}
itr = table.Scan().StartFrom(itr.LastEvaluatedKey()).SearchLimit(1).Iter()
}
}
func TestScanMagicLEK(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
table := testDB.Table(testTable)
widgets := []interface{}{
widget{
UserID: 2069,
Time: time.Date(2069, 4, 00, 0, 0, 0, 0, time.UTC),
Msg: "TestScanMagicLEK",
},
widget{
UserID: 2069,
Time: time.Date(2069, 4, 10, 0, 0, 0, 0, time.UTC),
Msg: "TestScanMagicLEK",
},
widget{
UserID: 2069,
Time: time.Date(2069, 4, 20, 0, 0, 0, 0, time.UTC),
Msg: "TestScanMagicLEK",
},
}
t.Run("prepare data", func(t *testing.T) {
if _, err := table.Batch().Write().Put(widgets...).Run(); err != nil {
t.Fatal(err)
}
})
t.Run("having to use DescribeTable", func(t *testing.T) {
itr := table.Scan().Filter("'Msg' = ?", "TestScanMagicLEK").Limit(2).Iter()
for i := 0; i < len(widgets); i++ {
var w widget
itr.Next(&w)
if itr.Err() != nil {
t.Error("unexpected error", itr.Err())
}
itr = table.Scan().Filter("'Msg' = ?", "TestScanMagicLEK").StartFrom(itr.LastEvaluatedKey()).Limit(2).Iter()
}
})
t.Run("via index", func(t *testing.T) {
itr := table.Scan().Index("Msg-Time-index").Filter("UserID = ?", 2069).Limit(2).Iter()
for i := 0; i < len(widgets); i++ {
var w widget
itr.Next(&w)
if itr.Err() != nil {
t.Error("unexpected error", itr.Err())
}
itr = table.Scan().Index("Msg-Time-index").Filter("UserID = ?", 2069).StartFrom(itr.LastEvaluatedKey()).Limit(2).Iter()
}
})
t.Run("table cache", func(t *testing.T) {
pk, err := table.primaryKeys(aws.BackgroundContext(), nil, nil, "")
if err != nil {
t.Fatal(err)
}
expect := map[string]struct{}{
"UserID": {},
"Time": {},
}
if !reflect.DeepEqual(pk, expect) {
t.Error("unexpected key cache. want:", expect, "got:", pk)
}
})
}