-
Notifications
You must be signed in to change notification settings - Fork 50
/
update_item.go
98 lines (78 loc) · 3.28 KB
/
update_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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 *UpdateItemExpectation) ToTable(table string) *UpdateItemExpectation {
e.table = &table
return e
}
// WithKeys - method for set Keys expectation
func (e *UpdateItemExpectation) WithKeys(keys map[string]*dynamodb.AttributeValue) *UpdateItemExpectation {
e.key = keys
return e
}
// Updates - method for set Updates expectation
func (e *UpdateItemExpectation) Updates(attrs map[string]*dynamodb.AttributeValueUpdate) *UpdateItemExpectation {
e.attributeUpdates = attrs
return e
}
// WillReturns - method for set desired result
func (e *UpdateItemExpectation) WillReturns(res dynamodb.UpdateItemOutput) *UpdateItemExpectation {
e.output = &res
return e
}
// UpdateItem - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) UpdateItem(input *dynamodb.UpdateItemInput) (*dynamodb.UpdateItemOutput, error) {
if len(e.dynaMock.UpdateItemExpect) > 0 {
x := e.dynaMock.UpdateItemExpect[0] //get first element of expectation
if x.table != nil {
if *x.table != *input.TableName {
return &dynamodb.UpdateItemOutput{}, 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.UpdateItemOutput{}, fmt.Errorf("Expect key %+v but found key %+v", x.key, input.Key)
}
}
if x.attributeUpdates != nil {
if !reflect.DeepEqual(x.attributeUpdates, input.AttributeUpdates) {
return &dynamodb.UpdateItemOutput{}, fmt.Errorf("Expect key %+v but found key %+v", x.attributeUpdates, input.AttributeUpdates)
}
}
// delete first element of expectation
e.dynaMock.UpdateItemExpect = append(e.dynaMock.UpdateItemExpect[:0], e.dynaMock.UpdateItemExpect[1:]...)
return x.output, nil
}
return &dynamodb.UpdateItemOutput{}, fmt.Errorf("Update Item Expectation Not Found")
}
// UpdateItemWithContext - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) UpdateItemWithContext(ctx aws.Context, input *dynamodb.UpdateItemInput, opt ...request.Option) (*dynamodb.UpdateItemOutput, error) {
if len(e.dynaMock.UpdateItemExpect) > 0 {
x := e.dynaMock.UpdateItemExpect[0] //get first element of expectation
if x.table != nil {
if *x.table != *input.TableName {
return &dynamodb.UpdateItemOutput{}, 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.UpdateItemOutput{}, fmt.Errorf("Expect key %+v but found key %+v", x.key, input.Key)
}
}
if x.attributeUpdates != nil {
if !reflect.DeepEqual(x.attributeUpdates, input.AttributeUpdates) {
return &dynamodb.UpdateItemOutput{}, fmt.Errorf("Expect key %+v but found key %+v", x.attributeUpdates, input.AttributeUpdates)
}
}
// delete first element of expectation
e.dynaMock.UpdateItemExpect = append(e.dynaMock.UpdateItemExpect[:0], e.dynaMock.UpdateItemExpect[1:]...)
return x.output, nil
}
return &dynamodb.UpdateItemOutput{}, fmt.Errorf("Update Item With Context Expectation Not Found")
}