-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_test.go
266 lines (226 loc) · 8.46 KB
/
acceptance_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright © 2022 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 hubspot
import (
"context"
"net/http"
"os"
"testing"
"time"
"github.com/brianvoe/gofakeit/v6"
"github.com/conduitio-labs/conduit-connector-hubspot/config"
"github.com/conduitio-labs/conduit-connector-hubspot/hubspot"
"github.com/conduitio-labs/conduit-connector-hubspot/source"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-connector-sdk"
"go.uber.org/goleak"
)
// testResource is a test resource that we use for integration tests.
const testResource = "crm.contacts"
// acceptanceTestTimeout is a timeout used for both read and write.
const acceptanceTestTimeout = time.Second * 20
// The list of HubSpot field names that are used in acceptance tests.
const (
testIDFieldName = "id"
testEmailFieldName = "email"
testFirstNameFieldName = "firstname"
testLastNameFieldName = "lastname"
)
// testAccessToken holds a value of HUBSPOT_ACCESS_TOKEN which is required for the acceptance tests.
var testAccessToken = os.Getenv("HUBSPOT_ACCESS_TOKEN")
type driver struct {
sdk.ConfigurableAcceptanceTestDriver
hubspotClient *hubspot.Client
}
// GenerateRecord overides the [sdk.ConfigurableAcceptanceTestDriver] [GenerateRecord] method.
func (d driver) GenerateRecord(t *testing.T, operation opencdc.Operation) opencdc.Record {
t.Helper()
return opencdc.Record{
Operation: operation,
Payload: opencdc.Change{
After: opencdc.StructuredData{
"properties": map[string]any{
testFirstNameFieldName: gofakeit.FirstName(),
testLastNameFieldName: gofakeit.LastName(),
testEmailFieldName: gofakeit.Email(),
},
},
},
}
}
// ReadFromDestination overrides the [sdk.ConfigurableAcceptanceTestDriver] [ReadFromDestination] method.
// It removes some redundant fields that are unknown when we insert data.
func (d driver) ReadFromDestination(t *testing.T, records []opencdc.Record) []opencdc.Record {
t.Helper()
// the search endpoint lags behind, query it and wait for all results to disappear
hubspotClient := hubspot.NewClient(testAccessToken, &http.Client{
Timeout: acceptanceTestTimeout,
})
for i := 0; i < 5; i++ {
time.Sleep(time.Second * 5)
listResp, err := hubspotClient.Search(context.Background(), testResource, &hubspot.SearchRequest{})
if err != nil {
t.Fatalf("error querying search: %v", err)
}
if listResp.Total >= len(records) {
break
}
}
newRecords := d.ConfigurableAcceptanceTestDriver.ReadFromDestination(t, records)
out := make([]opencdc.Record, len(newRecords))
for i, newRecord := range newRecords {
newRecordStructuredPayload, ok := newRecord.Payload.After.(opencdc.StructuredData)
if !ok {
// this shouldn't happen
t.Fatalf("expected payload to contain opencdc.StructuredData, got %T", newRecord.Payload.After)
}
out[i] = opencdc.Record{
Operation: newRecord.Operation,
Position: newRecord.Position,
Payload: opencdc.Change{
After: opencdc.StructuredData{
"properties": map[string]any{
testIDFieldName: newRecordStructuredPayload[testIDFieldName],
testFirstNameFieldName: newRecordStructuredPayload[testFirstNameFieldName],
testLastNameFieldName: newRecordStructuredPayload[testLastNameFieldName],
testEmailFieldName: newRecordStructuredPayload[testEmailFieldName],
},
},
},
}
}
return records
}
// WriteToSource overrides the [sdk.ConfigurableAcceptanceTestDriver] [WriteToSource] method.
// It takes items from HubSpot and returns them as [opencdc.Record]s, because Source returns fields
// that are unknown when we insert them.
func (d driver) WriteToSource(t *testing.T, records []opencdc.Record) []opencdc.Record {
t.Helper()
newRecords := d.ConfigurableAcceptanceTestDriver.WriteToSource(t, records)
listResp, err := d.hubspotClient.List(context.Background(), testResource, nil)
if err != nil {
t.Errorf("list items: %v", err)
}
// create a map that holds emails and corresponding list response results
listRespMap := make(map[string]hubspot.ListResponseResult)
for _, listRespResult := range listResp.Results {
listRespResultName, ok := listRespResult["properties"].(map[string]any)[testEmailFieldName].(string)
if !ok {
t.Errorf("list resp result email is not a string or doesn't exist")
}
listRespMap[listRespResultName] = listRespResult
}
// fill records payload with the newly created HubSpot items properties
for i := range newRecords {
recordPayloadAfter, ok := newRecords[i].Payload.After.(opencdc.StructuredData)
if !ok {
t.Fatal("record's payload after is not structured")
}
recordPayloadAfterProperties, ok := recordPayloadAfter["properties"].(map[string]any)
if !ok {
t.Fatal("record's payload after does not contain properties or it's not a map")
}
recordPayloadAfterEmail, ok := recordPayloadAfterProperties[testEmailFieldName].(string)
if !ok {
t.Fatal("record's payload after email is not a string or doesn't exist")
}
listRespResult, ok := listRespMap[recordPayloadAfterEmail]
if !ok {
t.Fatal("can't find a list resp result by email")
}
newRecords[i].Key = opencdc.StructuredData{hubspot.ResultsFieldID: listRespResult["id"]}
newRecords[i].Payload.After = opencdc.StructuredData(listRespResult)
}
return newRecords
}
func TestAcceptance(t *testing.T) {
if testAccessToken == "" {
t.Skip("HUBSPOT_ACCESS_TOKEN env var must be set")
}
cfg := map[string]string{
config.KeyAccessToken: testAccessToken,
config.KeyResource: testResource,
source.ConfigKeyPollingPeriod: "1s",
}
sdk.AcceptanceTest(t, driver{
ConfigurableAcceptanceTestDriver: sdk.ConfigurableAcceptanceTestDriver{
Config: sdk.ConfigurableAcceptanceTestDriverConfig{
Connector: Connector,
SourceConfig: cfg,
DestinationConfig: cfg,
GoleakOptions: []goleak.Option{
// these leaks mainly come from the go-retryablehttp
goleak.IgnoreTopFunction("internal/poll.runtime_pollWait"),
goleak.IgnoreTopFunction("net/http.(*persistConn).writeLoop"),
},
AfterTest: afterTest,
ReadTimeout: acceptanceTestTimeout,
WriteTimeout: acceptanceTestTimeout,
Skip: []string{
// ResumeAtPosition tests are failing, it's hard to get a
// stable test run with the hubspot API as the search API
// has quite a delay between writes and reads. Also, the
// "updatedAt" field gets updated randomly even after the
// resource has been created.
// To top it off, the API has a rate limit which we are
// hitting, so we can easily hit the test timeout.
"TestSource_Open_ResumeAtPositionCDC",
"TestSource_Open_ResumeAtPositionSnapshot",
// TestDestination_Write_Success is also flaky, although
// succeeds more often than not.
},
},
},
hubspotClient: hubspot.NewClient(testAccessToken, &http.Client{
Timeout: acceptanceTestTimeout,
}),
})
}
// afterTest is a test helper that deletes all resource items after each test.
func afterTest(t *testing.T) {
t.Helper()
ctx := context.Background()
hubspotClient := hubspot.NewClient(testAccessToken, &http.Client{
Timeout: acceptanceTestTimeout,
})
listResp, err := hubspotClient.List(ctx, testResource, nil)
if err != nil {
t.Errorf("hubspot list: %v", err)
}
for _, item := range listResp.Results {
itemID, ok := item[testIDFieldName].(string)
if !ok {
t.Errorf("item is not a string")
}
if err := hubspotClient.Delete(context.Background(), testResource, itemID); err != nil {
t.Errorf("hubspot delete: %v", err)
}
}
// the search endpoint lags behind, query it and wait for all results to disappear
searchEmpty := false
for i := 0; i < 5; i++ {
listResp, err := hubspotClient.Search(ctx, testResource, &hubspot.SearchRequest{})
if err != nil {
t.Fatalf("error querying search: %v", err)
}
if listResp.Total == 0 {
searchEmpty = true
break
}
time.Sleep(time.Second * 5)
}
if !searchEmpty {
t.Log("WARNING: hubspot still returns data in the search endpoint, next tests might fail because of this")
}
}