Skip to content

Commit 808e9b4

Browse files
committed
feat: add test_tos.py
1 parent 1364a8c commit 808e9b4

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

tests/test_tos.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from unittest import mock
17+
import veadk.database.tos.tos_client as tos_mod
18+
19+
# 使用 pytest-asyncio
20+
pytest_plugins = ("pytest_asyncio",)
21+
22+
23+
@pytest.fixture
24+
def mock_client(monkeypatch):
25+
fake_client = mock.Mock()
26+
27+
monkeypatch.setattr(tos_mod.tos, "TosClientV2", lambda *a, **k: fake_client)
28+
29+
class FakeExceptions:
30+
class TosServerError(Exception):
31+
def __init__(self, msg):
32+
super().__init__(msg)
33+
self.status_code = None
34+
35+
monkeypatch.setattr(tos_mod.tos, "exceptions", FakeExceptions)
36+
monkeypatch.setattr(
37+
tos_mod.tos,
38+
"StorageClassType",
39+
type("S", (), {"Storage_Class_Standard": "STANDARD"}),
40+
)
41+
monkeypatch.setattr(
42+
tos_mod.tos, "ACLType", type("A", (), {"ACL_Private": "private"})
43+
)
44+
45+
return fake_client
46+
47+
48+
@pytest.fixture
49+
def tos_client(mock_client):
50+
return tos_mod.TOSClient()
51+
52+
53+
def test_create_bucket_exists(tos_client, mock_client):
54+
mock_client.head_bucket.return_value = None # head_bucket 正常返回表示存在
55+
result = tos_client.create_bucket()
56+
assert result is True
57+
mock_client.create_bucket.assert_not_called()
58+
59+
60+
def test_create_bucket_not_exists(tos_client, mock_client):
61+
exc = tos_mod.tos.exceptions.TosServerError("not found")
62+
exc.status_code = 404
63+
mock_client.head_bucket.side_effect = exc
64+
65+
result = tos_client.create_bucket()
66+
assert result is True
67+
mock_client.create_bucket.assert_called_once()
68+
69+
70+
@pytest.mark.asyncio
71+
async def test_upload_bytes_success(tos_client, mock_client):
72+
mock_client.head_bucket.return_value = True
73+
data = b"hello world"
74+
75+
result = await tos_client.upload("obj-key", data)
76+
assert result is True
77+
mock_client.put_object.assert_called_once()
78+
79+
80+
@pytest.mark.asyncio
81+
async def test_upload_file_success(tmp_path, tos_client, mock_client):
82+
mock_client.head_bucket.return_value = True
83+
file_path = tmp_path / "file.txt"
84+
file_path.write_text("hello file")
85+
86+
result = await tos_client.upload("obj-key", str(file_path))
87+
assert result is True
88+
mock_client.put_object_from_file.assert_called_once()
89+
90+
91+
def test_download_success(tmp_path, tos_client, mock_client):
92+
save_path = tmp_path / "out.txt"
93+
mock_client.get_object.return_value = [b"abc", b"def"]
94+
95+
result = tos_client.download("obj-key", str(save_path))
96+
assert result is True
97+
assert save_path.read_bytes() == b"abcdef"
98+
99+
100+
def test_download_fail(tos_client, mock_client):
101+
mock_client.get_object.side_effect = Exception("boom")
102+
result = tos_client.download("obj-key", "somewhere.txt")
103+
assert result is False
104+
105+
106+
def test_close(tos_client, mock_client):
107+
tos_client.close()
108+
mock_client.close.assert_called_once()

veadk/database/tos/tos_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def _do_upload_file(self, object_key: str, file_path: str) -> bool:
115115
try:
116116
if not self._client:
117117
return False
118-
if not self.create_bucket(self._client, self.config.bucket_name):
118+
if not self.create_bucket():
119119
return False
120120

121121
self._client.put_object_from_file(

0 commit comments

Comments
 (0)