-
Notifications
You must be signed in to change notification settings - Fork 50
/
put_item.go
80 lines (63 loc) · 2.53 KB
/
put_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 *PutItemExpectation) ToTable(table string) *PutItemExpectation {
e.table = &table
return e
}
// WithItems - method for set Items expectation
func (e *PutItemExpectation) WithItems(item map[string]*dynamodb.AttributeValue) *PutItemExpectation {
e.item = item
return e
}
// WillReturns - method for set desired result
func (e *PutItemExpectation) WillReturns(res dynamodb.PutItemOutput) *PutItemExpectation {
e.output = &res
return e
}
// PutItem - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) PutItem(input *dynamodb.PutItemInput) (*dynamodb.PutItemOutput, error) {
if len(e.dynaMock.PutItemExpect) > 0 {
x := e.dynaMock.PutItemExpect[0] //get first element of expectation
if x.table != nil {
if *x.table != *input.TableName {
return &dynamodb.PutItemOutput{}, fmt.Errorf("Expect table %s but found table %s", *x.table, *input.TableName)
}
}
if x.item != nil {
if !reflect.DeepEqual(x.item, input.Item) {
return &dynamodb.PutItemOutput{}, fmt.Errorf("Expect item %+v but found item %+v", x.item, input.Item)
}
}
// delete first element of expectation
e.dynaMock.PutItemExpect = append(e.dynaMock.PutItemExpect[:0], e.dynaMock.PutItemExpect[1:]...)
return x.output, nil
}
return &dynamodb.PutItemOutput{}, fmt.Errorf("Put Item Expectation Not Found")
}
// PutItemWithContext - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) PutItemWithContext(ctx aws.Context, input *dynamodb.PutItemInput, opt ...request.Option) (*dynamodb.PutItemOutput, error) {
if len(e.dynaMock.PutItemExpect) > 0 {
x := e.dynaMock.PutItemExpect[0] //get first element of expectation
if x.table != nil {
if *x.table != *input.TableName {
return &dynamodb.PutItemOutput{}, fmt.Errorf("Expect table %s but found table %s", *x.table, *input.TableName)
}
}
if x.item != nil {
if !reflect.DeepEqual(x.item, input.Item) {
return &dynamodb.PutItemOutput{}, fmt.Errorf("Expect item %+v but found item %+v", x.item, input.Item)
}
}
// delete first element of expectation
e.dynaMock.PutItemExpect = append(e.dynaMock.PutItemExpect[:0], e.dynaMock.PutItemExpect[1:]...)
return x.output, nil
}
return &dynamodb.PutItemOutput{}, fmt.Errorf("Put Item With Context Expectation Not Found")
}