|
| 1 | +from unittest.mock import patch, Mock |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +from huggingface_hub import hf_hub_url |
| 6 | +from requests.adapters import HTTPAdapter |
| 7 | + |
| 8 | +from plantumlcli.utils.session import TimeoutHTTPAdapter, get_requests_session, get_random_ua |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def mock_requests_session(): |
| 13 | + with patch('requests.session') as mock_session: |
| 14 | + yield mock_session.return_value |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture |
| 18 | +def mock_ua_pool(): |
| 19 | + with patch('plantumlcli.utils.session._ua_pool') as mock_pool: |
| 20 | + mock_pool.return_value.get_random_user_agent.return_value = 'MockUserAgent' |
| 21 | + yield mock_pool |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture() |
| 25 | +def example_url(): |
| 26 | + return hf_hub_url( |
| 27 | + repo_id='deepghs/danbooru_newest', |
| 28 | + repo_type='dataset', |
| 29 | + filename='README.md' |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.unittest |
| 34 | +class TestUtilsSession: |
| 35 | + def test_timeout_http_adapter_init(self, ): |
| 36 | + adapter = TimeoutHTTPAdapter() |
| 37 | + assert adapter.timeout == 15 |
| 38 | + |
| 39 | + adapter = TimeoutHTTPAdapter(timeout=30) |
| 40 | + assert adapter.timeout == 30 |
| 41 | + |
| 42 | + def test_timeout_http_adapter_send(self, ): |
| 43 | + adapter = TimeoutHTTPAdapter(timeout=10) |
| 44 | + mock_request = Mock() |
| 45 | + mock_kwargs = {} |
| 46 | + |
| 47 | + with patch.object(HTTPAdapter, 'send') as mock_send: |
| 48 | + adapter.send(mock_request, **mock_kwargs) |
| 49 | + mock_send.assert_called_once_with(mock_request, timeout=10) |
| 50 | + |
| 51 | + mock_kwargs = {'timeout': 20} |
| 52 | + with patch.object(HTTPAdapter, 'send') as mock_send: |
| 53 | + adapter.send(mock_request, **mock_kwargs) |
| 54 | + mock_send.assert_called_once_with(mock_request, timeout=20) |
| 55 | + |
| 56 | + def test_get_requests_session(self, mock_ua_pool): |
| 57 | + session = get_requests_session() |
| 58 | + assert isinstance(session, requests.Session) |
| 59 | + assert 'User-Agent' in session.headers |
| 60 | + assert session.headers['User-Agent'] == 'MockUserAgent' |
| 61 | + |
| 62 | + custom_headers = {'Custom-Header': 'Value'} |
| 63 | + session = get_requests_session(headers=custom_headers) |
| 64 | + assert 'Custom-Header' in session.headers |
| 65 | + assert session.headers['Custom-Header'] == 'Value' |
| 66 | + |
| 67 | + session = get_requests_session(verify=False) |
| 68 | + assert session.verify is False |
| 69 | + |
| 70 | + existing_session = requests.Session() |
| 71 | + session = get_requests_session(session=existing_session) |
| 72 | + assert session is existing_session |
| 73 | + |
| 74 | + def test_get_requests_session_with_custom_params(self): |
| 75 | + session = get_requests_session(max_retries=3, timeout=30) |
| 76 | + assert isinstance(session, requests.Session) |
| 77 | + # You might want to add more assertions here to check if the custom parameters are applied correctly |
| 78 | + |
| 79 | + def test_get_random_ua(self, mock_ua_pool): |
| 80 | + ua = get_random_ua() |
| 81 | + assert ua == 'MockUserAgent' |
| 82 | + mock_ua_pool.return_value.get_random_user_agent.assert_called_once() |
0 commit comments