11import tempfile
22from pathlib import Path
3+ from unittest .mock import AsyncMock , patch
34
45import pytest
56
67from aleph .sdk import AlephHttpClient
7- from aleph .sdk .conf import settings as sdk_settings
8+
9+ from .conftest import make_mock_get_session
10+
11+
12+ def make_mock_download_client (item_hash : str ) -> AlephHttpClient :
13+ if item_hash == "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" :
14+ return make_mock_get_session (b"test\n " )
15+ if item_hash == "Qmdy5LaAL4eghxE7JD6Ah5o4PJGarjAV9st8az2k52i1vq" :
16+ return make_mock_get_session (bytes (5817703 ))
17+ raise NotImplementedError
818
919
1020@pytest .mark .parametrize (
1626)
1727@pytest .mark .asyncio
1828async def test_download (file_hash : str , expected_size : int ):
19- async with AlephHttpClient (api_server = sdk_settings .API_HOST ) as client :
20- file_content = await client .download_file (file_hash ) # File is 5B
21- file_size = len (file_content )
22- assert file_size == expected_size
29+ mock_download_client = make_mock_download_client (file_hash )
30+ async with mock_download_client :
31+ file_content = await mock_download_client .download_file (file_hash )
32+ file_size = len (file_content )
33+ assert file_size == expected_size
2334
2435
2536@pytest .mark .asyncio
2637async def test_download_to_file ():
38+ file_hash = "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH"
39+ mock_download_client = make_mock_download_client (file_hash )
2740 with tempfile .TemporaryDirectory () as temp_dir :
2841 temp_dir_path = Path (temp_dir )
2942 download_path = temp_dir_path / "test.txt"
3043
31- async with AlephHttpClient ( api_server = sdk_settings . API_HOST ) as client :
32- returned_path = await client .download_file_to_path (
33- "QmeomffUNfmQy76CQGy9NdmqEnnHU9soCexBnGU3ezPHVH" , str (download_path )
44+ async with mock_download_client :
45+ returned_path = await mock_download_client .download_file_to_path (
46+ file_hash , str (download_path )
3447 )
3548
3649 assert returned_path == download_path
@@ -48,7 +61,8 @@ async def test_download_to_file():
4861)
4962@pytest .mark .asyncio
5063async def test_download_ipfs (file_hash : str , expected_size : int ):
51- async with AlephHttpClient (api_server = sdk_settings .API_HOST ) as client :
52- file_content = await client .download_file_ipfs (file_hash ) # 5817703 B FILE
53- file_size = len (file_content )
54- assert file_size == expected_size
64+ mock_download_client = make_mock_download_client (file_hash )
65+ async with mock_download_client :
66+ file_content = await mock_download_client .download_file_ipfs (file_hash )
67+ file_size = len (file_content )
68+ assert file_size == expected_size
0 commit comments