forked from gusaul/go-dynamock
/
put_item.go
91 lines (75 loc) · 2.83 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
81
82
83
84
85
86
87
88
89
90
91
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
}
// WillReturnError - method for set desired error result
func (e *PutItemExpectation) WillReturnError(res dynamodb.PutItemOutput, err *error) *PutItemExpectation {
e.output = &res
e.err = err
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:]...)
if x.err != nil {
return x.output, *x.err
}
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:]...)
if x.err != nil {
return x.output, *x.err
}
return x.output, nil
}
return &dynamodb.PutItemOutput{}, fmt.Errorf("Put Item With Context Expectation Not Found")
}