Skip to content

Commit 6e23fab

Browse files
fix: pass metadata_file_path for content detection and use monkeypatch in tests
- Pass remote_file.uri as metadata_file_path to detect_filetype for content-based detection, preserving the extension hint for remote files. - Use monkeypatch.setattr for partition globals in tests to ensure automatic cleanup and prevent mock leakage between tests. Co-Authored-By: AJ Steers <aj@airbyte.io>
1 parent 5a5044d commit 6e23fab

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

airbyte_cdk/sources/file_based/file_types/unstructured_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ def _get_filetype(self, file: IOBase, remote_file: RemoteFile) -> Optional[FileT
424424
if file_type and file_type != FileType.UNK:
425425
return file_type
426426

427-
type_based_on_content = detect_filetype(file=cast(IO[bytes], file))
427+
type_based_on_content = detect_filetype(
428+
file=cast(IO[bytes], file), metadata_file_path=remote_file.uri
429+
)
428430
file.seek(0) # detect_filetype is reading to read the file content, so we need to reset
429431

430432
if type_based_on_content and type_based_on_content != FileType.UNK:

unit_tests/sources/file_based/file_types/test_unstructured_parser.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,13 +239,20 @@ def test_parse_records(
239239
raises,
240240
expected_records,
241241
parsing_error,
242+
monkeypatch,
242243
):
243244
mock_partition_pdf = MagicMock()
244245
mock_partition_docx = MagicMock()
245246
mock_partition_pptx = MagicMock()
246-
unstructured_parser_module.unstructured_partition_pdf = mock_partition_pdf
247-
unstructured_parser_module.unstructured_partition_docx = mock_partition_docx
248-
unstructured_parser_module.unstructured_partition_pptx = mock_partition_pptx
247+
monkeypatch.setattr(
248+
unstructured_parser_module, "unstructured_partition_pdf", mock_partition_pdf
249+
)
250+
monkeypatch.setattr(
251+
unstructured_parser_module, "unstructured_partition_docx", mock_partition_docx
252+
)
253+
monkeypatch.setattr(
254+
unstructured_parser_module, "unstructured_partition_pptx", mock_partition_pptx
255+
)
249256
stream_reader = MagicMock()
250257
mock_open(stream_reader.open_file, read_data=bytes(str(parse_result), "utf-8"))
251258
fake_file = RemoteFile(uri=FILE_URI, last_modified=datetime.now())

0 commit comments

Comments
 (0)