Skip to content

Commit 2712942

Browse files
committed
chore: test refactor
1 parent 601eb28 commit 2712942

File tree

2 files changed

+67
-74
lines changed

2 files changed

+67
-74
lines changed

unit_tests/source_declarative_manifest/conftest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
# Copyright (c) 2024 Airbyte, Inc., all rights reserved.
33
#
44

5+
import sys
6+
import tempfile
57
from pathlib import Path
8+
from typing import Generator
69

710
import pytest
811
import yaml
@@ -52,3 +55,49 @@ def valid_local_config_file():
5255
@pytest.fixture
5356
def invalid_local_config_file():
5457
return get_resource_path("invalid_local_pokeapi_config.json")
58+
59+
60+
# Sample component code for testing
61+
SAMPLE_COMPONENTS_PY_TEXT = """
62+
def sample_function() -> str:
63+
return "Hello, World!"
64+
65+
class SimpleClass:
66+
def sample_method(self) -> str:
67+
return sample_function()
68+
"""
69+
70+
71+
def verify_components_loaded() -> None:
72+
"""Verify that components were properly loaded."""
73+
import components
74+
75+
assert hasattr(components, "sample_function")
76+
assert components.sample_function() == "Hello, World!"
77+
78+
# Verify the components module is registered in sys.modules
79+
assert "components" in sys.modules
80+
assert "source_declarative_manifest.components" in sys.modules
81+
82+
# Verify they are the same module
83+
assert sys.modules["components"] is sys.modules["source_declarative_manifest.components"]
84+
85+
86+
@pytest.fixture
87+
def components_file() -> Generator[str, None, None]:
88+
"""Create a temporary file with sample components code and clean up modules afterwards."""
89+
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as temp_file:
90+
temp_file.write(SAMPLE_COMPONENTS_PY_TEXT)
91+
temp_file.flush()
92+
file_path = temp_file.name
93+
94+
try:
95+
yield file_path
96+
finally:
97+
# Clean up the modules
98+
if "components" in sys.modules:
99+
del sys.modules["components"]
100+
if "source_declarative_manifest.components" in sys.modules:
101+
del sys.modules["source_declarative_manifest.components"]
102+
# Clean up the temporary file
103+
Path(file_path).unlink(missing_ok=True)

unit_tests/source_declarative_manifest/test_source_declarative_w_custom_components.py

Lines changed: 18 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections.abc import Callable, Mapping
1212
from pathlib import Path
1313
from tempfile import NamedTemporaryFile
14-
from typing import Any
14+
from typing import Any, Generator
1515

1616
import pytest
1717
import yaml
@@ -36,15 +36,10 @@
3636
register_components_module_from_string,
3737
)
3838
from airbyte_cdk.utils.connector_paths import MANIFEST_YAML
39-
40-
SAMPLE_COMPONENTS_PY_TEXT = """
41-
def sample_function() -> str:
42-
return "Hello, World!"
43-
44-
class SimpleClass:
45-
def sample_method(self) -> str:
46-
return sample_function()
47-
"""
39+
from unit_tests.source_declarative_manifest.conftest import (
40+
SAMPLE_COMPONENTS_PY_TEXT,
41+
verify_components_loaded,
42+
)
4843

4944

5045
def get_resource_path(file_name) -> str:
@@ -293,52 +288,21 @@ def _read_fn(*args, **kwargs):
293288
_read_fn()
294289

295290

296-
def test_register_components_from_file() -> None:
291+
def test_register_components_from_file(components_file: str) -> None:
297292
"""Test that components can be properly loaded from a file."""
298-
# Create a temporary file with the sample components
299-
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as temp_file:
300-
temp_file.write(SAMPLE_COMPONENTS_PY_TEXT)
301-
temp_file.flush()
302-
file_path = temp_file.name
303-
304-
try:
305-
# Register the components
306-
_register_components_from_file(file_path)
307-
308-
# Verify the components were loaded correctly
309-
import components
310-
311-
assert hasattr(components, "sample_function")
312-
assert components.sample_function() == "Hello, World!"
293+
# Register the components
294+
_register_components_from_file(components_file)
313295

314-
# Verify the components module is registered in sys.modules
315-
assert "components" in sys.modules
316-
assert "source_declarative_manifest.components" in sys.modules
296+
# Verify the components were loaded correctly
297+
verify_components_loaded()
317298

318-
# Verify they are the same module
319-
assert sys.modules["components"] is sys.modules["source_declarative_manifest.components"]
320299

321-
# Clean up the modules
322-
if "components" in sys.modules:
323-
del sys.modules["components"]
324-
if "source_declarative_manifest.components" in sys.modules:
325-
del sys.modules["source_declarative_manifest.components"]
326-
finally:
327-
# Clean up the temporary file
328-
Path(file_path).unlink(missing_ok=True)
329-
330-
331-
def test_parse_components_from_args(monkeypatch: pytest.MonkeyPatch) -> None:
300+
def test_parse_components_from_args(monkeypatch: pytest.MonkeyPatch, components_file: str) -> None:
332301
"""Test that components can be loaded from command line arguments."""
333-
# Create a temporary file with sample components
334-
with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False) as temp_file:
335-
temp_file.write(SAMPLE_COMPONENTS_PY_TEXT)
336-
temp_file.flush()
337-
file_path = temp_file.name
338302

339303
# Mock the arguments
340304
class MockArgs:
341-
components_path = file_path
305+
components_path = components_file
342306

343307
# Mock the parse_args function to return our mock args
344308
def mock_parse_args(*args: Any, **kwargs: Any) -> Any:
@@ -349,31 +313,11 @@ def mock_parse_args(*args: Any, **kwargs: Any) -> Any:
349313

350314
monkeypatch.setattr(AirbyteEntrypoint, "parse_args", mock_parse_args)
351315

352-
try:
353-
# Call the function with any args (they'll be ignored due to the mock)
354-
result = _parse_components_from_args(["some", "args"])
355-
356-
# Verify result
357-
assert result is True # Should return True when successful
358-
359-
# Verify the components were loaded
360-
import components
361-
362-
assert hasattr(components, "sample_function")
363-
assert components.sample_function() == "Hello, World!"
364-
365-
# Verify both module names are registered
366-
assert "components" in sys.modules
367-
assert "source_declarative_manifest.components" in sys.modules
316+
# Call the function with any args (they'll be ignored due to the mock)
317+
result = _parse_components_from_args(["some", "args"])
368318

369-
# Verify they are the same module
370-
assert sys.modules["components"] is sys.modules["source_declarative_manifest.components"]
319+
# Verify result
320+
assert result is True # Should return True when successful
371321

372-
# Clean up the modules
373-
if "components" in sys.modules:
374-
del sys.modules["components"]
375-
if "source_declarative_manifest.components" in sys.modules:
376-
del sys.modules["source_declarative_manifest.components"]
377-
finally:
378-
# Clean up the temporary file
379-
Path(file_path).unlink(missing_ok=True)
322+
# Verify the components were loaded
323+
verify_components_loaded()

0 commit comments

Comments
 (0)