Skip to content

Commit 9933a52

Browse files
committed
file-mode-api: add more tests and make internal check of include files to always return bool
1 parent 8e54720 commit 9933a52

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

airbyte_cdk/sources/declarative/manifest_declarative_source.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,16 @@ def streams(
179179
def _get_include_files(
180180
stream_config: Dict[str, Any],
181181
catalog_with_streams_name: Mapping[str, ConfiguredAirbyteStream] | None,
182-
) -> Optional[bool]:
182+
) -> bool:
183183
"""
184184
Returns the include_files for the stream if it exists in the catalog.
185185
"""
186186
if catalog_with_streams_name:
187187
stream_name = stream_config.get("name")
188-
configured_catalog_stream = catalog_with_streams_name.get(stream_name)
189-
return configured_catalog_stream and configured_catalog_stream.include_files
188+
configured_catalog_stream = (
189+
catalog_with_streams_name.get(stream_name) if stream_name else None
190+
)
191+
return bool(configured_catalog_stream and configured_catalog_stream.include_files)
190192
return False
191193

192194
@staticmethod

unit_tests/sources/declarative/file/test_file_stream.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ def test_get_article_attachments_with_filename_extractor(self) -> None:
274274
)
275275
assert file_reference.file_size_bytes == file_size
276276

277-
def test_get_article_attachments_without_include_files(self) -> None:
278-
"""Test that article attachments can be read without including files, it can be opt-out by configured catalog"""
277+
def test_get_article_attachments_with_include_files_false(self) -> None:
278+
"""Test that article attachments can be read with including files False, it can be opt-out by configured catalog"""
279279
include_files = False
280280
with (
281281
HttpMocker() as http_mocker,
@@ -323,16 +323,52 @@ def test_get_article_attachments_without_include_files(self) -> None:
323323
# Ensure that NoopFileWriter is called to simulate file writing
324324
mock_noop_write.assert_called()
325325
file_reference = output.records[0].record.file_reference
326-
assert file_reference
327-
assert (
328-
file_reference.staging_file_url
329-
== "/tmp/airbyte-file-transfer/article_attachments/12138758717583/some_image_name.png"
326+
assert file_reference.file_size_bytes == NoopFileWriter.NOOP_FILE_SIZE
327+
328+
def test_get_article_attachments_without_include_files(self) -> None:
329+
"""Test that article attachments can be read without including files, it can be opt-out by configured catalog"""
330+
with (
331+
HttpMocker() as http_mocker,
332+
patch(
333+
"airbyte_cdk.sources.declarative.retrievers.file_uploader.noop_file_writer.NoopFileWriter.write"
334+
) as mock_noop_write,
335+
patch(
336+
"airbyte_cdk.sources.declarative.retrievers.file_uploader.local_file_system_file_writer.LocalFileSystemFileWriter.write"
337+
) as mock_file_system_write,
338+
):
339+
http_mocker.get(
340+
HttpRequest(url=STREAM_URL),
341+
HttpResponse(json.dumps(find_template("file_api/articles", __file__)), 200),
342+
)
343+
http_mocker.get(
344+
HttpRequest(url=STREAM_ATTACHMENTS_URL),
345+
HttpResponse(
346+
json.dumps(find_template("file_api/article_attachments", __file__)), 200
347+
),
348+
)
349+
http_mocker.get(
350+
HttpRequest(url=STREAM_ATTACHMENT_CONTENT_URL),
351+
HttpResponse(
352+
find_binary_response("file_api/article_attachment_content.png", __file__), 200
353+
),
330354
)
331355

332-
assert file_reference.source_file_relative_path
333-
assert not re.match(
334-
r"^article_attachments/[0-9a-fA-F-]{36}$", file_reference.source_file_relative_path
356+
mock_noop_write.return_value = NoopFileWriter.NOOP_FILE_SIZE
357+
358+
output = read(
359+
self._config(),
360+
CatalogBuilder()
361+
.with_stream(ConfiguredAirbyteStreamBuilder().with_name("article_attachments"))
362+
.build(),
363+
yaml_file="test_file_stream_with_filename_extractor.yaml",
335364
)
365+
366+
assert len(output.records) == 1
367+
# Ensure that LocalFileSystemFileWriter is not called when include_files is False
368+
mock_file_system_write.assert_not_called()
369+
# Ensure that NoopFileWriter is called to simulate file writing
370+
mock_noop_write.assert_called()
371+
file_reference = output.records[0].record.file_reference
336372
assert file_reference.file_size_bytes == NoopFileWriter.NOOP_FILE_SIZE
337373

338374
def test_get_article_attachments_messages_for_connector_builder(self) -> None:

0 commit comments

Comments
 (0)