Skip to content

Commit 9389cc5

Browse files
committed
reduce code needed for inheritance
1 parent e24594b commit 9389cc5

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

airbyte_cdk/test/declarative/test_suites/connector_base.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import abc
77
import inspect
88
import sys
9+
from collections.abc import Callable
910
from pathlib import Path
10-
from typing import Any, Literal
11+
from typing import Any, Literal, cast
1112

1213
import pytest
1314
import yaml
@@ -90,23 +91,34 @@ class TestMyConnector(ConnectorTestSuiteBase):
9091
class ConnectorTestSuiteBase(abc.ABC):
9192
"""Base class for connector test suites."""
9293

94+
connector: type[IConnector] | Callable[[], IConnector] | None = None
95+
"""The connector class or a factory function that returns an instance of IConnector."""
96+
9397
@classmethod
9498
def get_test_class_dir(cls) -> Path:
9599
"""Get the file path that contains the class."""
96100
module = sys.modules[cls.__module__]
97101
# Get the directory containing the test file
98102
return Path(inspect.getfile(module)).parent
99103

100-
connector: type[Connector] | Path | JavaClass | DockerImage | None = None
101-
"""The connector class or path to the connector to test."""
102-
103104
@classmethod
104105
def create_connector(
105106
cls,
106107
scenario: ConnectorTestScenario,
107108
) -> IConnector:
108109
"""Instantiate the connector class."""
109-
raise NotImplementedError("Subclasses must implement this method.")
110+
connector = cls.connector # type: ignore
111+
if connector:
112+
if callable(connector) or isinstance(connector, type):
113+
# If the connector is a class or factory function, instantiate it:
114+
return cast(IConnector, connector())
115+
116+
# Otherwise, we can't instantiate the connector. Fail with a clear error message.
117+
raise NotImplementedError(
118+
"No connector class or connector factory function provided. "
119+
"Please provide a class or factory function in `cls.connector`, or "
120+
"override `cls.create_connector()` to define a custom initialization process."
121+
)
110122

111123
def run_test_scenario(
112124
self,

unit_tests/resources/source_pokeapi_w_components_py/integration_tests/test_mini_cat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)
88

99

10-
def pytest_generate_tests(metafunc):
10+
def pytest_generate_tests(metafunc) -> None:
1111
generate_tests(metafunc)
1212

1313

0 commit comments

Comments
 (0)