-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_for_test.go
111 lines (101 loc) · 3.15 KB
/
helpers_for_test.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
99
100
101
102
103
104
105
106
107
108
109
110
111
package pqxd
import (
"database/sql/driver"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/miyamo2/pqxd/internal"
"go.uber.org/mock/gomock"
"testing"
)
var CmpAttributeValuesOpt = []cmp.Option{
cmp.AllowUnexported(types.AttributeValueMemberB{}),
cmp.AllowUnexported(types.AttributeValueMemberBOOL{}),
cmp.AllowUnexported(types.AttributeValueMemberBS{}),
cmp.AllowUnexported(types.AttributeValueMemberL{}),
cmp.AllowUnexported(types.AttributeValueMemberM{}),
cmp.AllowUnexported(types.AttributeValueMemberN{}),
cmp.AllowUnexported(types.AttributeValueMemberNS{}),
cmp.AllowUnexported(types.AttributeValueMemberNULL{}),
cmp.AllowUnexported(types.AttributeValueMemberS{}),
cmp.AllowUnexported(types.AttributeValueMemberSS{}),
}
var CmpExecuteStatementInputOpt = append([]cmp.Option{
cmpopts.IgnoreFields(
dynamodb.ExecuteStatementInput{},
"noSmithyDocumentSerde",
),
}, CmpAttributeValuesOpt...)
func CndExecuteStatementInput(t *testing.T, want *dynamodb.ExecuteStatementInput) func(x any) bool {
t.Helper()
return func(got any) bool {
if got == nil {
return want == nil
}
if want == nil {
return false
}
actual, ok := got.(*dynamodb.ExecuteStatementInput)
if !ok {
return false
}
if actual == nil {
return false
}
if diff := cmp.Diff(*actual, *want, CmpExecuteStatementInputOpt...); diff != "" {
t.Fatalf("unexpected difference: %v", diff)
return false
}
return true
}
}
func MustPartiQLParameters(t *testing.T, args []driver.NamedValue) []types.AttributeValue {
t.Helper()
params, err := toPartiQLParameters(args)
if err != nil {
t.Fatalf("failed to convert to PartiQL parameters: %v", err)
}
return params
}
type ExecuteStatementResult struct {
out *dynamodb.ExecuteStatementOutput
err error
}
type MockDynamoDBClientOption func(*internal.MockDynamoDBClient)
func MockDynamoDBClientWithExecuteStatement(t *testing.T, input dynamodb.ExecuteStatementInput, executeStatementResults []ExecuteStatementResult) func(client *internal.MockDynamoDBClient) {
return func(client *internal.MockDynamoDBClient) {
for _, esr := range executeStatementResults {
client.EXPECT().
ExecuteStatement(gomock.Any(), gomock.Cond(CndExecuteStatementInput(t, &input))).
Times(1).
Return(esr.out, esr.err)
input = dynamodb.ExecuteStatementInput{
Statement: input.Statement,
Parameters: input.Parameters,
NextToken: esr.out.NextToken,
}
}
}
}
func MockDynamoDBClient(t *testing.T, ctrl *gomock.Controller, opts ...MockDynamoDBClientOption) internal.DynamoDBClient {
t.Helper()
client := internal.NewMockDynamoDBClient(ctrl)
for _, opt := range opts {
opt(client)
}
return client
}
func GetAllResultSet(t *testing.T, rows driver.Rows) ([][]map[string]types.AttributeValue, error) {
t.Helper()
var results [][]map[string]types.AttributeValue
switch rs := (any)(rows).(type) {
case *pqxdRows:
for rs.NextResultSet() == nil {
resultSet := *rs.out.Load()
results = append(results, resultSet)
rs.outCursor.Store(uint32(len(resultSet)))
}
}
return results, nil
}