|
1 | 1 | # |
2 | 2 | # |
3 | 3 |
|
4 | | -from unittest.mock import MagicMock |
| 4 | +from unittest.mock import MagicMock, patch |
5 | 5 |
|
6 | 6 | import pytest |
7 | | - |
| 7 | +from airbyte_cdk.models import AirbyteCatalog |
8 | 8 | from airbyte_cdk.sources.declarative.manifest_declarative_source import ManifestDeclarativeSource |
| 9 | +from airbyte_cdk.sources.utils.schema_helpers import check_config_against_spec_or_exit |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def test_check_config_against_spec_with_dynamic_schema_loader(): |
@@ -63,4 +64,96 @@ def test_check_config_against_spec_without_dynamic_schema_loader(): |
63 | 64 |
|
64 | 65 | source = ManifestDeclarativeSource(source_config=source_config) |
65 | 66 |
|
| 67 | + |
| 68 | +@patch("airbyte_cdk.sources.declarative.manifest_declarative_source.ManifestDeclarativeSource.streams") |
| 69 | +def test_discover_with_dynamic_schema_loader_no_config(mock_streams): |
| 70 | + """Test that discovery works without config when DynamicSchemaLoader is used.""" |
| 71 | + mock_stream = MagicMock() |
| 72 | + mock_stream.name = "test_dynamic_stream" |
| 73 | + |
| 74 | + mock_airbyte_stream = MagicMock() |
| 75 | + type(mock_airbyte_stream).name = "test_dynamic_stream" |
| 76 | + mock_stream.as_airbyte_stream.return_value = mock_airbyte_stream |
| 77 | + |
| 78 | + mock_streams.return_value = [mock_stream] |
| 79 | + |
| 80 | + source_config = { |
| 81 | + "type": "DeclarativeSource", |
| 82 | + "check": {"type": "CheckStream"}, |
| 83 | + "streams": [ |
| 84 | + { |
| 85 | + "name": "test_dynamic_stream", |
| 86 | + "schema_loader": { |
| 87 | + "type": "DynamicSchemaLoader", |
| 88 | + "retriever": { |
| 89 | + "type": "SimpleRetriever", |
| 90 | + "requester": {"url_base": "https://example.com", "http_method": "GET"}, |
| 91 | + "record_selector": {"extractor": {"field_path": []}}, |
| 92 | + }, |
| 93 | + "schema_type_identifier": { |
| 94 | + "key_pointer": ["name"], |
| 95 | + }, |
| 96 | + }, |
| 97 | + "retriever": { |
| 98 | + "type": "SimpleRetriever", |
| 99 | + "requester": {"url_base": "https://example.com", "http_method": "GET"}, |
| 100 | + "record_selector": {"extractor": {"field_path": []}}, |
| 101 | + }, |
| 102 | + } |
| 103 | + ], |
| 104 | + "version": "0.1.0", |
| 105 | + } |
| 106 | + |
| 107 | + source = ManifestDeclarativeSource(source_config=source_config) |
| 108 | + |
| 109 | + assert source.check_config_against_spec is False |
| 110 | + |
| 111 | + logger = MagicMock() |
| 112 | + catalog = source.discover(logger, {}) |
| 113 | + |
| 114 | + assert isinstance(catalog, AirbyteCatalog) |
| 115 | + assert len(catalog.streams) == 1 |
| 116 | + assert catalog.streams[0].name == "test_dynamic_stream" |
| 117 | + |
| 118 | + |
| 119 | +@patch("airbyte_cdk.sources.declarative.manifest_declarative_source.ManifestDeclarativeSource.streams") |
| 120 | +def test_discover_without_dynamic_schema_loader_no_config(mock_streams): |
| 121 | + """Test that discovery validates config when DynamicSchemaLoader is not used.""" |
| 122 | + mock_stream = MagicMock() |
| 123 | + mock_stream.name = "test_static_stream" |
| 124 | + |
| 125 | + mock_airbyte_stream = MagicMock() |
| 126 | + type(mock_airbyte_stream).name = "test_static_stream" |
| 127 | + mock_stream.as_airbyte_stream.return_value = mock_airbyte_stream |
| 128 | + |
| 129 | + mock_streams.return_value = [mock_stream] |
| 130 | + |
| 131 | + source_config = { |
| 132 | + "type": "DeclarativeSource", |
| 133 | + "check": {"type": "CheckStream"}, |
| 134 | + "streams": [ |
| 135 | + { |
| 136 | + "name": "test_static_stream", |
| 137 | + "schema_loader": {"type": "InlineSchemaLoader", "schema": {}}, |
| 138 | + "retriever": { |
| 139 | + "type": "SimpleRetriever", |
| 140 | + "requester": {"url_base": "https://example.com", "http_method": "GET"}, |
| 141 | + "record_selector": {"extractor": {"field_path": []}}, |
| 142 | + }, |
| 143 | + } |
| 144 | + ], |
| 145 | + "version": "0.1.0", |
| 146 | + } |
| 147 | + |
| 148 | + source = ManifestDeclarativeSource(source_config=source_config) |
| 149 | + |
| 150 | + assert source.check_config_against_spec is True |
| 151 | + |
| 152 | + logger = MagicMock() |
| 153 | + catalog = source.discover(logger, {}) |
| 154 | + |
| 155 | + assert isinstance(catalog, AirbyteCatalog) |
| 156 | + assert len(catalog.streams) == 1 |
| 157 | + assert catalog.streams[0].name == "test_static_stream" |
| 158 | + |
66 | 159 | assert source.check_config_against_spec is True |
0 commit comments