|
| 1 | +from io import StringIO |
| 2 | +from unittest.mock import Mock, patch |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | + |
| 7 | +from tasks.file_processing import process_csv_file |
| 8 | + |
| 9 | + |
| 10 | +class TestFileProcessing: |
| 11 | + """Test Celery file processing tasks""" |
| 12 | + |
| 13 | + @patch("tasks.file_processing.storage_service") |
| 14 | + @patch("tasks.file_processing.get_project_service") |
| 15 | + @patch.object(process_csv_file, "update_state", autospec=True) |
| 16 | + def test_process_csv_file_success( |
| 17 | + self, mock_update_state, mock_project_service, mock_storage_service |
| 18 | + ): |
| 19 | + """Test successful CSV file processing""" |
| 20 | + # Mock project service |
| 21 | + mock_service = Mock() |
| 22 | + mock_project_service.return_value = mock_service |
| 23 | + |
| 24 | + # Mock storage service |
| 25 | + csv_content = """id,name,email,age |
| 26 | +1,John Doe,john@example.com,30 |
| 27 | +2,Jane Smith,jane@example.com,25 |
| 28 | +3,Bob Johnson,bob@example.com,35""" |
| 29 | + |
| 30 | + mock_storage_service.download_file.return_value = csv_content.encode("utf-8") |
| 31 | + |
| 32 | + # Call the task function directly (not through Celery wrapper) |
| 33 | + result = process_csv_file.run( |
| 34 | + "00000000-0000-0000-0000-000000000001", |
| 35 | + "00000000-0000-0000-0000-000000000002", |
| 36 | + ) |
| 37 | + |
| 38 | + # Verify storage service was called |
| 39 | + mock_storage_service.download_file.assert_called_once_with( |
| 40 | + "00000000-0000-0000-0000-000000000002/00000000-0000-0000-0000-000000000001/data.csv" |
| 41 | + ) |
| 42 | + |
| 43 | + # Verify project service was called |
| 44 | + mock_service.update_project_status.assert_called() |
| 45 | + mock_service.update_project_metadata.assert_called() |
| 46 | + |
| 47 | + # Verify result |
| 48 | + assert result["project_id"] == "00000000-0000-0000-0000-000000000001" |
| 49 | + assert result["status"] == "completed" |
| 50 | + assert result["row_count"] == 3 |
| 51 | + assert result["column_count"] == 4 |
| 52 | + |
| 53 | + # Verify column metadata |
| 54 | + columns_metadata = result["columns_metadata"] |
| 55 | + assert len(columns_metadata) == 4 |
| 56 | + |
| 57 | + # Check that columns are detected |
| 58 | + column_names = [col["name"] for col in columns_metadata] |
| 59 | + assert "id" in column_names |
| 60 | + assert "name" in column_names |
| 61 | + assert "email" in column_names |
| 62 | + assert "age" in column_names |
| 63 | + |
| 64 | + @patch("tasks.file_processing.storage_service") |
| 65 | + @patch("tasks.file_processing.get_project_service") |
| 66 | + @patch.object(process_csv_file, "update_state", autospec=True) |
| 67 | + def test_process_csv_file_download_failure( |
| 68 | + self, mock_update_state, mock_project_service, mock_storage_service |
| 69 | + ): |
| 70 | + """Test CSV processing when file download fails""" |
| 71 | + # Mock project service |
| 72 | + mock_service = Mock() |
| 73 | + mock_project_service.return_value = mock_service |
| 74 | + |
| 75 | + # Mock storage service to return None (download failure) |
| 76 | + mock_storage_service.download_file.return_value = None |
| 77 | + |
| 78 | + # Call the task and expect exception |
| 79 | + with pytest.raises(Exception, match="Failed to download file from storage"): |
| 80 | + process_csv_file.run( |
| 81 | + "00000000-0000-0000-0000-000000000001", |
| 82 | + "00000000-0000-0000-0000-000000000002", |
| 83 | + ) |
| 84 | + |
| 85 | + @patch("tasks.file_processing.storage_service") |
| 86 | + @patch("tasks.file_processing.get_project_service") |
| 87 | + @patch.object(process_csv_file, "update_state", autospec=True) |
| 88 | + @patch("tasks.file_processing.pd.read_csv") |
| 89 | + def test_process_csv_file_parse_failure( |
| 90 | + self, |
| 91 | + mock_pandas_read_csv, |
| 92 | + mock_update_state, |
| 93 | + mock_project_service, |
| 94 | + mock_storage_service, |
| 95 | + ): |
| 96 | + """Test CSV processing when file parsing fails""" |
| 97 | + # Mock project service |
| 98 | + mock_service = Mock() |
| 99 | + mock_project_service.return_value = mock_service |
| 100 | + |
| 101 | + # Mock storage service to return valid content |
| 102 | + mock_storage_service.download_file.return_value = b"valid,csv,content" |
| 103 | + |
| 104 | + # Mock pandas to raise an exception |
| 105 | + mock_pandas_read_csv.side_effect = Exception("CSV parsing failed") |
| 106 | + |
| 107 | + # Call the task and expect exception |
| 108 | + with pytest.raises(Exception, match="Failed to parse CSV"): |
| 109 | + process_csv_file.run( |
| 110 | + "00000000-0000-0000-0000-000000000001", |
| 111 | + "00000000-0000-0000-0000-000000000002", |
| 112 | + ) |
| 113 | + |
| 114 | + @patch("tasks.file_processing.storage_service") |
| 115 | + @patch("tasks.file_processing.get_project_service") |
| 116 | + @patch.object(process_csv_file, "update_state", autospec=True) |
| 117 | + def test_process_csv_file_error_handling( |
| 118 | + self, mock_update_state, mock_project_service, mock_storage_service |
| 119 | + ): |
| 120 | + """Test error handling in CSV processing""" |
| 121 | + # Mock project service to raise exception |
| 122 | + mock_service = Mock() |
| 123 | + mock_service.update_project_status.side_effect = Exception("Database error") |
| 124 | + mock_project_service.return_value = mock_service |
| 125 | + |
| 126 | + # Mock storage service |
| 127 | + csv_content = """id,name |
| 128 | +1,John Doe""" |
| 129 | + mock_storage_service.download_file.return_value = csv_content.encode("utf-8") |
| 130 | + |
| 131 | + # Call the task and expect exception |
| 132 | + with pytest.raises(Exception, match="Database error"): |
| 133 | + process_csv_file.run( |
| 134 | + "00000000-0000-0000-0000-000000000001", |
| 135 | + "00000000-0000-0000-0000-000000000002", |
| 136 | + ) |
0 commit comments