Skip to content

Commit aa0a832

Browse files
committed
file-mode-api: Mock the test instead of messing with Classes
1 parent 2035956 commit aa0a832

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

airbyte_cdk/sources/declarative/yaml_declarative_source.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def __init__(
2424
catalog: Optional[ConfiguredAirbyteCatalog] = None,
2525
config: Optional[Mapping[str, Any]] = None,
2626
state: Optional[List[AirbyteStateMessage]] = None,
27-
emit_connector_builder_messages: Optional[bool] = False,
2827
) -> None:
2928
"""
3029
:param path_to_yaml: Path to the yaml file describing the source
@@ -36,8 +35,7 @@ def __init__(
3635
catalog=catalog or ConfiguredAirbyteCatalog(streams=[]),
3736
config=config or {},
3837
state=state or [],
39-
source_config=source_config,
40-
emit_connector_builder_messages=emit_connector_builder_messages or False,
38+
source_config=source_config
4139
)
4240

4341
def _read_and_parse_yaml_file(self, path_to_yaml_file: str) -> ConnectionDefinition:

unit_tests/sources/declarative/file/test_file_stream.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from pathlib import Path
44
from typing import Any, Dict, List, Optional
55
from unittest import TestCase
6-
from unittest.mock import Mock
6+
from unittest.mock import Mock, patch
77

88
from airbyte_cdk.models import AirbyteStateMessage, ConfiguredAirbyteCatalog, Status
9+
from airbyte_cdk.sources.declarative.parsers.model_to_component_factory import ModelToComponentFactory as OriginalModelToComponentFactory
910
from airbyte_cdk.sources.declarative.retrievers.file_uploader.noop_file_writer import NoopFileWriter
1011
from airbyte_cdk.sources.declarative.yaml_declarative_source import YamlDeclarativeSource
1112
from airbyte_cdk.test.catalog_builder import CatalogBuilder, ConfiguredAirbyteStreamBuilder
@@ -35,16 +36,14 @@ def _source(
3536
config: Dict[str, Any],
3637
state: Optional[List[AirbyteStateMessage]] = None,
3738
yaml_file: Optional[str] = None,
38-
emit_connector_builder_messages: Optional[bool] = False,
3939
) -> YamlDeclarativeSource:
4040
if not yaml_file:
4141
yaml_file = "file_stream_manifest.yaml"
4242
return YamlDeclarativeSource(
4343
path_to_yaml=str(Path(__file__).parent / yaml_file),
4444
catalog=catalog,
4545
config=config,
46-
state=state,
47-
emit_connector_builder_messages=emit_connector_builder_messages,
46+
state=state
4847
)
4948

5049

@@ -53,13 +52,12 @@ def read(
5352
catalog: ConfiguredAirbyteCatalog,
5453
state_builder: Optional[StateBuilder] = None,
5554
expecting_exception: bool = False,
56-
yaml_file: Optional[str] = None,
57-
emit_connector_builder_messages: Optional[bool] = False,
55+
yaml_file: Optional[str] = None
5856
) -> EntrypointOutput:
5957
config = config_builder.build()
6058
state = state_builder.build() if state_builder else StateBuilder().build()
6159
return entrypoint_read(
62-
_source(catalog, config, state, yaml_file, emit_connector_builder_messages),
60+
_source(catalog, config, state, yaml_file),
6361
config,
6462
catalog,
6563
state,
@@ -185,7 +183,7 @@ def test_get_article_attachments_with_filename_extractor(self) -> None:
185183
yaml_file="test_file_stream_with_filename_extractor.yaml",
186184
)
187185

188-
assert output.records
186+
assert len(output.records) == 1
189187
file_reference = output.records[0].record.file_reference
190188
assert file_reference
191189
assert (
@@ -217,30 +215,40 @@ def test_get_article_attachments_messages_for_connector_builder(self) -> None:
217215
),
218216
)
219217

220-
output = read(
221-
self._config(),
222-
CatalogBuilder()
223-
.with_stream(ConfiguredAirbyteStreamBuilder().with_name("article_attachments"))
224-
.build(),
225-
yaml_file="test_file_stream_with_filename_extractor.yaml",
226-
emit_connector_builder_messages=True,
227-
)
218+
# Define a mock factory that forces emit_connector_builder_messages=True
219+
class MockModelToComponentFactory(OriginalModelToComponentFactory):
220+
def __init__(self, *args, **kwargs):
221+
kwargs['emit_connector_builder_messages'] = True
222+
super().__init__(*args, **kwargs)
228223

229-
assert len(output.records) == 1
230-
file_reference = output.records[0].record.file_reference
231-
assert file_reference
232-
assert file_reference.staging_file_url
233-
assert file_reference.source_file_relative_path
234-
# because we didn't write the file, the size is 0
235-
assert file_reference.file_size_bytes == NoopFileWriter.NOOP_FILE_SIZE
224+
# Patch the factory class where ConcurrentDeclarativeSource (parent of YamlDeclarativeSource) imports it
225+
with patch(
226+
"airbyte_cdk.sources.declarative.concurrent_declarative_source.ModelToComponentFactory",
227+
new=MockModelToComponentFactory
228+
):
229+
output = read(
230+
self._config(),
231+
CatalogBuilder()
232+
.with_stream(ConfiguredAirbyteStreamBuilder().with_name("article_attachments"))
233+
.build(),
234+
yaml_file="test_file_stream_with_filename_extractor.yaml"
235+
)
236236

237-
# Assert file reference fields are copied to record data
238-
record_data = output.records[0].record.data
239-
assert record_data["staging_file_url"] == file_reference.staging_file_url
240-
assert (
241-
record_data["source_file_relative_path"] == file_reference.source_file_relative_path
242-
)
243-
assert record_data["file_size_bytes"] == file_reference.file_size_bytes
237+
assert len(output.records) == 1
238+
file_reference = output.records[0].record.file_reference
239+
assert file_reference
240+
assert file_reference.staging_file_url
241+
assert file_reference.source_file_relative_path
242+
# because we didn't write the file, the size is NOOP_FILE_SIZE
243+
assert file_reference.file_size_bytes == NoopFileWriter.NOOP_FILE_SIZE
244+
245+
# Assert file reference fields are copied to record data
246+
record_data = output.records[0].record.data
247+
assert record_data["staging_file_url"] == file_reference.staging_file_url
248+
assert (
249+
record_data["source_file_relative_path"] == file_reference.source_file_relative_path
250+
)
251+
assert record_data["file_size_bytes"] == file_reference.file_size_bytes
244252

245253
def test_discover_article_attachments(self) -> None:
246254
output = discover(self._config())

0 commit comments

Comments
 (0)