Skip to content

Commit 7bb282d

Browse files
committed
Fix: macos 12 build
Fix: Test Fix: try to force use python 3.11 on macos-12 Fix: Force use 3.11 on macos 11 Fix; Read me Fix: test upload wasn't mocked & fix black Fix: Mocking test Solutions: Use AsyncMock instead of Magic Mock Fix test_upload Unused Import
1 parent 00592c2 commit 7bb282d

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

.github/workflows/build-wheels.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ jobs:
1212
build:
1313
strategy:
1414
matrix:
15-
os: [ macos-11, macos-12, ubuntu-20.04, ubuntu-22.04 ]
16-
runs-on: ${{matrix.os}}
15+
os: [macos-11, macos-12, ubuntu-20.04, ubuntu-22.04]
16+
runs-on: ${{ matrix.os }}
1717

1818
steps:
1919
- uses: actions/checkout@v3
@@ -22,6 +22,12 @@ jobs:
2222
if: startsWith(matrix.os, 'ubuntu-')
2323
run: sudo echo RESET grub-efi/install_devices | sudo debconf-communicate grub-pc
2424

25+
- name: Set up Python
26+
if: startsWith(matrix.os, 'macos')
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: 3.11
30+
2531
- name: Cache dependencies
2632
uses: actions/cache@v3
2733
with:

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ $ apt-get install -y python3-pip libsecp256k1-dev
2020
```
2121
Using some chains may also require installing `libgmp3-dev`.
2222

23-
### macOs
23+
### macOs
24+
This project does not support Python 3.12 on macOS. Please use Python 3.11 instead.
2425
```shell
2526
$ brew tap cuber/homebrew-libsecp256k1
2627
$ brew install libsecp256k1

tests/unit/test_upload.py

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,60 @@
11
import hashlib
2+
from unittest.mock import AsyncMock, patch
23

34
import pytest
4-
from aleph_message.models import AlephMessage, StoreMessage
5+
from aleph_message.models import StoreMessage
56
from aleph_message.status import MessageStatus
67

7-
from aleph.sdk import AuthenticatedAlephHttpClient
88
from aleph.sdk.chains.common import get_fallback_private_key
99
from aleph.sdk.chains.ethereum import ETHAccount
1010
from aleph.sdk.types import StorageEnum
1111

1212

13-
@pytest.mark.asyncio
14-
async def test_upload_with_message():
15-
pkey = get_fallback_private_key()
16-
account = ETHAccount(private_key=pkey)
13+
@pytest.fixture
14+
def mock_authenticated_aleph_http_client():
15+
with patch(
16+
"aleph.sdk.AuthenticatedAlephHttpClient", autospec=True
17+
) as MockHttpClient:
18+
pkey = get_fallback_private_key()
19+
account = ETHAccount(private_key=pkey)
20+
21+
http_session = AsyncMock()
22+
mock_client = MockHttpClient.return_value
23+
mock_client.http_session = http_session
24+
mock_client.account = account
25+
return mock_client
26+
1727

28+
@pytest.mark.asyncio
29+
async def test_upload_with_message(mock_authenticated_aleph_http_client):
1830
content = b"Test pyaleph upload\n"
1931
file_hash = hashlib.sha256(content).hexdigest()
2032

21-
async with AuthenticatedAlephHttpClient(account=account, api_server=None) as client:
22-
message, status = await client.create_store(
23-
address=account.get_address(),
24-
file_content=content,
25-
storage_engine=StorageEnum.storage,
26-
sync=True,
27-
)
28-
print(message, status)
29-
30-
assert status == MessageStatus.PROCESSED
31-
assert message.content.item_hash == file_hash
32-
33-
server_content = await client.download_file(file_hash=file_hash)
34-
assert server_content == content
35-
36-
server_message: AlephMessage = await client.get_message(
37-
item_hash=message.item_hash, message_type=StoreMessage
38-
)
39-
assert server_message.content.item_hash == file_hash
33+
message = AsyncMock()
34+
message.content.item_hash = file_hash
35+
status = MessageStatus.PROCESSED
36+
mock_authenticated_aleph_http_client.create_store.return_value = (message, status)
37+
38+
mock_authenticated_aleph_http_client.download_file.return_value = content
39+
40+
mock_authenticated_aleph_http_client.get_message.return_value = message
41+
42+
message, status = await mock_authenticated_aleph_http_client.create_store(
43+
address=mock_authenticated_aleph_http_client.account.get_address(),
44+
file_content=content,
45+
storage_engine=StorageEnum.storage,
46+
sync=True,
47+
)
48+
49+
assert status == MessageStatus.PROCESSED
50+
assert message.content.item_hash == file_hash
51+
52+
server_content = await mock_authenticated_aleph_http_client.download_file(
53+
file_hash=file_hash
54+
)
55+
assert server_content == content
56+
57+
server_message = await mock_authenticated_aleph_http_client.get_message(
58+
item_hash=message.item_hash, message_type=StoreMessage
59+
)
60+
assert server_message.content.item_hash == file_hash

0 commit comments

Comments
 (0)