-
Notifications
You must be signed in to change notification settings - Fork 30
feat: add unit test fixtures for manifest-only connectors to CDK #121
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
Merged
+60
−0
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d110dea
feat: add unit test fixtures for manifest-only connectors to CDK
ChristoGrab 45375ca
chore: lint/format/license
ChristoGrab 6dcdb8a
fix: use invocation_params to find correct project directory
ChristoGrab 583f5f0
chore: format
ChristoGrab 0ac8dd5
chore: add comments
ChristoGrab 1b92503
chore: formatter raining on my parade
ChristoGrab f0aef10
fix: handle connector_dir by environment
ChristoGrab dbbeeb2
Merge branch 'main' into christo/manifest-only-unit-test-fixturs
ChristoGrab 763d247
Update airbyte_cdk/test/utils/manifest_only_fixtures.py
ChristoGrab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright (c) 2024 Airbyte, Inc., all rights reserved. | ||
|
||
|
||
import importlib.util | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Optional | ||
|
||
import pytest | ||
|
||
# The following fixtures are used to load a manifest-only connector's components module and manifest file. | ||
# They can be accessed from any test file in the connector's unit_tests directory by importing them as follows: | ||
|
||
# from airbyte_cdk.test.utils.manifest_only_fixtures import components_module, connector_dir, manifest_path | ||
|
||
# individual components can then be referenced as: components_module.<CustomComponentClass> | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def connector_dir(request: pytest.FixtureRequest) -> Path: | ||
"""Return the connector's root directory.""" | ||
|
||
current_dir = Path(request.config.invocation_params.dir) | ||
|
||
# If the tests are run locally from the connector's unit_tests directory, return the parent (connector) directory | ||
if current_dir.name == "unit_tests": | ||
return current_dir.parent | ||
# In CI, the tests are run from the connector directory itself | ||
return current_dir | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def components_module(connector_dir: Path) -> Optional[ModuleType]: | ||
"""Load and return the components module from the connector directory. | ||
|
||
This assumes the components module is located at <connector_dir>/components.py. | ||
""" | ||
components_path = connector_dir / "components.py" | ||
if not components_path.exists(): | ||
return None | ||
|
||
components_spec = importlib.util.spec_from_file_location("components", components_path) | ||
if components_spec is None: | ||
return None | ||
|
||
components_module = importlib.util.module_from_spec(components_spec) | ||
if components_spec.loader is None: | ||
return None | ||
|
||
components_spec.loader.exec_module(components_module) | ||
return components_module | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def manifest_path(connector_dir: Path) -> Path: | ||
"""Return the path to the connector's manifest file.""" | ||
path = connector_dir / "manifest.yaml" | ||
if not path.exists(): | ||
raise FileNotFoundError(f"Manifest file not found at {path}") | ||
return path |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.