-
Notifications
You must be signed in to change notification settings - Fork 9
Fix: Upload Test wasn't mocked #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+56
−28
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,60 @@ | ||
| import hashlib | ||
| from unittest.mock import AsyncMock, patch | ||
|
|
||
| import pytest | ||
| from aleph_message.models import AlephMessage, StoreMessage | ||
| from aleph_message.models import StoreMessage | ||
| from aleph_message.status import MessageStatus | ||
|
|
||
| from aleph.sdk import AuthenticatedAlephHttpClient | ||
| from aleph.sdk.chains.common import get_fallback_private_key | ||
| from aleph.sdk.chains.ethereum import ETHAccount | ||
| from aleph.sdk.types import StorageEnum | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_upload_with_message(): | ||
| pkey = get_fallback_private_key() | ||
| account = ETHAccount(private_key=pkey) | ||
| @pytest.fixture | ||
| def mock_authenticated_aleph_http_client(): | ||
| with patch( | ||
| "aleph.sdk.AuthenticatedAlephHttpClient", autospec=True | ||
| ) as MockHttpClient: | ||
| pkey = get_fallback_private_key() | ||
| account = ETHAccount(private_key=pkey) | ||
|
|
||
| http_session = AsyncMock() | ||
| mock_client = MockHttpClient.return_value | ||
| mock_client.http_session = http_session | ||
| mock_client.account = account | ||
| return mock_client | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_upload_with_message(mock_authenticated_aleph_http_client): | ||
| content = b"Test pyaleph upload\n" | ||
| file_hash = hashlib.sha256(content).hexdigest() | ||
|
|
||
| async with AuthenticatedAlephHttpClient(account=account, api_server=None) as client: | ||
| message, status = await client.create_store( | ||
| address=account.get_address(), | ||
| file_content=content, | ||
| storage_engine=StorageEnum.storage, | ||
| sync=True, | ||
| ) | ||
| print(message, status) | ||
|
|
||
| assert status == MessageStatus.PROCESSED | ||
| assert message.content.item_hash == file_hash | ||
|
|
||
| server_content = await client.download_file(file_hash=file_hash) | ||
| assert server_content == content | ||
|
|
||
| server_message: AlephMessage = await client.get_message( | ||
| item_hash=message.item_hash, message_type=StoreMessage | ||
| ) | ||
| assert server_message.content.item_hash == file_hash | ||
| message = AsyncMock() | ||
| message.content.item_hash = file_hash | ||
| status = MessageStatus.PROCESSED | ||
| mock_authenticated_aleph_http_client.create_store.return_value = (message, status) | ||
|
|
||
| mock_authenticated_aleph_http_client.download_file.return_value = content | ||
|
|
||
| mock_authenticated_aleph_http_client.get_message.return_value = message | ||
|
|
||
| message, status = await mock_authenticated_aleph_http_client.create_store( | ||
| address=mock_authenticated_aleph_http_client.account.get_address(), | ||
| file_content=content, | ||
| storage_engine=StorageEnum.storage, | ||
| sync=True, | ||
| ) | ||
|
|
||
| assert status == MessageStatus.PROCESSED | ||
| assert message.content.item_hash == file_hash | ||
|
|
||
| server_content = await mock_authenticated_aleph_http_client.download_file( | ||
| file_hash=file_hash | ||
| ) | ||
| assert server_content == content | ||
|
|
||
| server_message = await mock_authenticated_aleph_http_client.get_message( | ||
| item_hash=message.item_hash, message_type=StoreMessage | ||
| ) | ||
| assert server_message.content.item_hash == file_hash | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.