1111from collections .abc import Callable , Mapping
1212from pathlib import Path
1313from tempfile import NamedTemporaryFile
14- from typing import Any
14+ from typing import Any , Generator
1515
1616import pytest
1717import yaml
3636 register_components_module_from_string ,
3737)
3838from 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
5045def 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