-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdynamo_test.go
78 lines (69 loc) · 2.78 KB
/
dynamo_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
package main
import (
"fmt"
"reflect"
"sync"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
)
// dataSet represents the sets of data used for the tests in Dynamo
var dataSet = []map[string]*dynamodb.AttributeValue{
{"artist": {S: aws.String("Aerosmith")}, "songs": {SS: []*string{aws.String("Cryin'"), aws.String("Walk this way"), aws.String("Crazy"), aws.String("Rag Doll")}}},
{"artist": {S: aws.String("Queen")}, "songs": {SS: []*string{aws.String("Under pressure"), aws.String("Somebody to love"), aws.String("Fat bottom girls")}}},
{"artist": {S: aws.String("Metallica")}, "songs": {SS: []*string{aws.String("Enter Sandman"), aws.String("Master of Puppets")}}},
}
// struct to mock the Dynamo calls
type mockDynamoDBClient struct {
dynamodbiface.DynamoDBAPI
}
func (m *mockDynamoDBClient) ScanPages(params *dynamodb.ScanInput, pager func(*dynamodb.ScanOutput, bool) bool) error {
dsSize := int64(len(dataSet))
dataOut := dynamodb.ScanOutput{
ConsumedCapacity: &dynamodb.ConsumedCapacity{CapacityUnits: aws.Float64(23), TableName: params.TableName},
Count: aws.Int64(dsSize),
Items: dataSet,
}
pager(&dataOut, true)
return nil
}
func TestTableToChannel(t *testing.T) {
var wg sync.WaitGroup
dataPipe := make(chan map[string]*dynamodb.AttributeValue)
// Consumer
go func() {
// Checks that all the elements of the channel are part of the dataSet
idx := 0
for elem := range dataPipe {
if !reflect.DeepEqual(elem, dataSet[idx]) {
t.Fatalf("Element %d in the channel mismatch. Expecting: %v\nGot: %v\n", idx, dataSet[idx], elem)
}
idx++
}
// Checks that all the elements of the dataSet have been parsed
if idx != len(dataSet) {
t.Fatalf("Size of the dataSet is %d, only got %d elements from the channel\n", len(dataSet), idx)
}
wg.Done()
}()
wg.Add(1)
TableToChannel(&mockDynamoDBClient{}, "myTable", 10, time.Duration(42)*time.Millisecond, dataPipe)
wg.Wait()
}
func TestDynamoErrorCheck(t *testing.T) {
errorTest := []struct{ inputErr, expectedOut error }{
{inputErr: nil, expectedOut: nil},
{inputErr: fmt.Errorf("Random error"), expectedOut: fmt.Errorf("Random error")},
{inputErr: awserr.New(dynamodb.ErrCodeInternalServerError, "Bla bla", nil), expectedOut: awserr.New(dynamodb.ErrCodeInternalServerError, "Bla bla", nil)},
{inputErr: awserr.New(dynamodb.ErrCodeProvisionedThroughputExceededException, "Bla bla", nil), expectedOut: nil},
}
for _, item := range errorTest {
returnedOut := dynamoErrorCheck(item.inputErr, 1)
if !reflect.DeepEqual(returnedOut, item.expectedOut) {
t.Fatalf("Input %v should return %v. Got: %v\n", item.inputErr, item.expectedOut, returnedOut)
}
}
}