forked from gusaul/go-dynamock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transact_write_items.go
83 lines (64 loc) · 2.57 KB
/
transact_write_items.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
package dynamock
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
// Table - method for set Table expectation
func (e *TransactWriteItemsExpectation) Table(table string) *TransactWriteItemsExpectation {
e.table = &table
return e
}
// WillReturns - method for set desired result
func (e *TransactWriteItemsExpectation) WillReturns(res dynamodb.TransactWriteItemsOutput) *TransactWriteItemsExpectation {
e.output = &res
return e
}
// TransactWriteItems - this func will be invoked when test running matching expectation with actual input
func (e *MockDynamoDB) TransactWriteItems(input *dynamodb.TransactWriteItemsInput) (*dynamodb.TransactWriteItemsOutput, error){
if len(e.dynaMock.TransactWriteItemsExpect) > 0 {
x := e.dynaMock.TransactWriteItemsExpect[0] //get first element of expectation
foundTable := false
if x.table != nil {
for _, item := range input.TransactItems {
if (item.Update != nil && x.table == item.Update.TableName) ||
(item.Put != nil && x.table == item.Put.TableName) ||
(item.Delete != nil && x.table == item.Delete.TableName) {
foundTable = true
}
}
if foundTable == false {
return nil, fmt.Errorf("Expect table %s not found", *x.table)
}
}
// delete first element of expectation
e.dynaMock.TransactWriteItemsExpect = append(e.dynaMock.TransactWriteItemsExpect[:0],
e.dynaMock.TransactWriteItemsExpect[1:]...)
return x.output, nil
}
return nil, fmt.Errorf("Transact Write Items Table Expectation Not Found")
}
func (e *MockDynamoDB) TransactWriteItemsWithContext(ctx aws.Context, input *dynamodb.TransactWriteItemsInput, opts ...request.Option) (*dynamodb.TransactWriteItemsOutput, error) {
if len(e.dynaMock.TransactWriteItemsExpect) > 0 {
x := e.dynaMock.TransactWriteItemsExpect[0] //get first element of expectation
foundTable := false
if x.table != nil {
for _, item := range input.TransactItems {
if (item.Update != nil && x.table == item.Update.TableName) ||
(item.Put != nil && x.table == item.Put.TableName) ||
(item.Delete != nil && x.table == item.Delete.TableName) {
foundTable = true
}
}
if foundTable == false {
return nil, fmt.Errorf("Expect table %s not found", *x.table)
}
}
// delete first element of expectation
e.dynaMock.TransactWriteItemsExpect = append(e.dynaMock.TransactWriteItemsExpect[:0],
e.dynaMock.TransactWriteItemsExpect[1:]...)
return x.output, nil
}
return nil, fmt.Errorf("Transact Write Items Table With Context Expectation Not Found")
}