forked from guregu/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbatch_test.go
163 lines (143 loc) · 3.72 KB
/
batch_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
161
162
163
package helixddb
import (
"testing"
"time"
)
const batchSize = 101
func TestBatchGetWrite(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
table := testDB.Table(testTable)
items := make([]interface{}, batchSize)
widgets := make(map[int]widget)
keys := make([]Keyed, batchSize)
for i := 0; i < batchSize; i++ {
now := time.Now().UTC()
w := widget{
UserID: i,
Time: now,
Msg: "batch test",
}
widgets[i] = w
items[i] = w
keys[i] = Keys{i, now}
}
var wcc ConsumedCapacity
wrote, err := table.Batch().Write().Put(items...).ConsumedCapacity(&wcc).Run()
if wrote != batchSize {
t.Error("unexpected wrote:", wrote, "≠", batchSize)
}
if err != nil {
t.Error("unexpected error:", err)
}
if wcc.Total == 0 {
t.Error("bad consumed capacity", wcc)
}
// get all
var results []widget
var cc ConsumedCapacity
err = table.Batch("UserID", "Time").
Get(keys...).
Project("UserID", "Time").
Consistent(true).
ConsumedCapacity(&cc).
All(&results)
if err != nil {
t.Error("unexpected error:", err)
}
if len(results) != batchSize {
t.Error("expected", batchSize, "results, got", len(results))
}
if cc.Total == 0 {
t.Error("bad consumed capacity", cc)
}
for _, result := range results {
other := widgets[result.UserID]
if result.UserID != other.UserID && !result.Time.Equal(other.Time) {
t.Error("unexpected result", result, "≠", other)
}
if result.Msg != "" {
t.Error("projection not applied, want: blank. got:", result.Msg)
}
}
// delete both
wrote, err = table.Batch("UserID", "Time").Write().
Delete(keys...).Run()
if wrote != batchSize {
t.Error("unexpected wrote:", wrote, "≠", batchSize)
}
if err != nil {
t.Error("unexpected error:", err)
}
// get both again
results = nil
err = table.Batch("UserID", "Time").
Get(keys...).
Consistent(true).
All(&results)
if err != ErrNotFound {
t.Error("expected ErrNotFound, got", err)
}
if len(results) != 0 {
t.Error("expected 0 results, got", len(results))
}
}
func TestBatchGetEmptySets(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
table := testDB.Table(testTable)
now := time.Now().UnixNano() / 1000000000
id := int(now)
entry := widget{UserID: id, Time: time.Now()}
if err := table.Put(entry).Run(); err != nil {
panic(err)
}
entry2 := widget{UserID: id + batchSize*2, Time: entry.Time}
if err := table.Put(entry2).Run(); err != nil {
panic(err)
}
keysToCheck := []Keyed{}
for i := entry.UserID; i <= entry2.UserID; i += 1 {
keysToCheck = append(keysToCheck, Keys{i, entry.Time})
}
results := []widget{}
err := table.Batch("UserID", "Time").Get(keysToCheck...).Consistent(true).All(&results)
if err != nil {
t.Error(err)
}
if len(results) != 2 {
t.Error("batch get empty set, unexpected length:", len(results), "want:", 2)
}
if err := table.Delete("UserID", entry.UserID).Range("Time", entry.Time).Run(); err != nil {
panic(err)
}
results = []widget{}
err = table.Batch("UserID", "Time").Get(keysToCheck...).Consistent(true).All(&results)
if err != nil {
t.Error(err)
}
if len(results) != 1 {
t.Error("batch get empty set, unexpected length:", len(results), "want:", 1)
}
results = []widget{}
err = table.Batch("UserID", "Time").Get(keysToCheck[:len(keysToCheck)-1]...).Consistent(true).All(&results)
if err != ErrNotFound {
t.Error(err)
}
if len(results) != 0 {
t.Error("batch get empty set, unexpected length:", len(results), "want:", 0)
}
}
func TestBatchEmptyInput(t *testing.T) {
table := testDB.Table(testTable)
err := table.Batch("UserID", "Time").Get().All(nil)
if err != ErrNoInput {
t.Error("unexpected error", err)
}
_, err = table.Batch("UserID", "Time").Write().Run()
if err != ErrNoInput {
t.Error("unexpected error", err)
}
}