File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,9 @@ async def get_ipfs_size(
6868 dag_node = await asyncio .wait_for (
6969 self .ipfs_client .dag .get (hash ), timeout = timeout
7070 )
71+ result = 0
72+ if isinstance (dag_node , str ):
73+ dag_node = json .loads (dag_node )
7174 if isinstance (dag_node , dict ):
7275 if "Data" in dag_node and isinstance (dag_node ["Data" ], dict ):
7376 # This is the common structure for UnixFS nodes after aioipfs parsing
@@ -81,7 +84,11 @@ async def get_ipfs_size(
8184 "Tsize" in dag_node
8285 ): # Sometimes it might be at the top level directly
8386 result = dag_node ["Tsize" ]
84- elif "Links" in dag_node and isinstance (dag_node ["Links" ], list ):
87+ if (
88+ result == 0
89+ and "Links" in dag_node
90+ and isinstance (dag_node ["Links" ], list )
91+ ):
8592 total_size = 0
8693 for link in dag_node ["Links" ]:
8794 # In case it's a link list, get the Tsize property if exists
Original file line number Diff line number Diff line change 11import asyncio
2+ import json
23from unittest .mock import AsyncMock , patch
34
45import aioipfs
@@ -133,6 +134,31 @@ async def test_get_ipfs_size_non_dict():
133134 ipfs_client .block .stat .assert_called_once_with ("test_hash" )
134135
135136
137+ @pytest .mark .asyncio
138+ async def test_get_ipfs_size_json ():
139+ """Test when dag.get returns a non-dictionary (bytes)"""
140+ # Setup
141+ mock_dag_get_data = {
142+ "Data" : {"/" : {"bytes" : "CAE" }},
143+ "Links" : [
144+ {"Hash" : {"/" : "hash1" }, "Name" : "test1" , "Tsize" : 12596071 },
145+ {"Hash" : {"/" : "hash2" }, "Name" : "test2" , "Tsize" : 20168090 },
146+ ],
147+ }
148+ ipfs_client = AsyncMock ()
149+ ipfs_client .dag .get = AsyncMock (return_value = json .dumps (mock_dag_get_data ))
150+
151+ service = IpfsService (ipfs_client = ipfs_client )
152+
153+ # Execute
154+ result = await service .get_ipfs_size ("test_hash" )
155+
156+ # Assert
157+ assert result == 32764161
158+ ipfs_client .dag .get .assert_called_once_with ("test_hash" )
159+ ipfs_client .block .stat .assert_not_called ()
160+
161+
136162@pytest .mark .asyncio
137163async def test_get_ipfs_size_api_error ():
138164 """Test handling of APIError"""
You can’t perform that action at this time.
0 commit comments