Skip to content

Commit c012c9d

Browse files
committed
dev(hansbug): add planutml unittest
1 parent 9f5f87a commit c012c9d

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

.github/workflows/doc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
git branch -av
4848
git remote -v
4949
git tag
50-
plantumlcli -c
50+
python -m plantumlcli -c
5151
make pdocs
5252
mv ./docs/build/html ./public
5353
- name: Deploy to Github Page

plantumlcli/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from plantumlcli.entry.cli import cli as plantumlcli_cli
2+
3+
if __name__ == '__main__':
4+
plantumlcli_cli()

plantumlcli/models/remote.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import zlib
66
from typing import Optional, Mapping, Any, Union, Tuple
77

8-
import requests
98
from pyquery import PyQuery
109
from urlobject import URLObject
1110

1211
from .base import Plantuml, PlantumlResourceType, _has_cairosvg
12+
from ..utils import get_requests_session
1313

1414
PLANTUML_HOST_ENV = 'PLANTUML_HOST'
1515
OFFICIAL_PLANTUML_HOST = 'http://www.plantuml.com/plantuml'
@@ -56,7 +56,7 @@ def __init__(self, host: str, **kwargs):
5656
_check_remote(self.__host)
5757
self.__host = _host_process(self.__host)
5858

59-
self.__session = requests.session()
59+
self.__session = get_requests_session()
6060
self.__request_params = kwargs
6161

6262
@classmethod

test/utils/test_session.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)