-
Notifications
You must be signed in to change notification settings - Fork 50
/
get_item.go
80 lines (63 loc) · 2.51 KB
/
get_item.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
package dynamock
import (
"fmt"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// ToTable - method for set Table expectation
func (e *GetItemExpectation) ToTable(table string) *GetItemExpectation {
e.table = &table
return e
}
// WithKeys - method for set Keys expectation
func (e *GetItemExpectation) WithKeys(keys map[string]*dynamodb.AttributeValue) *GetItemExpectation {
e.key = keys
return e
}
// WillReturns - method for set desired result
func (e *GetItemExpectation) WillReturns(res dynamodb.GetItemOutput) *GetItemExpectation {
e.output = &res
return e
}
// GetItem - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) GetItem(input *dynamodb.GetItemInput) (*dynamodb.GetItemOutput, error) {
if len(e.dynaMock.GetItemExpect) > 0 {
x := e.dynaMock.GetItemExpect[0] //get first element of expectation
if x.table != nil {
if *x.table != *input.TableName {
return &dynamodb.GetItemOutput{}, fmt.Errorf("Expect table %s but found table %s", *x.table, *input.TableName)
}
}
if x.key != nil {
if !reflect.DeepEqual(x.key, input.Key) {
return &dynamodb.GetItemOutput{}, fmt.Errorf("Expect key %+v but found key %+v", x.key, input.Key)
}
}
// delete first element of expectation
e.dynaMock.GetItemExpect = append(e.dynaMock.GetItemExpect[:0], e.dynaMock.GetItemExpect[1:]...)
return x.output, nil
}
return &dynamodb.GetItemOutput{}, fmt.Errorf("Get Item Expectation Not Found")
}
// GetItemWithContext - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) GetItemWithContext(ctx aws.Context, input *dynamodb.GetItemInput, opt ...request.Option) (*dynamodb.GetItemOutput, error) {
if len(e.dynaMock.GetItemExpect) > 0 {
x := e.dynaMock.GetItemExpect[0] //get first element of expectation
if x.table != nil {
if *x.table != *input.TableName {
return &dynamodb.GetItemOutput{}, fmt.Errorf("Expect table %s but found table %s", *x.table, *input.TableName)
}
}
if x.key != nil {
if !reflect.DeepEqual(x.key, input.Key) {
return &dynamodb.GetItemOutput{}, fmt.Errorf("Expect key %+v but found key %+v", x.key, input.Key)
}
}
// delete first element of expectation
e.dynaMock.GetItemExpect = append(e.dynaMock.GetItemExpect[:0], e.dynaMock.GetItemExpect[1:]...)
return x.output, nil
}
return &dynamodb.GetItemOutput{}, fmt.Errorf("Get Item With Context Expectation Not Found")
}