Skip to content

Commit c8f9a7a

Browse files
author
maxi297
committed
Allow mock server tests for manifest only sources
1 parent 406542d commit c8f9a7a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,16 @@ def _get_class_from_fully_qualified_class_name(
14051405
try:
14061406
module_ref = importlib.import_module(module_name_full)
14071407
except ModuleNotFoundError as e:
1408-
raise ValueError(f"Could not load module `{module_name_full}`.") from e
1408+
if split[0] == "source_declarative_manifest":
1409+
# During testing, the modules containing the custom components are not moved to source_declarative_manifest. In order to run the test, add the source folder to your PYTHONPATH or add it runtime using sys.path.append
1410+
try:
1411+
import os
1412+
module_name_with_source_declarative_manifest = ".".join(split[1:-1])
1413+
module_ref = importlib.import_module(module_name_with_source_declarative_manifest)
1414+
except ModuleNotFoundError:
1415+
raise ValueError(f"Could not load module `{module_name_full}`.") from e
1416+
else:
1417+
raise ValueError(f"Could not load module `{module_name_full}`.") from e
14091418

14101419
try:
14111420
return getattr(module_ref, class_name)

airbyte_cdk/sources/declarative/yaml_declarative_source.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ def __init__(
3939
)
4040

4141
def _read_and_parse_yaml_file(self, path_to_yaml_file: str) -> ConnectionDefinition:
42-
package = self.__class__.__module__.split(".")[0]
42+
try:
43+
# For testing purposes, we want to allow to just pass a file. However, this
44+
with open(path_to_yaml_file, "r") as f:
45+
return yaml.safe_load(f) # type: ignore # we assume the yaml represents a ConnectionDefinition
46+
except FileNotFoundError:
47+
# Running inside the container, the working directory during an operation is not structured the same as the static files
48+
package = self.__class__.__module__.split(".")[0]
4349

44-
yaml_config = pkgutil.get_data(package, path_to_yaml_file)
45-
if yaml_config:
46-
decoded_yaml = yaml_config.decode()
47-
return self._parse(decoded_yaml)
48-
else:
50+
yaml_config = pkgutil.get_data(package, path_to_yaml_file)
51+
if yaml_config:
52+
decoded_yaml = yaml_config.decode()
53+
return self._parse(decoded_yaml)
4954
return {}
5055

5156
def _emit_manifest_debug_message(self, extra_args: dict[str, Any]) -> None:

0 commit comments

Comments
 (0)