Skip to content

Commit 0ef6219

Browse files
committed
add initial tests for storage
1 parent 0b7123d commit 0ef6219

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
from typing import ClassVar
3+
from unittest.mock import AsyncMock, MagicMock, patch
4+
5+
import pytest
6+
from pydantic import BaseModel
7+
8+
from thirdweb_ai.services.storage import Storage
9+
10+
11+
class MockStorage(Storage):
12+
def __init__(self, secret_key: str):
13+
super().__init__(secret_key=secret_key)
14+
self.base_url = "https://storage.thirdweb.com"
15+
16+
17+
@pytest.fixture
18+
def storage():
19+
return MockStorage(secret_key=os.getenv("__THIRDWEB_SECRET_KEY_DEV") or "test-key")
20+
21+
22+
class TestStorage:
23+
# Constants
24+
TEST_IPFS_HASH: ClassVar[str] = "ipfs://QmTcHZQ5QEjjbBMJrz7Xaz9AQyVBqsKCS4YQQ71B3gDQ4f"
25+
TEST_CONTENT: ClassVar[dict[str, str]] = {"name": "test", "description": "test description"}
26+
27+
def test_fetch_ipfs_content(self, storage: Storage):
28+
fetch_ipfs_content = storage.fetch_ipfs_content.__wrapped__
29+
30+
# Test invalid IPFS hash
31+
result = fetch_ipfs_content(storage, ipfs_hash="invalid-hash")
32+
assert "error" in result
33+
34+
# Mock the _get method to return test content
35+
storage._get = MagicMock(return_value=self.TEST_CONTENT) # type:ignore[assignment] # noqa: SLF001
36+
37+
# Test valid IPFS hash
38+
result = fetch_ipfs_content(storage, ipfs_hash=self.TEST_IPFS_HASH)
39+
assert result == self.TEST_CONTENT
40+
storage._get.assert_called_once() # noqa: SLF001 # type:ignore[union-attr]
41+
42+
@pytest.mark.asyncio
43+
async def test_upload_to_ipfs_json(self, storage: Storage):
44+
upload_to_ipfs = storage.upload_to_ipfs.__wrapped__
45+
46+
# Create test data
47+
class TestModel(BaseModel):
48+
name: str
49+
value: int
50+
51+
test_model = TestModel(name="test", value=123)
52+
53+
# Mock the _async_post_file method
54+
with patch.object(storage, "_async_post_file", new_callable=AsyncMock) as mock_post:
55+
mock_post.return_value = {"IpfsHash": "QmTest123"}
56+
57+
# Test with dict
58+
result = await upload_to_ipfs(storage, data={"test": "value"})
59+
assert result == "ipfs://QmTest123"
60+
61+
# Test with Pydantic model
62+
result = await upload_to_ipfs(storage, data=test_model)
63+
assert result == "ipfs://QmTest123"
64+
65+
# Verify post was called
66+
assert mock_post.call_count == 2
67+

0 commit comments

Comments
 (0)