File: batch_test.go

package info (click to toggle)
golang-github-adroll-goamz 0.0~git20170225.0.c5d7d9b-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,520 kB
  • ctags: 2,498
  • sloc: makefile: 41
file content (423 lines) | stat: -rw-r--r-- 11,297 bytes parent folder | download | duplicates (2)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package dynamodb

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"strconv"

	"gopkg.in/check.v1"
)

type BatchSuite struct {
	TableDescriptionT TableDescriptionT
	DynamoDBTest
	WithRange bool
}

func (s *BatchSuite) SetUpSuite(c *check.C) {
	setUpAuth(c)
	s.DynamoDBTest.TableDescriptionT = s.TableDescriptionT
	s.server = New(dynamodb_auth, dynamodb_region)
	pk, err := s.TableDescriptionT.BuildPrimaryKey()
	if err != nil {
		c.Skip(err.Error())
	}
	s.table = s.server.NewTable(s.TableDescriptionT.TableName, pk)

	// Cleanup
	s.TearDownSuite(c)
	_, err = s.server.CreateTable(s.TableDescriptionT)
	if err != nil {
		c.Fatal(err)
	}
	s.WaitUntilStatus(c, "ACTIVE")
}

var batch_suite = &BatchSuite{
	TableDescriptionT: TableDescriptionT{
		TableName: "DynamoDBTestMyTable",
		AttributeDefinitions: []AttributeDefinitionT{
			AttributeDefinitionT{"TestHashKey", "S"},
		},
		KeySchema: []KeySchemaT{
			KeySchemaT{"TestHashKey", "HASH"},
		},
		ProvisionedThroughput: ProvisionedThroughputT{
			ReadCapacityUnits:  1,
			WriteCapacityUnits: 1,
		},
	},
	WithRange: false,
}

var batch_suite_with_range = &BatchSuite{
	TableDescriptionT: TableDescriptionT{
		TableName: "DynamoDBTestMyTable",
		AttributeDefinitions: []AttributeDefinitionT{
			AttributeDefinitionT{"TestHashKey", "S"},
			AttributeDefinitionT{"TestRangeKey", "N"},
		},
		KeySchema: []KeySchemaT{
			KeySchemaT{"TestHashKey", "HASH"},
			KeySchemaT{"TestRangeKey", "RANGE"},
		},
		ProvisionedThroughput: ProvisionedThroughputT{
			ReadCapacityUnits:  1,
			WriteCapacityUnits: 1,
		},
	},
	WithRange: true,
}

var _ = check.Suite(batch_suite)
var _ = check.Suite(batch_suite_with_range)

func (s *BatchSuite) TestBatchGetDocument(c *check.C) {
	numKeys := 3
	keys := make([]*Key, 0, numKeys)
	ins := make([]map[string]interface{}, 0, numKeys)
	outs := make([]map[string]interface{}, numKeys)
	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}

		in := map[string]interface{}{
			"Attr1": "Attr1Val" + strconv.Itoa(i),
			"Attr2": 12 + i,
		}

		if i%2 == 0 { // only add the even keys
			if err := s.table.PutDocument(k, in); err != nil {
				c.Fatal(err)
			}
		}

		keys = append(keys, k)
		ins = append(ins, in)
	}

	errs, err := s.table.BatchGetDocument(keys, true, outs)
	if err != nil {
		c.Fatal(err)
	}

	for i := 0; i < numKeys; i++ {
		if i%2 == 0 {
			c.Assert(errs[i], check.Equals, nil)
			c.Assert(outs[i], check.DeepEquals, ins[i])
		} else {
			c.Assert(errs[i], check.Equals, ErrNotFound)
		}
	}
}

func (s *BatchSuite) TestBatchGetDocumentTyped(c *check.C) {
	type myInnterStruct struct {
		List []interface{}
	}
	type myStruct struct {
		Attr1  string
		Attr2  int64
		Nested myInnterStruct
	}

	numKeys := 3
	keys := make([]*Key, 0, numKeys)
	ins := make([]myStruct, 0, numKeys)
	outs := make([]myStruct, numKeys)

	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}

		in := myStruct{
			Attr1:  "Attr1Val" + strconv.Itoa(i),
			Attr2:  1000000 + int64(i),
			Nested: myInnterStruct{[]interface{}{true, false, nil, "some string", 3.14}},
		}

		if i%2 == 0 { // only add the even keys
			if err := s.table.PutDocument(k, in); err != nil {
				c.Fatal(err)
			}
		}

		keys = append(keys, k)
		ins = append(ins, in)
	}

	errs, err := s.table.BatchGetDocument(keys, true, outs)
	if err != nil {
		c.Fatal(err)
	}

	for i := 0; i < numKeys; i++ {
		if i%2 == 0 {
			c.Assert(errs[i], check.Equals, nil)
			c.Assert(outs[i], check.DeepEquals, ins[i])
		} else {
			c.Assert(errs[i], check.Equals, ErrNotFound)
		}
	}
}

func (s *BatchSuite) TestBatchGetDocumentUnprocessedKeys(c *check.C) {
	// Here we test what happens if DynamoDB returns partial success and returns
	// some keys in the UnprocessedKeys field. To do so, we setup a fake endpoint
	// for DynamoDB which will simply returns a canned response. This is more
	// efficient than loading enough data into DynamoDB local to force it over
	// the 16mb limit.
	endpoint := s.server.Region.DynamoDBEndpoint
	defer func() {
		s.server.Region.DynamoDBEndpoint = endpoint
	}()
	invocations := 0
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rawjson := `{"Responses":{"DynamoDBTestMyTable":[{"Nested":{"M":{"List":{"L":[{"BOOL":true},{"BOOL":false},{"NULL":true},{"S":"some string"},{"N":"3.14"}]}}},"TestHashKey":{"S":"NewHashKeyVal0"},"Attr1":{"S":"Attr1Val0"},"Attr2":{"N":"1000000"},"TestRangeKey":{"N":"12"}}]},"UnprocessedKeys":{"DynamoDBTestMyTable":{"Keys":[{"TestHashKey":{"S":"NewHashKeyVal2"},"TestRangeKey":{"N":"14"}},{"TestHashKey":{"S":"NewHashKeyVal1"},"TestRangeKey":{"N":"13"}}],"ConsistentRead":true}}}`
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, rawjson)
		// Restore the endpoint so any retries succeed.
		s.server.Region.DynamoDBEndpoint = endpoint
		invocations++
	}))
	defer server.Close()
	s.server.Region.DynamoDBEndpoint = server.URL

	type myInnterStruct struct {
		List []interface{}
	}
	type myStruct struct {
		Attr1  string
		Attr2  int64
		Nested myInnterStruct
	}

	numKeys := 3
	keys := make([]*Key, 0, numKeys)
	outs := make([]myStruct, numKeys)

	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}

		keys = append(keys, k)
	}

	errs, err := s.table.BatchGetDocument(keys, true, outs)
	if err != nil {
		c.Fatal(err)
	}

	// Make sure our fake endpoint was called exactly once.
	c.Assert(invocations, check.Equals, 1)

	// Confirm that each key was processed.
	for i := 0; i < numKeys; i++ {
		if i == 0 {
			c.Assert(errs[i], check.Equals, nil)
		} else {
			c.Assert(errs[i], check.Equals, ErrNotFound)
		}
	}
}

func (s *BatchSuite) TestBatchGetDocumentSizeExceeded(c *check.C) {
	numKeys := MaxGetBatchSize + 1
	keys := make([]*Key, 0, numKeys)
	outs := make([]map[string]interface{}, numKeys)
	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}
		keys = append(keys, k)
	}

	_, err := s.table.BatchGetDocument(keys, true, outs)
	if err == nil {
		c.Fatal("Expected max batch size exceeded error")
	} else {
		c.Assert(err.Error(), check.Equals, "Cannot add key, max batch size (100) exceeded")
	}
}

func (s *BatchSuite) TestBatchPutDocument(c *check.C) {
	numKeys := 3
	keys := make([]*Key, 0, numKeys)
	ins := make([]map[string]interface{}, 0, numKeys)
	outs := make([]map[string]interface{}, numKeys)
	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}

		in := map[string]interface{}{
			"Attr1": "Attr1Val" + strconv.Itoa(i),
			"Attr2": 12 + i,
		}

		keys = append(keys, k)
		ins = append(ins, in)
	}

	errs, err := s.table.BatchPutDocument(keys, ins)
	if err != nil {
		c.Fatal(err)
	}
	for i := 0; i < numKeys; i++ {
		c.Assert(errs[i], check.Equals, nil)
	}

	errs, err = s.table.BatchGetDocument(keys, true, outs)
	if err != nil {
		c.Fatal(err)
	}

	for i := 0; i < numKeys; i++ {
		c.Assert(errs[i], check.Equals, nil)
		c.Assert(outs[i], check.DeepEquals, ins[i])
	}
}

func (s *BatchSuite) TestBatchPutDocumentTyped(c *check.C) {
	type myInnterStruct struct {
		List []interface{}
	}
	type myStruct struct {
		Attr1  string
		Attr2  int64
		Nested myInnterStruct
	}

	numKeys := 3
	keys := make([]*Key, 0, numKeys)
	ins := make([]myStruct, 0, numKeys)
	outs := make([]myStruct, numKeys)

	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}

		in := myStruct{
			Attr1:  "Attr1Val" + strconv.Itoa(i),
			Attr2:  1000000 + int64(i),
			Nested: myInnterStruct{[]interface{}{true, false, nil, "some string", 3.14}},
		}

		keys = append(keys, k)
		ins = append(ins, in)
	}

	errs, err := s.table.BatchPutDocument(keys, ins)
	if err != nil {
		c.Fatal(err)
	}

	errs, err = s.table.BatchGetDocument(keys, true, outs)
	if err != nil {
		c.Fatal(err)
	}

	for i := 0; i < numKeys; i++ {
		c.Assert(errs[i], check.Equals, nil)
		c.Assert(outs[i], check.DeepEquals, ins[i])
	}
}

func (s *BatchSuite) TestBatchPutDocumentUnprocessedItems(c *check.C) {
	// Here we test what happens if DynamoDB returns partial success and returns
	// some items in the UnprocessedItems field. To do so, we setup a fake
	// endpoint for DynamoDB which will simply returns a canned response.
	endpoint := s.server.Region.DynamoDBEndpoint
	defer func() {
		s.server.Region.DynamoDBEndpoint = endpoint
	}()
	invocations := 0
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		rawjson := `{"UnprocessedItems":{"DynamoDBTestMyTable":[{"PutRequest":{"Item":{"Attr1":{"S":"Attr1Val1"},"Attr2":{"N":"1000001"},"Nested":{"M":{"List":{"L":[{"BOOL":true},{"BOOL":false},{"NULL":true},{"S":"some string"},{"N":"3.14"}]}}},"TestHashKey":{"S":"NewHashKeyVal1"},"TestRangeKey":{"N":"13"}}}},{"PutRequest":{"Item":{"Attr1":{"S":"Attr1Val2"},"Attr2":{"N":"1000002"},"Nested":{"M":{"List":{"L":[{"BOOL":true},{"BOOL":false},{"NULL":true},{"S":"some string"},{"N":"3.14"}]}}},"TestHashKey":{"S":"NewHashKeyVal2"},"TestRangeKey":{"N":"14"}}}}]}}`
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.WriteHeader(http.StatusOK)
		fmt.Fprintf(w, rawjson)
		// Restore the endpoint so any retries succeed.
		s.server.Region.DynamoDBEndpoint = endpoint
		invocations++
	}))
	defer server.Close()
	s.server.Region.DynamoDBEndpoint = server.URL

	type myInnterStruct struct {
		List []interface{}
	}
	type myStruct struct {
		Attr1  string
		Attr2  int64
		Nested myInnterStruct
	}

	numKeys := 3
	keys := make([]*Key, 0, numKeys)
	ins := make([]myStruct, 0, numKeys)

	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}

		in := myStruct{
			Attr1:  "Attr1Val" + strconv.Itoa(i),
			Attr2:  1000000 + int64(i),
			Nested: myInnterStruct{[]interface{}{true, false, nil, "some string", 3.14}},
		}

		keys = append(keys, k)
		ins = append(ins, in)
	}

	errs, err := s.table.BatchPutDocument(keys, ins)
	if err != nil {
		c.Fatal(err)
	}

	// Make sure our fake endpoint was called exactly once.
	c.Assert(invocations, check.Equals, 1)

	// Confirm that each key was processed.
	for i := 0; i < numKeys; i++ {
		c.Assert(errs[i], check.Equals, nil)
	}
}

func (s *BatchSuite) TestBatchPutDocumentSizeExceeded(c *check.C) {
	numKeys := MaxPutBatchSize + 1
	keys := make([]*Key, 0, numKeys)
	ins := make([]map[string]interface{}, 0, numKeys)
	for i := 0; i < numKeys; i++ {
		k := &Key{HashKey: "NewHashKeyVal" + strconv.Itoa(i)}
		if s.WithRange {
			k.RangeKey = strconv.Itoa(12 + i)
		}
		in := map[string]interface{}{}

		keys = append(keys, k)
		ins = append(ins, in)
	}

	_, err := s.table.BatchPutDocument(keys, ins)
	if err == nil {
		c.Fatal("Expected max batch size exceeded error")
	} else {
		c.Assert(err.Error(), check.Equals, "Cannot add item, max batch size (25) exceeded")
	}
}