-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
integration_test.py
221 lines (178 loc) · 8.03 KB
/
integration_test.py
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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import base64
import json
import time
from datetime import datetime
from pathlib import Path
import pendulum
import pytest
import requests
from airbyte_cdk.models import SyncMode
from airbyte_protocol.models import ConfiguredAirbyteCatalog
from source_salesforce.api import Salesforce
from source_salesforce.source import SourceSalesforce
HERE = Path(__file__).parent
NOTE_CONTENT = "It's the note for integration test"
UPDATED_NOTE_CONTENT = "It's the updated note for integration test"
_ANY_CATALOG = ConfiguredAirbyteCatalog.parse_obj({"streams": []})
_ANY_CONFIG = {}
_ANY_STATE = {}
@pytest.fixture(scope="module")
def input_sandbox_config():
with open(HERE.parent / "secrets/config_sandbox.json", "r") as file:
return json.loads(file.read())
@pytest.fixture(scope="module")
def sf(input_sandbox_config):
sf = Salesforce(**input_sandbox_config)
sf.login()
return sf
@pytest.fixture(scope="module")
def stream_name():
return "ContentNote"
@pytest.fixture(scope="module")
def stream(input_sandbox_config, stream_name, sf):
return SourceSalesforce(_ANY_CATALOG, _ANY_CONFIG, _ANY_STATE).generate_streams(input_sandbox_config, {stream_name: None}, sf)[0]._legacy_stream
def _encode_content(text):
base64_bytes = base64.b64encode(text.encode("utf-8"))
return base64_bytes.decode("utf-8")
def create_note(stream, headers):
url = stream.url_base + f"/services/data/{stream.sf_api.version}/sobjects/{stream.name}"
note_data = {"Title": "Integration Test", "Content": _encode_content(NOTE_CONTENT)}
return requests.post(url, headers=headers, json=note_data)
def delete_note(stream, note_id, headers):
url = stream.url_base + f"/services/data/{stream.sf_api.version}/sobjects/{stream.name}/{note_id}"
return requests.delete(url, headers=headers)
def update_note(stream, note_id, headers):
url = stream.url_base + f"/services/data/{stream.sf_api.version}/sobjects/{stream.name}/{note_id}"
note_data = {"Content": _encode_content(UPDATED_NOTE_CONTENT)}
return requests.patch(url, headers=headers, json=note_data)
def get_stream_state():
return {"LastModifiedDate": pendulum.now(tz="UTC").add(days=-1).isoformat(timespec="milliseconds")}
def test_update_for_deleted_record(stream):
headers = stream.authenticator.get_auth_header()
stream_state = get_stream_state()
time.sleep(1)
response = create_note(stream, headers)
assert response.status_code == 201, "Note was not created"
created_note_id = response.json()["id"]
# A record may not be accessible right after creation. This workaround makes few attempts to receive latest record
notes = []
attempts = 10
while created_note_id not in notes:
now = pendulum.now(tz="UTC")
stream_slice = {
"start_date": now.add(days=-1).isoformat(timespec="milliseconds"),
"end_date": now.isoformat(timespec="milliseconds"),
}
notes = set(record["Id"] for record in stream.read_records(sync_mode=SyncMode.full_refresh, stream_slice=stream_slice))
try:
assert created_note_id in notes, "The stream didn't return the note we created"
break
except Exception as e:
if attempts:
time.sleep(2)
else:
raise e
attempts = attempts - 1
response = delete_note(stream, created_note_id, headers)
assert response.status_code == 204, "Note was not deleted"
# A record may still be accessible right after deletion for some time
attempts = 10
while True:
is_note_updated = False
is_deleted = False
now = pendulum.now(tz="UTC")
stream_slice = {
"start_date": now.add(days=-1).isoformat(timespec="milliseconds"),
"end_date": now.isoformat(timespec="milliseconds"),
}
for record in stream.read_records(sync_mode=SyncMode.incremental, stream_state=stream_state, stream_slice=stream_slice):
if created_note_id == record["Id"]:
is_note_updated = True
is_deleted = record["IsDeleted"]
break
try:
assert is_note_updated, "No deleted note during the sync"
assert is_deleted, "Wrong field value for deleted note during the sync"
break
except Exception as e:
if attempts:
time.sleep(2)
else:
raise e
attempts = attempts - 1
time.sleep(1)
response = update_note(stream, created_note_id, headers)
assert response.status_code == 404, "Expected an update to a deleted note to return 404"
def test_deleted_record(stream):
headers = stream.authenticator.get_auth_header()
response = create_note(stream, headers)
assert response.status_code == 201, "Note was note created"
created_note_id = response.json()["id"]
# A record may not be accessible right after creation. This workaround makes few attempts to receive latest record
notes = []
attempts = 10
while created_note_id not in notes:
now = pendulum.now(tz="UTC")
stream_slice = {
"start_date": now.add(days=-1).isoformat(timespec="milliseconds"),
"end_date": now.isoformat(timespec="milliseconds"),
}
notes = set(record["Id"] for record in stream.read_records(sync_mode=SyncMode.full_refresh, stream_slice=stream_slice))
try:
assert created_note_id in notes, "No created note during the sync"
break
except Exception as e:
if attempts:
time.sleep(2)
else:
raise e
attempts = attempts - 1
response = update_note(stream, created_note_id, headers)
assert response.status_code == 204, "Note was not updated"
stream_state = get_stream_state()
response = delete_note(stream, created_note_id, headers)
assert response.status_code == 204, "Note was not deleted"
# A record updates take some time to become accessible
attempts = 10
while created_note_id not in notes:
now = pendulum.now(tz="UTC")
stream_slice = {
"start_date": now.add(days=-1).isoformat(timespec="milliseconds"),
"end_date": now.isoformat(timespec="milliseconds"),
}
record = None
for record in stream.read_records(sync_mode=SyncMode.incremental, stream_state=stream_state, stream_slice=stream_slice):
if created_note_id == record["Id"]:
break
try:
assert record, "No updated note during the sync"
assert record["IsDeleted"], "Wrong field value for deleted note during the sync"
assert record["TextPreview"] == UPDATED_NOTE_CONTENT and record["TextPreview"] != NOTE_CONTENT, "Note Content was not updated"
break
except Exception as e:
if attempts:
time.sleep(2)
else:
raise e
attempts = attempts - 1
def test_parallel_discover(input_sandbox_config):
sf = Salesforce(**input_sandbox_config)
sf.login()
stream_objects = sf.get_validated_streams(config=input_sandbox_config)
# try to load all schema with the old consecutive logic
consecutive_schemas = {}
start_time = datetime.now()
for stream_name, sobject_options in stream_objects.items():
consecutive_schemas[stream_name] = sf.generate_schema(stream_name, sobject_options)
consecutive_loading_time = (datetime.now() - start_time).total_seconds()
start_time = datetime.now()
parallel_schemas = sf.generate_schemas(stream_objects)
parallel_loading_time = (datetime.now() - start_time).total_seconds()
print(f"\nparallel discover ~ {round(consecutive_loading_time/parallel_loading_time, 1)}x faster over traditional.\n")
assert parallel_loading_time < consecutive_loading_time, "parallel should be more than 10x faster"
assert set(consecutive_schemas.keys()) == set(parallel_schemas.keys())
for stream_name, schema in consecutive_schemas.items():
assert schema == parallel_schemas[stream_name]