11import pytest
2- from unittest .mock import Mock
2+ from unittest .mock import Mock , patch , MagicMock
33from libs .pipeline .entities .pipeline_step_result import StepResult
44from libs .pipeline .entities .pipeline_status import PipelineStatus
5+ from libs .pipeline .entities .pipeline_data import DataPipeline
6+ from libs .pipeline .entities .pipeline_file import ArtifactType
57
68
79def test_update_step ():
@@ -47,57 +49,6 @@ def test_get_previous_step_result():
4749 assert result is None
4850
4951
50- # def test_save_to_persistent_storage(mocker):
51- # # Mock the StorageBlobHelper.upload_text method
52- # mock_upload_text = mocker.patch(
53- # "libs.azure_helper.storage_blob.StorageBlobHelper.upload_text"
54- # )
55-
56- # # Mock the StorageBlobHelper constructor to return a mock instance
57- # mock_storage_blob_helper = mocker.patch(
58- # "libs.azure_helper.storage_blob.StorageBlobHelper", autospec=True
59- # )
60- # mock_storage_blob_helper_instance = mock_storage_blob_helper.return_value
61-
62- # # Mock the create_container method on the container_client
63- # mock_container_client = Mock()
64- # mock_container_client.create_container = Mock()
65- # mock_storage_blob_helper_instance._invalidate_container = Mock()
66- # mock_storage_blob_helper_instance._invalidate_container.return_value = (
67- # mock_container_client
68- # )
69-
70- # # Create a PipelineStatus object with a process_id
71- # pipeline_status = PipelineStatus(process_id="123")
72-
73- # # Mock the update_step method using pytest-mock
74- # mock_update_step = mocker.patch.object(
75- # PipelineStatus, "update_step", return_value=None
76- # )
77-
78- # # Mock the model_dump_json method using pytest-mock
79- # mock_model_dump_json = mocker.patch.object(
80- # PipelineStatus, "model_dump_json", return_value='{"key": "value"}'
81- # )
82-
83- # account_url = "https://example.com"
84- # container_name = "container"
85-
86- # # Call the save_to_persistent_storage method
87- # pipeline_status.save_to_persistent_storage(account_url, container_name)
88-
89- # # Assert that update_step was called once
90- # mock_update_step.assert_called_once()
91-
92- # # Assert that model_dump_json was called once
93- # mock_model_dump_json.assert_called_once()
94-
95- # # Assert that upload_text was called with the correct arguments
96- # mock_upload_text.assert_called_once_with(
97- # container_name="123", blob_name="process-status.json", text='{"key": "value"}'
98- # )
99-
100-
10152def test_save_to_persistent_storage_no_process_id ():
10253 pipeline_status = PipelineStatus ()
10354 with pytest .raises (ValueError , match = "Process ID is required to save the result." ):
@@ -115,3 +66,91 @@ def test_move_to_next_step():
11566 assert pipeline_status .completed_steps == ["step1" , "step2" ]
11667 assert pipeline_status .remaining_steps == []
11768 assert pipeline_status .completed is True
69+
70+
71+ # DataPipeline Tests
72+ class TestDataPipeline :
73+ """Tests for DataPipeline class."""
74+
75+ def test_get_object_valid_json (self ):
76+ """Test parsing valid JSON string to DataPipeline."""
77+ json_str = '{"process_id": "test-123", "PipelineStatus": {"Completed": false}, "Files": []}'
78+ result = DataPipeline .get_object (json_str )
79+ assert result .process_id == "test-123"
80+ assert result .pipeline_status is not None
81+
82+ def test_get_object_invalid_json (self ):
83+ """Test that invalid JSON raises ValueError."""
84+ with pytest .raises (ValueError , match = "Failed to parse" ):
85+ DataPipeline .get_object ("invalid json {" )
86+
87+ def test_add_file (self ):
88+ """Test adding a file to the pipeline."""
89+ pipeline_status = PipelineStatus (process_id = "test-123" , active_step = "step1" )
90+ data_pipeline = DataPipeline (process_id = "test-123" , pipeline_status = pipeline_status )
91+
92+ file = data_pipeline .add_file ("document.pdf" , ArtifactType .SourceContent )
93+
94+ assert len (data_pipeline .files ) == 1
95+ assert file .name == "document.pdf"
96+ assert file .artifact_type == ArtifactType .SourceContent
97+ assert file .processed_by == "step1"
98+
99+ def test_get_step_result (self ):
100+ """Test getting step result from DataPipeline."""
101+ pipeline_status = PipelineStatus (process_id = "test-123" )
102+ step_result = StepResult (step_name = "extract" , result = {"data" : "value" })
103+ pipeline_status .process_results .append (step_result )
104+
105+ data_pipeline = DataPipeline (process_id = "test-123" , pipeline_status = pipeline_status )
106+
107+ result = data_pipeline .get_step_result ("extract" )
108+ assert result == step_result
109+
110+ def test_get_previous_step_result (self ):
111+ """Test getting previous step result from DataPipeline."""
112+ pipeline_status = PipelineStatus (process_id = "test-123" , completed_steps = ["step1" ])
113+ step_result = StepResult (step_name = "step1" , result = {"data" : "value" })
114+ pipeline_status .process_results .append (step_result )
115+
116+ data_pipeline = DataPipeline (process_id = "test-123" , pipeline_status = pipeline_status )
117+
118+ result = data_pipeline .get_previous_step_result ("step2" )
119+ assert result == step_result
120+
121+ def test_get_source_files (self ):
122+ """Test getting source files from pipeline."""
123+ pipeline_status = PipelineStatus (process_id = "test-123" , active_step = "step1" )
124+ data_pipeline = DataPipeline (process_id = "test-123" , pipeline_status = pipeline_status )
125+
126+ # Add source file
127+ data_pipeline .add_file ("source.pdf" , ArtifactType .SourceContent )
128+ # Add extracted file
129+ data_pipeline .add_file ("output.json" , ArtifactType .ExtractedContent )
130+
131+ source_files = data_pipeline .get_source_files ()
132+
133+ assert len (source_files ) == 1
134+ assert source_files [0 ].name == "source.pdf"
135+
136+ def test_save_to_database_not_implemented (self ):
137+ """Test that save_to_database raises NotImplementedError."""
138+ pipeline_status = PipelineStatus (process_id = "test-123" )
139+ data_pipeline = DataPipeline (process_id = "test-123" , pipeline_status = pipeline_status )
140+
141+ with pytest .raises (NotImplementedError ):
142+ data_pipeline .save_to_database ()
143+
144+ @patch ("libs.pipeline.entities.pipeline_data.StorageBlobHelper" )
145+ def test_save_to_persistent_storage (self , mock_storage_helper ):
146+ """Test saving pipeline to persistent storage."""
147+ mock_instance = MagicMock ()
148+ mock_storage_helper .return_value = mock_instance
149+
150+ pipeline_status = PipelineStatus (process_id = "test-123" )
151+ data_pipeline = DataPipeline (process_id = "test-123" , pipeline_status = pipeline_status )
152+
153+ data_pipeline .save_to_persistent_storage ("https://storage.blob.core.windows.net" , "container" )
154+
155+ mock_storage_helper .assert_called_once ()
156+ mock_instance .upload_text .assert_called_once ()
0 commit comments