Skip to content

Commit ed9a5e7

Browse files
feat: add unit test fixtures for manifest-only connectors to CDK (#121)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 42ee3b4 commit ed9a5e7

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
2+
3+
4+
import importlib.util
5+
from pathlib import Path
6+
from types import ModuleType
7+
from typing import Optional
8+
9+
import pytest
10+
11+
# The following fixtures are used to load a manifest-only connector's components module and manifest file.
12+
# They can be accessed from any test file in the connector's unit_tests directory by importing them as follows:
13+
14+
# from airbyte_cdk.test.utils.manifest_only_fixtures import components_module, connector_dir, manifest_path
15+
16+
# individual components can then be referenced as: components_module.<CustomComponentClass>
17+
18+
19+
@pytest.fixture(scope="session")
20+
def connector_dir(request: pytest.FixtureRequest) -> Path:
21+
"""Return the connector's root directory."""
22+
23+
current_dir = Path(request.config.invocation_params.dir)
24+
25+
# If the tests are run locally from the connector's unit_tests directory, return the parent (connector) directory
26+
if current_dir.name == "unit_tests":
27+
return current_dir.parent
28+
# In CI, the tests are run from the connector directory itself
29+
return current_dir
30+
31+
32+
@pytest.fixture(scope="session")
33+
def components_module(connector_dir: Path) -> Optional[ModuleType]:
34+
"""Load and return the components module from the connector directory.
35+
36+
This assumes the components module is located at <connector_dir>/components.py.
37+
"""
38+
components_path = connector_dir / "components.py"
39+
if not components_path.exists():
40+
return None
41+
42+
components_spec = importlib.util.spec_from_file_location("components", components_path)
43+
if components_spec is None:
44+
return None
45+
46+
components_module = importlib.util.module_from_spec(components_spec)
47+
if components_spec.loader is None:
48+
return None
49+
50+
components_spec.loader.exec_module(components_module)
51+
return components_module
52+
53+
54+
@pytest.fixture(scope="session")
55+
def manifest_path(connector_dir: Path) -> Path:
56+
"""Return the path to the connector's manifest file."""
57+
path = connector_dir / "manifest.yaml"
58+
if not path.exists():
59+
raise FileNotFoundError(f"Manifest file not found at {path}")
60+
return path

0 commit comments

Comments
 (0)