|
1 | 1 | from collections import OrderedDict
|
| 2 | +from unittest.mock import patch |
2 | 3 |
|
3 | 4 | import pytest
|
| 5 | +from huggingface_hub.utils import HfHubHTTPError |
4 | 6 | from torch import nn
|
5 | 7 |
|
6 |
| -from vllm.lora.utils import parse_fine_tuned_lora_name, replace_submodule |
| 8 | +from vllm.lora.utils import (get_adapter_absolute_path, |
| 9 | + parse_fine_tuned_lora_name, replace_submodule) |
7 | 10 | from vllm.utils import LRUCache
|
8 | 11 |
|
9 | 12 |
|
@@ -182,3 +185,55 @@ def test_lru_cache():
|
182 | 185 | assert 2 in cache
|
183 | 186 | assert 4 in cache
|
184 | 187 | assert 6 in cache
|
| 188 | + |
| 189 | + |
| 190 | +# Unit tests for get_adapter_absolute_path |
| 191 | +@patch('os.path.isabs') |
| 192 | +def test_get_adapter_absolute_path_absolute(mock_isabs): |
| 193 | + path = '/absolute/path/to/lora' |
| 194 | + mock_isabs.return_value = True |
| 195 | + assert get_adapter_absolute_path(path) == path |
| 196 | + |
| 197 | + |
| 198 | +@patch('os.path.expanduser') |
| 199 | +def test_get_adapter_absolute_path_expanduser(mock_expanduser): |
| 200 | + # Path with ~ that needs to be expanded |
| 201 | + path = '~/relative/path/to/lora' |
| 202 | + absolute_path = '/home/user/relative/path/to/lora' |
| 203 | + mock_expanduser.return_value = absolute_path |
| 204 | + assert get_adapter_absolute_path(path) == absolute_path |
| 205 | + |
| 206 | + |
| 207 | +@patch('os.path.exists') |
| 208 | +@patch('os.path.abspath') |
| 209 | +def test_get_adapter_absolute_path_local_existing(mock_abspath, mock_exist): |
| 210 | + # Relative path that exists locally |
| 211 | + path = 'relative/path/to/lora' |
| 212 | + absolute_path = '/absolute/path/to/lora' |
| 213 | + mock_exist.return_value = True |
| 214 | + mock_abspath.return_value = absolute_path |
| 215 | + assert get_adapter_absolute_path(path) == absolute_path |
| 216 | + |
| 217 | + |
| 218 | +@patch('huggingface_hub.snapshot_download') |
| 219 | +@patch('os.path.exists') |
| 220 | +def test_get_adapter_absolute_path_huggingface(mock_exist, |
| 221 | + mock_snapshot_download): |
| 222 | + # Hugging Face model identifier |
| 223 | + path = 'org/repo' |
| 224 | + absolute_path = '/mock/snapshot/path' |
| 225 | + mock_exist.return_value = False |
| 226 | + mock_snapshot_download.return_value = absolute_path |
| 227 | + assert get_adapter_absolute_path(path) == absolute_path |
| 228 | + |
| 229 | + |
| 230 | +@patch('huggingface_hub.snapshot_download') |
| 231 | +@patch('os.path.exists') |
| 232 | +def test_get_adapter_absolute_path_huggingface_error(mock_exist, |
| 233 | + mock_snapshot_download): |
| 234 | + # Hugging Face model identifier with download error |
| 235 | + path = 'org/repo' |
| 236 | + mock_exist.return_value = False |
| 237 | + mock_snapshot_download.side_effect = HfHubHTTPError( |
| 238 | + "failed to query model info") |
| 239 | + assert get_adapter_absolute_path(path) == path |
0 commit comments