-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader_test.go
128 lines (114 loc) · 3 KB
/
reader_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//go:build integration
package dynamoql_test
import (
"context"
"testing"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/maestre3d/dynamoql-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type queryReaderTestSuite struct {
suite.Suite
client *dynamodb.Client
}
func TestNewQueryReader(t *testing.T) {
suite.Run(t, &queryReaderTestSuite{})
}
func (s *queryReaderTestSuite) SetupSuite() {
s.client = newDynamoClient()
}
func (s *queryReaderTestSuite) SetupTest() {}
func (s *queryReaderTestSuite) TearDownTest() {}
func (s *queryReaderTestSuite) TearDownSuite() {}
func (s *queryReaderTestSuite) TestQueryReader_GetItem() {
tests := []struct {
name string
query dynamodb.QueryInput
pageSize int32
expItems int
wantErr bool
}{
{
name: "Empty",
query: dynamodb.QueryInput{}, // missing table
pageSize: 0,
expItems: 0,
wantErr: true,
},
{
name: "Invalid query",
query: dynamoql.NewQueryInput(dynamoql.Select().From("InvoiceAndBills").Where(dynamoql.Condition{
IsKey: true,
Operator: dynamoql.Equals,
Field: "PK",
Value: dynamoql.NewCompositeKey("I", "1191"),
}, dynamoql.Condition{
IsKey: false,
Operator: dynamoql.Equals,
Field: "SK",
Value: dynamoql.NewCompositeKey("B", ""),
})),
pageSize: 0,
expItems: 0,
wantErr: true, // AWS SDK malformed query error
},
{
name: "Valid empty result",
query: dynamoql.NewQueryInput(dynamoql.Select().From("InvoiceAndBills").Where(dynamoql.Condition{
IsKey: true,
Operator: dynamoql.Equals,
Field: "PK",
Value: dynamoql.NewCompositeKey("I", "abc"),
}, dynamoql.Condition{
IsKey: true,
Operator: dynamoql.BeginsWith,
Field: "SK",
Value: dynamoql.NewCompositeKey("B", ""),
})),
pageSize: 100,
expItems: 0,
wantErr: true, // Reader has reached end of file (ErrReaderEOF), no items found
},
{
name: "Valid",
query: dynamoql.NewQueryInput(dynamoql.Select().From("InvoiceAndBills").Where(dynamoql.Condition{
IsKey: true,
Operator: dynamoql.Equals,
Field: "PK",
Value: dynamoql.NewCompositeKey("I", "1191"),
}, dynamoql.Condition{
IsKey: true,
Operator: dynamoql.BeginsWith,
Field: "SK",
Value: dynamoql.NewCompositeKey("B", ""),
})),
pageSize: 100,
expItems: 4,
},
}
for _, tt := range tests {
s.T().Run(tt.name, func(t *testing.T) {
r := dynamoql.NewQueryReader(tt.pageSize, s.client, tt.query)
ctx := context.Background()
bills := make([]Bill, 0, tt.expItems)
for r.Next() {
item, err := r.GetItem(ctx)
require.Equal(t, tt.wantErr, err != nil)
if err != nil {
break
}
bill := Bill{}
err = bill.UnmarshalDynamoDB(item)
require.Nil(t, err)
bills = append(bills, bill)
if r.Count() >= tt.expItems {
break
}
}
assert.Equal(t, r.Count(), tt.expItems)
assert.Len(t, bills, tt.expItems)
})
}
}