Skip to content

Commit 0895115

Browse files
maxi297octavia-squidington-iii
andauthored
feat(test): Allow mock server tests for manifest only sources (#371)
Co-authored-by: octavia-squidington-iii <[email protected]>
1 parent edafc40 commit 0895115

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

airbyte_cdk/sources/declarative/parsers/model_to_component_factory.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,19 @@ def _get_class_from_fully_qualified_class_name(
14761476
try:
14771477
module_ref = importlib.import_module(module_name_full)
14781478
except ModuleNotFoundError as e:
1479-
raise ValueError(f"Could not load module `{module_name_full}`.") from e
1479+
if split[0] == "source_declarative_manifest":
1480+
# 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
1481+
try:
1482+
import os
1483+
1484+
module_name_with_source_declarative_manifest = ".".join(split[1:-1])
1485+
module_ref = importlib.import_module(
1486+
module_name_with_source_declarative_manifest
1487+
)
1488+
except ModuleNotFoundError:
1489+
raise ValueError(f"Could not load module `{module_name_full}`.") from e
1490+
else:
1491+
raise ValueError(f"Could not load module `{module_name_full}`.") from e
14801492

14811493
try:
14821494
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
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)