1
+ import builtins
2
+ import importlib
1
3
import sys
4
+ from typing import Any
2
5
from unittest .mock import MagicMock , patch
3
6
4
7
import pytest
5
8
from langchain_core .tools .structured import StructuredTool
6
9
from llama_index .core .tools import FunctionTool
7
10
8
11
from cleanlab_codex .codex_tool import CodexTool
12
+ from cleanlab_codex .utils .errors import MissingDependencyError
13
+
14
+
15
+ def patch_import_with_import_error (missing_module : str ) -> None :
16
+ def custom_import (name : str , * args : Any , ** kwargs : Any ) -> Any :
17
+ if name .startswith (missing_module ):
18
+ raise ImportError ("test" , name = missing_module )
19
+ return importlib .__import__ (name , * args , ** kwargs )
20
+
21
+ builtins .__import__ = custom_import
9
22
10
23
11
24
def test_to_openai_tool (mock_client_from_access_key : MagicMock ) -> None :
@@ -32,6 +45,20 @@ def test_to_llamaindex_tool(mock_client_from_access_key: MagicMock) -> None:
32
45
assert llama_index_tool .fn == tool .query
33
46
34
47
48
+ def test_to_llamaindex_tool_import_error (
49
+ mock_client_from_access_key : MagicMock ,
50
+ ) -> None :
51
+ with patch ("cleanlab_codex.codex_tool.Project" ) as mock_project :
52
+ mock_project .from_access_key .return_value = MagicMock (client = mock_client_from_access_key , id = "test_project_id" )
53
+
54
+ tool = CodexTool .from_access_key ("sk-test-123" )
55
+ patch_import_with_import_error ("llama_index" )
56
+ with pytest .raises (MissingDependencyError ) as exc_info :
57
+ tool .to_llamaindex_tool ()
58
+
59
+ assert exc_info .value .import_name == "llama_index"
60
+
61
+
35
62
def test_to_langchain_tool (mock_client_from_access_key : MagicMock ) -> None :
36
63
with patch ("cleanlab_codex.codex_tool.Project" ) as mock_project :
37
64
mock_project .from_access_key .return_value = MagicMock (client = mock_client_from_access_key , id = "test_project_id" )
@@ -50,6 +77,18 @@ def test_to_langchain_tool(mock_client_from_access_key: MagicMock) -> None:
50
77
), f"Expected description '{ tool .tool_description } ', got '{ langchain_tool .description } '."
51
78
52
79
80
+ def test_to_langchain_tool_import_error (mock_client_from_access_key : MagicMock ) -> None :
81
+ with patch ("cleanlab_codex.codex_tool.Project" ) as mock_project :
82
+ mock_project .from_access_key .return_value = MagicMock (client = mock_client_from_access_key , id = "test_project_id" )
83
+
84
+ tool = CodexTool .from_access_key ("sk-test-123" )
85
+ patch_import_with_import_error ("langchain" )
86
+ with pytest .raises (MissingDependencyError ) as exc_info :
87
+ tool .to_langchain_tool ()
88
+
89
+ assert exc_info .value .import_name == "langchain"
90
+
91
+
53
92
def test_to_aws_converse_tool (mock_client_from_access_key : MagicMock ) -> None :
54
93
with patch ("cleanlab_codex.codex_tool.Project" ) as mock_project :
55
94
mock_project .from_access_key .return_value = MagicMock (client = mock_client_from_access_key , id = "test_project_id" )
@@ -95,3 +134,21 @@ def test_to_smolagents_tool(mock_client_from_access_key: MagicMock) -> None:
95
134
assert isinstance (smolagents_tool , Tool )
96
135
assert smolagents_tool .name == tool .tool_name
97
136
assert smolagents_tool .description == tool .tool_description
137
+
138
+
139
+ def test_to_smolagents_tool_import_error (
140
+ mock_client_from_access_key : MagicMock ,
141
+ ) -> None :
142
+ with patch ("cleanlab_codex.codex_tool.Project" ) as mock_project :
143
+ mock_project .from_access_key .return_value = MagicMock (client = mock_client_from_access_key , id = "test_project_id" )
144
+
145
+ tool = CodexTool .from_access_key ("sk-test-123" )
146
+ import_module_name = "smolagents"
147
+ if sys .version_info >= (3 , 10 ):
148
+ import_module_name = "cleanlab_codex.utils.smolagents"
149
+ patch_import_with_import_error (import_module_name )
150
+
151
+ with pytest .raises (MissingDependencyError ) as exc_info :
152
+ tool .to_smolagents_tool ()
153
+
154
+ assert exc_info .value .import_name == import_module_name
0 commit comments