|
| 1 | +"""Test utilities module.""" |
| 2 | + |
| 3 | +from unittest.mock import Mock, patch |
| 4 | + |
| 5 | +from databricks_langchain.utils import get_openai_client |
| 6 | + |
| 7 | + |
| 8 | +def test_get_openai_client_with_timeout_and_max_retries() -> None: |
| 9 | + """Test that get_openai_client properly passes timeout and max_retries as kwargs to the SDK.""" |
| 10 | + |
| 11 | + mock_openai_client = Mock() |
| 12 | + |
| 13 | + mock_workspace_client = Mock() |
| 14 | + mock_workspace_client.serving_endpoints.get_open_ai_client.return_value = mock_openai_client |
| 15 | + |
| 16 | + # Test with workspace_client, timeout, and max_retries |
| 17 | + client = get_openai_client(workspace_client=mock_workspace_client, timeout=45.0, max_retries=3) |
| 18 | + |
| 19 | + # Verify the OpenAI client was obtained with the correct kwargs |
| 20 | + mock_workspace_client.serving_endpoints.get_open_ai_client.assert_called_once_with( |
| 21 | + timeout=45.0, max_retries=3 |
| 22 | + ) |
| 23 | + |
| 24 | + # Verify the client is returned |
| 25 | + assert client == mock_openai_client |
| 26 | + |
| 27 | + |
| 28 | +def test_get_openai_client_with_default_workspace_client() -> None: |
| 29 | + """Test get_openai_client creates default WorkspaceClient when none provided.""" |
| 30 | + |
| 31 | + mock_openai_client = Mock() |
| 32 | + |
| 33 | + mock_workspace_client = Mock() |
| 34 | + mock_workspace_client.serving_endpoints.get_open_ai_client.return_value = mock_openai_client |
| 35 | + |
| 36 | + with patch("databricks.sdk.WorkspaceClient", return_value=mock_workspace_client): |
| 37 | + client = get_openai_client(timeout=30.0, max_retries=2) |
| 38 | + |
| 39 | + # Verify default WorkspaceClient was created and kwargs were passed |
| 40 | + mock_workspace_client.serving_endpoints.get_open_ai_client.assert_called_once_with( |
| 41 | + timeout=30.0, max_retries=2 |
| 42 | + ) |
| 43 | + |
| 44 | + # Verify the client is returned |
| 45 | + assert client == mock_openai_client |
| 46 | + |
| 47 | + |
| 48 | +def test_get_openai_client_without_timeout_and_retries() -> None: |
| 49 | + """Test get_openai_client doesn't pass kwargs when not provided.""" |
| 50 | + |
| 51 | + mock_openai_client = Mock() |
| 52 | + |
| 53 | + mock_workspace_client = Mock() |
| 54 | + mock_workspace_client.serving_endpoints.get_open_ai_client.return_value = mock_openai_client |
| 55 | + |
| 56 | + client = get_openai_client(workspace_client=mock_workspace_client) |
| 57 | + |
| 58 | + # Verify the OpenAI client was obtained without kwargs |
| 59 | + mock_workspace_client.serving_endpoints.get_open_ai_client.assert_called_once_with() |
| 60 | + |
| 61 | + # Verify the client is returned |
| 62 | + assert client == mock_openai_client |
0 commit comments