-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtest_file_based_stream_reader.py
More file actions
528 lines (481 loc) · 16.3 KB
/
test_file_based_stream_reader.py
File metadata and controls
528 lines (481 loc) · 16.3 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import logging
from datetime import datetime
from io import IOBase
from os import path
from typing import Any, ClassVar, Dict, Iterable, List, Mapping, Optional, Set
from unittest.mock import MagicMock
import pytest
from pydantic.v1 import AnyUrl
from airbyte_cdk.sources.file_based.config.abstract_file_based_spec import AbstractFileBasedSpec
from airbyte_cdk.sources.file_based.exceptions import FileSizeLimitError
from airbyte_cdk.sources.file_based.file_based_stream_reader import AbstractFileBasedStreamReader
from airbyte_cdk.sources.file_based.remote_file import RemoteFile, UploadableRemoteFile
from airbyte_cdk.sources.utils.files_directory import get_files_directory
from unit_tests.sources.file_based.helpers import make_remote_files
reader = AbstractFileBasedStreamReader
files_directory = get_files_directory()
"""
The rules are:
- All files at top-level: /*
- All files at top-level of mydir: mydir/*
- All files anywhere under mydir: mydir/**/*
- All files in any directory: **/*
- All files in any directory that end in .csv: **/*.csv
- All files in any directory that have a .csv extension: **/*.csv*
"""
FILEPATHS = [
"a",
"a.csv",
"a.csv.gz",
"a.jsonl",
"a/b",
"a/b.csv",
"a/b.csv.gz",
"a/b.jsonl",
"a/c",
"a/c.csv",
"a/c.csv.gz",
"a/c.jsonl",
"a/b/c",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/b/c.jsonl",
"a/c/c",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/c/c.jsonl",
"a/b/c/d",
"a/b/c/d.csv",
"a/b/c/d.csv.gz",
"a/b/c/d.jsonl",
]
FILES = make_remote_files(FILEPATHS)
DEFAULT_CONFIG = {
"streams": [],
}
class TestStreamReaderWithDefaultUpload(AbstractFileBasedStreamReader):
__test__: ClassVar[bool] = False # Tell Pytest this is not a Pytest class, despite its name
@property
def config(self) -> Optional[AbstractFileBasedSpec]:
return self._config
@config.setter
def config(self, value: AbstractFileBasedSpec) -> None:
self._config = value
def get_matching_files(self, globs: List[str]) -> Iterable[RemoteFile]:
pass
def open_file(self, file: RemoteFile) -> IOBase:
pass
def get_file_acl_permissions(self, file: RemoteFile, logger: logging.Logger) -> Dict[str, Any]:
return {}
def load_identity_groups(self, logger: logging.Logger) -> Iterable[Dict[str, Any]]:
return [{}]
@property
def file_permissions_schema(self) -> Dict[str, Any]:
return {"type": "object", "properties": {}}
@property
def identities_schema(self) -> Dict[str, Any]:
return {"type": "object", "properties": {}}
class TestStreamReader(AbstractFileBasedStreamReader):
__test__: ClassVar[bool] = False # Tell Pytest this is not a Pytest class, despite its name
@property
def config(self) -> Optional[AbstractFileBasedSpec]:
return self._config
@config.setter
def config(self, value: AbstractFileBasedSpec) -> None:
self._config = value
def get_matching_files(self, globs: List[str]) -> Iterable[RemoteFile]:
pass
def open_file(self, file: RemoteFile) -> IOBase:
pass
def file_size(self, file: RemoteFile) -> int:
return 0
def upload(
self, file: RemoteFile, local_directory: str, logger: logging.Logger
) -> Dict[str, Any]:
return {}
def get_file_acl_permissions(self, file: RemoteFile, logger: logging.Logger) -> Dict[str, Any]:
return {}
def load_identity_groups(self, logger: logging.Logger) -> Iterable[Dict[str, Any]]:
return [{}]
@property
def file_permissions_schema(self) -> Dict[str, Any]:
return {"type": "object", "properties": {}}
@property
def identities_schema(self) -> Dict[str, Any]:
return {"type": "object", "properties": {}}
class TestSpec(AbstractFileBasedSpec):
__test__: ClassVar[bool] = (
False # Prevent pytest from thinking that this is a test class, despite the name
)
@classmethod
def documentation_url(cls) -> AnyUrl:
return AnyUrl(scheme="https", url="https://docs.airbyte.com/integrations/sources/test") # type: ignore
@pytest.mark.parametrize(
"globs,config,expected_matches,expected_path_prefixes",
[
pytest.param([], DEFAULT_CONFIG, set(), set(), id="no-globs"),
pytest.param([""], DEFAULT_CONFIG, set(), set(), id="empty-string"),
pytest.param(["**"], DEFAULT_CONFIG, set(FILEPATHS), set(), id="**"),
pytest.param(
["**/*.csv"],
DEFAULT_CONFIG,
{"a.csv", "a/b.csv", "a/c.csv", "a/b/c.csv", "a/c/c.csv", "a/b/c/d.csv"},
set(),
id="**/*.csv",
),
pytest.param(
["**/*.csv*"],
DEFAULT_CONFIG,
{
"a.csv",
"a.csv.gz",
"a/b.csv",
"a/b.csv.gz",
"a/c.csv",
"a/c.csv.gz",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/b/c/d.csv",
"a/b/c/d.csv.gz",
},
set(),
id="**/*.csv*",
),
pytest.param(["*"], DEFAULT_CONFIG, {"a", "a.csv", "a.csv.gz", "a.jsonl"}, set(), id="*"),
pytest.param(["*.csv"], DEFAULT_CONFIG, {"a.csv"}, set(), id="*.csv"),
pytest.param(["*.csv*"], DEFAULT_CONFIG, {"a.csv", "a.csv.gz"}, set(), id="*.csv*"),
pytest.param(
["*/*"],
DEFAULT_CONFIG,
{
"a/b",
"a/b.csv",
"a/b.csv.gz",
"a/b.jsonl",
"a/c",
"a/c.csv",
"a/c.csv.gz",
"a/c.jsonl",
},
set(),
id="*/*",
),
pytest.param(["*/*.csv"], DEFAULT_CONFIG, {"a/b.csv", "a/c.csv"}, set(), id="*/*.csv"),
pytest.param(
["*/*.csv*"],
DEFAULT_CONFIG,
{"a/b.csv", "a/b.csv.gz", "a/c.csv", "a/c.csv.gz"},
set(),
id="*/*.csv*",
),
pytest.param(
["*/**"],
DEFAULT_CONFIG,
{
"a/b",
"a/b.csv",
"a/b.csv.gz",
"a/b.jsonl",
"a/c",
"a/c.csv",
"a/c.csv.gz",
"a/c.jsonl",
"a/b/c",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/b/c.jsonl",
"a/c/c",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/c/c.jsonl",
"a/b/c/d",
"a/b/c/d.csv",
"a/b/c/d.csv.gz",
"a/b/c/d.jsonl",
},
set(),
id="*/**",
),
pytest.param(
["a/*"],
DEFAULT_CONFIG,
{
"a/b",
"a/b.csv",
"a/b.csv.gz",
"a/b.jsonl",
"a/c",
"a/c.csv",
"a/c.csv.gz",
"a/c.jsonl",
},
{"a/"},
id="a/*",
),
pytest.param(["a/*.csv"], DEFAULT_CONFIG, {"a/b.csv", "a/c.csv"}, {"a/"}, id="a/*.csv"),
pytest.param(
["a/*.csv*"],
DEFAULT_CONFIG,
{"a/b.csv", "a/b.csv.gz", "a/c.csv", "a/c.csv.gz"},
{"a/"},
id="a/*.csv*",
),
pytest.param(
["a/b/*"],
DEFAULT_CONFIG,
{"a/b/c", "a/b/c.csv", "a/b/c.csv.gz", "a/b/c.jsonl"},
{"a/b/"},
id="a/b/*",
),
pytest.param(["a/b/*.csv"], DEFAULT_CONFIG, {"a/b/c.csv"}, {"a/b/"}, id="a/b/*.csv"),
pytest.param(
["a/b/*.csv*"], DEFAULT_CONFIG, {"a/b/c.csv", "a/b/c.csv.gz"}, {"a/b/"}, id="a/b/*.csv*"
),
pytest.param(
["a/*/*"],
DEFAULT_CONFIG,
{
"a/b/c",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/b/c.jsonl",
"a/c/c",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/c/c.jsonl",
},
{"a/"},
id="a/*/*",
),
pytest.param(
["a/*/*.csv"], DEFAULT_CONFIG, {"a/b/c.csv", "a/c/c.csv"}, {"a/"}, id="a/*/*.csv"
),
pytest.param(
["a/*/*.csv*"],
DEFAULT_CONFIG,
{"a/b/c.csv", "a/b/c.csv.gz", "a/c/c.csv", "a/c/c.csv.gz"},
{"a/"},
id="a/*/*.csv*",
),
pytest.param(
["a/**/*"],
DEFAULT_CONFIG,
{
"a/b",
"a/b.csv",
"a/b.csv.gz",
"a/b.jsonl",
"a/c",
"a/c.csv",
"a/c.csv.gz",
"a/c.jsonl",
"a/b/c",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/b/c.jsonl",
"a/c/c",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/c/c.jsonl",
"a/b/c/d",
"a/b/c/d.csv",
"a/b/c/d.csv.gz",
"a/b/c/d.jsonl",
},
{"a/"},
id="a/**/*",
),
pytest.param(
["a/**/*.csv"],
DEFAULT_CONFIG,
{"a/b.csv", "a/c.csv", "a/b/c.csv", "a/c/c.csv", "a/b/c/d.csv"},
{"a/"},
id="a/**/*.csv",
),
pytest.param(
["a/**/*.csv*"],
DEFAULT_CONFIG,
{
"a/b.csv",
"a/b.csv.gz",
"a/c.csv",
"a/c.csv.gz",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/b/c/d.csv",
"a/b/c/d.csv.gz",
},
{"a/"},
id="a/**/*.csv*",
),
pytest.param(
["**/*.csv", "**/*.gz"],
DEFAULT_CONFIG,
{
"a.csv",
"a.csv.gz",
"a/b.csv",
"a/b.csv.gz",
"a/c.csv",
"a/c.csv.gz",
"a/b/c.csv",
"a/b/c.csv.gz",
"a/c/c.csv",
"a/c/c.csv.gz",
"a/b/c/d.csv",
"a/b/c/d.csv.gz",
},
set(),
id="**/*.csv,**/*.gz",
),
pytest.param(
["*.csv", "*.gz"], DEFAULT_CONFIG, {"a.csv", "a.csv.gz"}, set(), id="*.csv,*.gz"
),
pytest.param(
["a/*.csv", "a/*/*.csv"],
DEFAULT_CONFIG,
{"a/b.csv", "a/c.csv", "a/b/c.csv", "a/c/c.csv"},
{"a/"},
id="a/*.csv,a/*/*.csv",
),
pytest.param(
["a/*.csv", "a/b/*.csv"],
DEFAULT_CONFIG,
{"a/b.csv", "a/c.csv", "a/b/c.csv"},
{"a/", "a/b/"},
id="a/*.csv,a/b/*.csv",
),
pytest.param(
["**/*.csv"],
{"start_date": "2023-06-01T03:54:07.000Z", "streams": []},
{"a.csv", "a/b.csv", "a/c.csv", "a/b/c.csv", "a/c/c.csv", "a/b/c/d.csv"},
set(),
id="all_csvs_modified_after_start_date",
),
pytest.param(
["**/*.csv"],
{"start_date": "2023-06-10T03:54:07.000Z", "streams": []},
set(),
set(),
id="all_csvs_modified_before_start_date",
),
pytest.param(
["**/*.csv"],
{"start_date": "2023-06-05T03:54:07.000Z", "streams": []},
{"a.csv", "a/b.csv", "a/c.csv", "a/b/c.csv", "a/c/c.csv", "a/b/c/d.csv"},
set(),
id="all_csvs_modified_exactly_on_start_date",
),
],
)
def test_globs_and_prefixes_from_globs(
globs: List[str],
config: Mapping[str, Any],
expected_matches: Set[str],
expected_path_prefixes: Set[str],
) -> None:
reader = TestStreamReader()
reader.config = TestSpec(**config)
assert (
set([f.uri for f in reader.filter_files_by_globs_and_start_date(FILES, globs)])
== expected_matches
)
assert set(reader.get_prefixes_from_globs(globs)) == expected_path_prefixes
@pytest.mark.parametrize(
"config, source_file_path, expected_file_relative_path, expected_local_file_path",
[
pytest.param(
{
"streams": [],
"delivery_method": {
"delivery_type": "use_file_transfer",
"preserve_directory_structure": True,
},
},
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
f"{files_directory}/mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
id="preserve_directories_present_and_true",
),
pytest.param(
{
"streams": [],
"delivery_method": {
"delivery_type": "use_file_transfer",
"preserve_directory_structure": False,
},
},
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
"monthly-kickoff-202402.mpeg",
f"{files_directory}/monthly-kickoff-202402.mpeg",
id="preserve_directories_present_and_false",
),
pytest.param(
{"streams": [], "delivery_method": {"delivery_type": "use_file_transfer"}},
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
f"{files_directory}/mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
id="preserve_directories_not_present_defaults_true",
),
pytest.param(
{"streams": []},
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
"mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
f"{files_directory}/mirror_paths_testing/not_duplicates/data/jan/monthly-kickoff-202402.mpeg",
id="file_transfer_flag_not_present_defaults_true",
),
],
)
def test_preserve_sub_directories_scenarios(
config: Mapping[str, Any],
source_file_path: str,
expected_file_relative_path: str,
expected_local_file_path: str,
) -> None:
"""
Test scenarios when preserve_directory_structure is True or False, the flag indicates whether we need to
use a relative path to upload the file or simply place it in the root.
"""
reader = TestStreamReader()
reader.config = TestSpec(**config)
file_paths = reader._get_file_transfer_paths(
source_file_path, staging_directory=f"{files_directory}/"
)
assert (
file_paths[AbstractFileBasedStreamReader.FILE_RELATIVE_PATH] == expected_file_relative_path
)
assert file_paths[AbstractFileBasedStreamReader.LOCAL_FILE_PATH] == expected_local_file_path
assert file_paths[AbstractFileBasedStreamReader.FILE_NAME] == path.basename(source_file_path)
assert file_paths[AbstractFileBasedStreamReader.FILE_FOLDER] == path.dirname(source_file_path)
def test_upload_with_file_transfer_reader():
stream_reader = TestStreamReaderWithDefaultUpload()
class TestUploadableRemoteFile(UploadableRemoteFile):
blob: Any
@property
def size(self) -> int:
return self.blob.size
def download_to_local_directory(self, local_file_path: str) -> None:
pass
blob = MagicMock()
blob.size = 200
uploadable_remote_file = TestUploadableRemoteFile(
uri="test/uri", last_modified=datetime.now(), blob=blob
)
logger = logging.getLogger("airbyte")
file_record_data, file_reference = stream_reader.upload(
uploadable_remote_file, "test_directory", logger
)
assert file_record_data
assert file_reference
blob.size = 2_500_000_000
with pytest.raises(FileSizeLimitError):
stream_reader.upload(uploadable_remote_file, "test_directory", logger)
with pytest.raises(FileSizeLimitError):
stream_reader.upload(uploadable_remote_file, "test_directory", logger)