1414
1515import pytest
1616from unittest import mock
17- import veadk .integrations .ve_tos .ve_tos as tos_mod
17+
18+ # Check if tos module is available
19+ import importlib
20+
21+ TOS_AVAILABLE = False
22+ try :
23+ importlib .import_module ("veadk.integrations.ve_tos.ve_tos" )
24+ TOS_AVAILABLE = True
25+ except ImportError :
26+ pass
27+
28+ # Skip tests that require tos module if it's not available
29+ require_tos = pytest .mark .skipif (not TOS_AVAILABLE , reason = "tos module not available" )
1830
1931# 使用 pytest-asyncio
2032pytest_plugins = ("pytest_asyncio" ,)
2133
2234
2335@pytest .fixture
36+ @require_tos
2437def mock_client (monkeypatch ):
38+ import veadk .integrations .ve_tos .ve_tos as tos_mod
39+
2540 fake_client = mock .Mock ()
2641
2742 monkeypatch .setenv ("DATABASE_TOS_REGION" , "test-region" )
@@ -33,9 +48,17 @@ def mock_client(monkeypatch):
3348
3449 class FakeExceptions :
3550 class TosServerError (Exception ):
36- def __init__ (self , msg ):
51+ def __init__ (
52+ self ,
53+ msg : str ,
54+ code : int = 0 ,
55+ host_id : str = "" ,
56+ resource : str = "" ,
57+ request_id : str = "" ,
58+ header = None ,
59+ ):
3760 super ().__init__ (msg )
38- self .status_code = None
61+ self .status_code = code
3962
4063 monkeypatch .setattr (tos_mod .tos , "exceptions" , FakeExceptions )
4164 monkeypatch .setattr (
@@ -51,27 +74,34 @@ def __init__(self, msg):
5174
5275
5376@pytest .fixture
77+ @require_tos
5478def tos_client (mock_client ):
79+ import veadk .integrations .ve_tos .ve_tos as tos_mod
80+
5581 return tos_mod .VeTOS ()
5682
5783
84+ @require_tos
5885def test_create_bucket_exists (tos_client , mock_client ):
5986 mock_client .head_bucket .return_value = None # head_bucket 正常返回表示存在
6087 result = tos_client .create_bucket ()
6188 assert result is True
6289 mock_client .create_bucket .assert_not_called ()
6390
6491
92+ @require_tos
6593def test_create_bucket_not_exists (tos_client , mock_client ):
66- exc = tos_mod .tos .exceptions .TosServerError ("not found" )
67- exc .status_code = 404
94+ import veadk .integrations .ve_tos .ve_tos as tos_mod
95+
96+ exc = tos_mod .tos .exceptions .TosServerError (msg = "not found" , code = 404 )
6897 mock_client .head_bucket .side_effect = exc
6998
7099 result = tos_client .create_bucket ()
71100 assert result is True
72101 mock_client .create_bucket .assert_called_once ()
73102
74103
104+ @require_tos
75105@pytest .mark .asyncio
76106async def test_upload_bytes_success (tos_client , mock_client ):
77107 mock_client .head_bucket .return_value = True
@@ -83,6 +113,7 @@ async def test_upload_bytes_success(tos_client, mock_client):
83113 mock_client .close .assert_called_once ()
84114
85115
116+ @require_tos
86117@pytest .mark .asyncio
87118async def test_upload_file_success (tmp_path , tos_client , mock_client ):
88119 mock_client .head_bucket .return_value = True
@@ -95,6 +126,7 @@ async def test_upload_file_success(tmp_path, tos_client, mock_client):
95126 mock_client .close .assert_called_once ()
96127
97128
129+ @require_tos
98130def test_download_success (tmp_path , tos_client , mock_client ):
99131 save_path = tmp_path / "out.txt"
100132 mock_client .get_object .return_value = [b"abc" , b"def" ]
@@ -104,7 +136,32 @@ def test_download_success(tmp_path, tos_client, mock_client):
104136 assert save_path .read_bytes () == b"abcdef"
105137
106138
139+ @require_tos
107140def test_download_fail (tos_client , mock_client ):
108141 mock_client .get_object .side_effect = Exception ("boom" )
109142 result = tos_client .download ("obj-key" , "somewhere.txt" )
110143 assert result is False
144+
145+
146+ @require_tos
147+ @pytest .mark .skipif (TOS_AVAILABLE , reason = "tos module is available" )
148+ def test_tos_import_error ():
149+ """Test VeTOS behavior when tos module is not installed"""
150+ # Remove tos from sys.modules to simulate it's not installed
151+ import sys
152+
153+ original_tos = sys .modules .get ("tos" )
154+ if "tos" in sys .modules :
155+ del sys .modules ["tos" ]
156+
157+ try :
158+ # Try to import ve_tos module, which should raise ImportError
159+ with pytest .raises (ImportError ) as exc_info :
160+ pass
161+
162+ # Check that the error message contains installation instructions
163+ assert "pip install tos" in str (exc_info .value )
164+ finally :
165+ # Restore original state
166+ if original_tos is not None :
167+ sys .modules ["tos" ] = original_tos
0 commit comments