Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded CDK: run a check before starting to load #29079

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
from airbyte_cdk.sources.embedded.catalog import create_configured_catalog, get_stream, get_stream_names
from airbyte_cdk.sources.embedded.runner import SourceRunner
from airbyte_cdk.sources.embedded.tools import get_defined_id
from airbyte_cdk.sources.utils.schema_helpers import check_config_against_spec_or_exit
from airbyte_protocol.models import AirbyteRecordMessage, AirbyteStateMessage, SyncMode, Type

TOutput = TypeVar("TOutput")


class BaseEmbeddedIntegration(ABC, Generic[TConfig, TOutput]):
def __init__(self, runner: SourceRunner[TConfig], config: TConfig):
check_config_against_spec_or_exit(config, runner.spec())

self.source = runner
self.config = config

Expand Down
9 changes: 8 additions & 1 deletion airbyte-cdk/python/airbyte_cdk/sources/embedded/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
from typing import Generic, Iterable, Optional

from airbyte_cdk.connector import TConfig
from airbyte_cdk.models import AirbyteCatalog, AirbyteMessage, AirbyteStateMessage, ConfiguredAirbyteCatalog
from airbyte_cdk.models import AirbyteCatalog, AirbyteMessage, AirbyteStateMessage, ConfiguredAirbyteCatalog, ConnectorSpecification
from airbyte_cdk.sources.source import Source


class SourceRunner(ABC, Generic[TConfig]):
@abstractmethod
def spec(self) -> ConnectorSpecification:
pass

@abstractmethod
def discover(self, config: TConfig) -> AirbyteCatalog:
pass
Expand All @@ -27,6 +31,9 @@ def __init__(self, source: Source, name: str):
self._source = source
self._logger = logging.getLogger(name)

def spec(self) -> ConnectorSpecification:
return self._source.spec(self._logger)

def discover(self, config: TConfig) -> AirbyteCatalog:
return self._source.discover(self._logger, config)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest.mock import MagicMock

from airbyte_cdk.sources.embedded.base_integration import BaseEmbeddedIntegration
from airbyte_cdk.utils import AirbyteTracedException
from airbyte_protocol.models import (
AirbyteCatalog,
AirbyteLogMessage,
Expand All @@ -16,6 +17,7 @@
AirbyteStream,
ConfiguredAirbyteCatalog,
ConfiguredAirbyteStream,
ConnectorSpecification,
DestinationSyncMode,
Level,
SyncMode,
Expand All @@ -33,7 +35,14 @@ def setUp(self):
self.source_class = MagicMock()
self.source = MagicMock()
self.source_class.return_value = self.source
self.config = MagicMock()
self.source.spec.return_value = ConnectorSpecification(connectionSpecification={
"properties": {
"test": {
"type": "string",
}
}
})
self.config = {"test": "abc"}
self.integration = TestIntegration(self.source, self.config)
self.stream1 = AirbyteStream(
name="test",
Expand Down Expand Up @@ -76,6 +85,12 @@ def test_integration(self):
None,
)

def test_failed_check(self):
self.config = {"test": 123}
with self.assertRaises(AirbyteTracedException) as error:
TestIntegration(self.source, self.config)
assert str(error.exception) == "123 is not of type 'string'"

def test_state(self):
state = AirbyteStateMessage(data={})
self.source.read.return_value = [
Expand Down