|
1 | 1 | """Tests for import utilities.""" |
2 | 2 |
|
| 3 | +import sys |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
3 | 6 | import pytest |
4 | | -from unittest.mock import patch |
5 | 7 |
|
6 | | -from crewai.utilities.import_utils import require, OptionalDependencyError |
| 8 | +from crewai.utilities.import_utils import ( |
| 9 | + OptionalDependencyError, |
| 10 | + import_and_validate_definition, |
| 11 | + require, |
| 12 | + validate_import_path, |
| 13 | +) |
7 | 14 |
|
8 | 15 |
|
9 | 16 | class TestRequire: |
@@ -40,3 +47,143 @@ def test_require_with_import_error(self): |
40 | 47 | def test_optional_dependency_error_is_import_error(self): |
41 | 48 | """Test that OptionalDependencyError is a subclass of ImportError.""" |
42 | 49 | assert issubclass(OptionalDependencyError, ImportError) |
| 50 | + |
| 51 | + def test_require_with_attr(self): |
| 52 | + """Test requiring a specific attribute from a module.""" |
| 53 | + loads = require("json", purpose="testing", attr="loads") |
| 54 | + import json |
| 55 | + |
| 56 | + assert loads == json.loads |
| 57 | + |
| 58 | + def test_require_with_nonexistent_attr(self): |
| 59 | + """Test requiring a nonexistent attribute raises AttributeError.""" |
| 60 | + with pytest.raises(AttributeError) as exc_info: |
| 61 | + require("json", purpose="testing", attr="nonexistent_attr") |
| 62 | + |
| 63 | + assert "Module 'json' has no attribute 'nonexistent_attr'" in str( |
| 64 | + exc_info.value |
| 65 | + ) |
| 66 | + |
| 67 | + def test_require_extracts_package_name(self): |
| 68 | + """Test that require correctly extracts package name from module path.""" |
| 69 | + with pytest.raises(OptionalDependencyError) as exc_info: |
| 70 | + require("some.nested.module.path", purpose="testing") |
| 71 | + |
| 72 | + error_msg = str(exc_info.value) |
| 73 | + assert "uv add some" in error_msg |
| 74 | + |
| 75 | + |
| 76 | +class TestValidateImportPath: |
| 77 | + """Test the validate_import_path function.""" |
| 78 | + |
| 79 | + def test_validate_import_path_success(self): |
| 80 | + """Test successful import of a class.""" |
| 81 | + result = validate_import_path("json.JSONDecoder") |
| 82 | + import json |
| 83 | + |
| 84 | + assert result == json.JSONDecoder |
| 85 | + |
| 86 | + def test_validate_import_path_malformed_no_module(self): |
| 87 | + """Test validation with no module path.""" |
| 88 | + with pytest.raises(ValueError) as exc_info: |
| 89 | + validate_import_path("ClassName") |
| 90 | + |
| 91 | + assert "import_path 'ClassName' must be of the form 'module.ClassName'" in str( |
| 92 | + exc_info.value |
| 93 | + ) |
| 94 | + |
| 95 | + def test_validate_import_path_empty_string(self): |
| 96 | + """Test validation with empty string.""" |
| 97 | + with pytest.raises(ValueError) as exc_info: |
| 98 | + validate_import_path("") |
| 99 | + |
| 100 | + assert "import_path '' must be of the form 'module.ClassName'" in str( |
| 101 | + exc_info.value |
| 102 | + ) |
| 103 | + |
| 104 | + def test_validate_import_path_module_not_found(self): |
| 105 | + """Test validation with non-existent module.""" |
| 106 | + with pytest.raises(ValueError) as exc_info: |
| 107 | + validate_import_path("nonexistent_module.ClassName") |
| 108 | + |
| 109 | + error_msg = str(exc_info.value) |
| 110 | + assert "Package 'nonexistent_module' could not be imported" in error_msg |
| 111 | + assert "uv add nonexistent_module" in error_msg |
| 112 | + |
| 113 | + def test_validate_import_path_attribute_not_found(self): |
| 114 | + """Test validation when attribute doesn't exist in module.""" |
| 115 | + with pytest.raises(ValueError) as exc_info: |
| 116 | + validate_import_path("json.NonExistentClass") |
| 117 | + |
| 118 | + assert "Attribute 'NonExistentClass' not found in module 'json'" in str( |
| 119 | + exc_info.value |
| 120 | + ) |
| 121 | + |
| 122 | + def test_validate_import_path_nested_module(self): |
| 123 | + """Test validation with nested module path.""" |
| 124 | + result = validate_import_path("unittest.mock.MagicMock") |
| 125 | + from unittest.mock import MagicMock |
| 126 | + |
| 127 | + assert result == MagicMock |
| 128 | + |
| 129 | + def test_validate_import_path_extracts_package_name(self): |
| 130 | + """Test that package name is correctly extracted for error message.""" |
| 131 | + with pytest.raises(ValueError) as exc_info: |
| 132 | + validate_import_path("some.nested.module.path.ClassName") |
| 133 | + |
| 134 | + error_msg = str(exc_info.value) |
| 135 | + assert "Package 'some' could not be imported" in error_msg |
| 136 | + assert "uv add some" in error_msg |
| 137 | + |
| 138 | + |
| 139 | +class TestImportAndValidateDefinition: |
| 140 | + """Test the import_and_validate_definition function.""" |
| 141 | + |
| 142 | + def test_import_and_validate_definition_success(self): |
| 143 | + """Test successful import through Pydantic adapter.""" |
| 144 | + result = import_and_validate_definition("json.JSONEncoder") |
| 145 | + import json |
| 146 | + |
| 147 | + assert result == json.JSONEncoder |
| 148 | + |
| 149 | + def test_import_and_validate_definition_with_function(self): |
| 150 | + """Test importing a function instead of a class.""" |
| 151 | + result = import_and_validate_definition("json.loads") |
| 152 | + import json |
| 153 | + |
| 154 | + assert result == json.loads |
| 155 | + |
| 156 | + def test_import_and_validate_definition_invalid(self): |
| 157 | + """Test that invalid paths raise ValueError.""" |
| 158 | + with pytest.raises(ValueError) as exc_info: |
| 159 | + import_and_validate_definition("InvalidPath") |
| 160 | + |
| 161 | + assert "must be of the form 'module.ClassName'" in str(exc_info.value) |
| 162 | + |
| 163 | + def test_import_and_validate_definition_module_error(self): |
| 164 | + """Test error handling for missing modules.""" |
| 165 | + with pytest.raises(ValueError) as exc_info: |
| 166 | + import_and_validate_definition("missing_package.SomeClass") |
| 167 | + |
| 168 | + error_msg = str(exc_info.value) |
| 169 | + assert "Package 'missing_package' could not be imported" in error_msg |
| 170 | + assert "uv add missing_package" in error_msg |
| 171 | + |
| 172 | + def test_import_and_validate_definition_attribute_error(self): |
| 173 | + """Test error handling for missing attributes.""" |
| 174 | + with pytest.raises(ValueError) as exc_info: |
| 175 | + import_and_validate_definition("json.MissingClass") |
| 176 | + |
| 177 | + assert "Attribute 'MissingClass' not found in module 'json'" in str( |
| 178 | + exc_info.value |
| 179 | + ) |
| 180 | + |
| 181 | + def test_import_and_validate_definition_with_mock(self): |
| 182 | + """Test that mocked modules work correctly.""" |
| 183 | + mock_module = MagicMock() |
| 184 | + mock_class = MagicMock() |
| 185 | + mock_module.MockClass = mock_class |
| 186 | + |
| 187 | + with patch.dict(sys.modules, {"mocked_module": mock_module}): |
| 188 | + result = import_and_validate_definition("mocked_module.MockClass") |
| 189 | + assert result == mock_class |
0 commit comments