-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_test.go
230 lines (192 loc) · 5.81 KB
/
iterator_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright © 2023 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ais
import (
"context"
"errors"
"testing"
"github.com/conduitio/conduit-commons/opencdc"
"github.com/machinebox/graphql"
"github.com/matryer/is"
"github.com/stretchr/testify/mock"
)
type MockGraphQLClient struct {
RunFn func(ctx context.Context, req *graphql.Request, resp interface{}) error
}
func (m *MockGraphQLClient) Run(ctx context.Context, req *graphql.Request, resp interface{}) error {
return m.RunFn(ctx, req, resp)
}
type MockLogger struct {
mock.Mock
}
func (m *MockLogger) Info(args ...interface{}) {
m.Called(args...)
}
func TestIterator(t *testing.T) {
t.Run("NewIterator", func(t *testing.T) {
is := is.New(t)
client := &graphql.Client{}
token := "test-token"
query := "test-query"
batchSize := 100
position := opencdc.Position("test-position")
it, err := NewIterator(client, token, query, batchSize, position)
is.NoErr(err)
is.Equal(client, it.client)
is.Equal(token, it.token)
is.Equal(query, it.query)
// is.Equal(position, it.position)
})
t.Run("HasNext", func(t *testing.T) {
is := is.New(t)
client := &MockGraphQLClient{}
token := "test-token"
query := "test-query"
batchSize := 100
position := opencdc.Position("test-position")
it, err := NewIterator(client, token, query, batchSize, position)
is.NoErr(err)
// Set up expected behavior
expectedResponse := struct {
Vessels Vessels
}{
Vessels: Vessels{
PageInfo: PageInfo{HasNextPage: false, EndCursor: ""},
Nodes: []Node{},
},
}
client.RunFn = func(ctx context.Context, req *graphql.Request, resp interface{}) error {
arg := resp.(*struct{ Vessels Vessels })
*arg = expectedResponse
return nil
}
// hasNext is true
it.hasNext = true
is.Equal(true, it.HasNext(context.Background()))
// hasNext is false, but there are more nodes
it.hasNext = false
it.currentBatch = []Node{{}, {}}
is.Equal(true, it.HasNext(context.Background()))
// hasNext is false, and no more nodes
it.currentBatch = []Node{}
is.Equal(false, it.HasNext(context.Background()))
})
t.Run("Next", func(t *testing.T) {
is := is.New(t)
client := &MockGraphQLClient{}
token := "test-token"
query := "test-query"
batchSize := 100
position := opencdc.Position("test-position")
it, err := NewIterator(client, token, query, batchSize, position)
is.NoErr(err)
it.currentBatch = []Node{
{
UpdateTimestamp: "2021-10-01T15:00:00Z",
},
}
record, err := it.Next(context.Background())
is.NoErr(err)
is.True(record.Payload.After != nil)
})
t.Run("loadBatch_HappyPath", func(t *testing.T) {
is := is.New(t)
client := &MockGraphQLClient{}
token := "test-token"
query := "test-query"
batchSize := 100
position := opencdc.Position("test-position")
it, err := NewIterator(client, token, query, batchSize, position)
is.NoErr(err)
// Mock the GraphQL response
mockResponse := struct {
Vessels Vessels
}{
Vessels: Vessels{
Nodes: []Node{},
PageInfo: PageInfo{
HasNextPage: false,
EndCursor: "some_cursor",
},
},
}
client.RunFn = func(ctx context.Context, req *graphql.Request, resp interface{}) error {
arg := resp.(*struct{ Vessels Vessels })
*arg = mockResponse
return nil
}
err = it.loadBatch(context.Background())
is.NoErr(err)
is.Equal(mockResponse.Vessels.Nodes, it.currentBatch)
is.Equal(mockResponse.Vessels.PageInfo.HasNextPage, it.hasNext)
is.Equal(mockResponse.Vessels.PageInfo.EndCursor, it.cursor)
is.Equal([]byte(mockResponse.Vessels.PageInfo.EndCursor), it.position)
})
t.Run("loadBatch_Error", func(t *testing.T) {
is := is.New(t)
client := &MockGraphQLClient{}
token := "test-token"
query := "test-query"
batchSize := 100
position := opencdc.Position("test-position")
it, err := NewIterator(client, token, query, batchSize, position)
is.NoErr(err)
client.RunFn = func(ctx context.Context, req *graphql.Request, resp interface{}) error {
return errors.New("some error")
}
err = it.loadBatch(context.Background())
is.True(err != nil)
is.Equal(err.Error(), "error making graphQL Request: some error")
})
t.Run("loadBatch_Retry", func(t *testing.T) {
is := is.New(t)
client := &MockGraphQLClient{}
token := "test-token"
query := "test-query"
batchSize := 100
position := opencdc.Position("test-position")
it, err := NewIterator(client, token, query, batchSize, position)
is.NoErr(err)
// Mock the GraphQL response
mockResponse := struct {
Vessels Vessels
}{
Vessels: Vessels{
Nodes: []Node{},
PageInfo: PageInfo{
HasNextPage: false,
EndCursor: "some_cursor",
},
},
}
retries := 0
maxRetries := 2
client.RunFn = func(ctx context.Context, req *graphql.Request, resp interface{}) error {
if retries < maxRetries {
retries++
return errors.New("some error")
}
arg := resp.(*struct{ Vessels Vessels })
*arg = mockResponse
return nil
}
err = it.loadBatch(context.Background())
is.NoErr(err)
is.Equal(mockResponse.Vessels.Nodes, it.currentBatch)
is.Equal(mockResponse.Vessels.PageInfo.HasNextPage, it.hasNext)
is.Equal(mockResponse.Vessels.PageInfo.EndCursor, it.cursor)
is.Equal([]byte(mockResponse.Vessels.PageInfo.EndCursor), it.position)
is.Equal(retries, maxRetries) // Check if the retries have been exhausted
})
}