-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved async client files into new folder. Finished coverage of async_…
…client, will probably need to review few of them to make sure they cover all functionality. Started test on utils.py
- Loading branch information
cadea
authored and
cadea
committed
Feb 26, 2024
1 parent
a43a29a
commit 6fa902a
Showing
6 changed files
with
849 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
|
||
from fractal.matrix.async_client import FractalAsyncClient, RoomSendResponse | ||
from nio import RoomSendError | ||
|
||
|
||
async def test_send_message_contains_bytes(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_bytes = b"test_message" | ||
await test_fractal_client.send_message(room=room, message=test_bytes) | ||
expected_argument = {"msgtype": "taskiq.task", "body": "test_message"} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_contains_string(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_string = "test_string" | ||
await test_fractal_client.send_message(room=room, message=test_string) | ||
expected_argument = {"msgtype": "taskiq.task", "body": "test_string"} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_contains_list(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_list = ["testlist"] | ||
await test_fractal_client.send_message(room=room, message=test_list) | ||
expected_argument = {"msgtype": "taskiq.task", "body": test_list} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_contains_dictionary(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_dic = {"val": "1"} | ||
await test_fractal_client.send_message(room=room, message=test_dic) | ||
expected_argument = {"msgtype": "taskiq.task", "body": test_dic} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_returns_error(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = RoomSendError(message="Test Error Message") | ||
with patch("fractal.matrix.async_client.logger") as mock_logger: | ||
room = "test_room" | ||
test_dic = {"val": "1"} | ||
await test_fractal_client.send_message(room=room, message=test_dic) | ||
mock_logger.error.assert_called_once() | ||
|
||
|
||
async def test_send_message_raises_exception(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.side_effect = Exception() | ||
with patch("fractal.matrix.async_client.logger") as mock_logger: | ||
room = "test_room" | ||
test_dic = {"val": "1"} | ||
await test_fractal_client.send_message(room=room, message=test_dic) | ||
mock_logger.error.assert_called_once() |
35 changes: 35 additions & 0 deletions
35
tests/fractal-async-client/test_fractal_async_client_get_latest_sync_token.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
|
||
import pytest | ||
from fractal.matrix.async_client import FractalAsyncClient | ||
from fractal.matrix.exceptions import GetLatestSyncTokenError | ||
from nio import RoomMessagesError, RoomMessagesResponse | ||
|
||
|
||
async def test_get_latest_sync_token_no_room_id(): | ||
client = FractalAsyncClient() | ||
assert client.room_id == None | ||
with pytest.raises(GetLatestSyncTokenError) as e: | ||
await client.get_latest_sync_token() | ||
assert "No room id provided" in str(e.value) | ||
|
||
|
||
async def test_get_latest_sync_token_successful_message(): | ||
sample_room_id = "sample_id" | ||
client = FractalAsyncClient(room_id=sample_room_id) | ||
mock_response = RoomMessagesResponse( | ||
room_id=sample_room_id, chunk=[], start="mock_sync_token" | ||
) | ||
client.room_messages = AsyncMock(return_value=mock_response) | ||
sync_token = await client.get_latest_sync_token() | ||
assert sync_token == "mock_sync_token" | ||
|
||
|
||
async def test_get_latest_sync_token_message_error(): | ||
sample_room_id = "sample_id" | ||
client = FractalAsyncClient(room_id=sample_room_id) | ||
mock_response = RoomMessagesError("Room Message Error") | ||
client.room_messages = AsyncMock(return_value=mock_response) | ||
with pytest.raises(GetLatestSyncTokenError) as e: | ||
await client.get_latest_sync_token() | ||
assert "Room Message Error" in str(e.value) |
143 changes: 143 additions & 0 deletions
143
tests/fractal-async-client/test_fractal_async_client_invite.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,143 @@ | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
|
||
import pytest | ||
from fractal.matrix.async_client import FractalAsyncClient | ||
from fractal.matrix.exceptions import InvalidMatrixIdException | ||
from nio import ( | ||
RoomGetStateEventError, | ||
RoomGetStateEventResponse, | ||
RoomInviteError, | ||
RoomInviteResponse, | ||
RoomPutStateError, | ||
RoomPutStateResponse, | ||
) | ||
|
||
|
||
async def test_invite_if_not_admin(): | ||
sample_user_id = "@sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
client = FractalAsyncClient() | ||
with pytest.raises(Exception) as e: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=False) | ||
assert "FIXME: Only admin invites are supported for now." in str(e.value) | ||
|
||
|
||
async def test_invite_all_lower_case_failed(): | ||
sample_user_id = "@SaMplE_uSer:sample_domain" | ||
sample_room_id = "sample_id" | ||
client = FractalAsyncClient() | ||
with pytest.raises(Exception) as e: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
assert "Matrix ids must be lowercase." in str(e.value) | ||
|
||
|
||
async def test_invite_send_invite(): | ||
sample_user_id = "@sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
sample_event_id = "event_id" | ||
sample_state_key = "state_key" | ||
client = FractalAsyncClient() | ||
client.room_invite = AsyncMock(return_value=RoomInviteResponse()) | ||
content = {"users": {}} | ||
client.room_get_state_event = AsyncMock( | ||
return_value=RoomGetStateEventResponse( | ||
content=content, | ||
event_type=sample_event_id, | ||
state_key=sample_state_key, | ||
room_id=sample_room_id, | ||
) | ||
) | ||
client.room_put_state = AsyncMock( | ||
return_value=RoomPutStateResponse(event_id=sample_event_id, room_id=sample_room_id) | ||
) | ||
with patch("fractal.matrix.async_client.logger", new=MagicMock()) as mock_logger: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
mock_logger.info.assert_called_once_with( | ||
f"Sending invite to {sample_room_id} to user ({sample_user_id})" | ||
) | ||
|
||
|
||
async def test_invite_raise_exception_for_userID(): | ||
sample_user_id = "sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
client = FractalAsyncClient() | ||
with pytest.raises(InvalidMatrixIdException) as e: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
assert f"{sample_user_id} is not a valid Matrix ID." in str(e.value) | ||
|
||
|
||
async def test_invite_get_power_levels(): | ||
sample_user_id = "@sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
sample_event_id = "event_id" | ||
sample_state_key = "state_key" | ||
client = FractalAsyncClient() | ||
client.room_invite = AsyncMock(return_value=RoomInviteResponse()) | ||
content = {"users": {}} | ||
client.room_get_state_event = AsyncMock( | ||
return_value=RoomGetStateEventResponse( | ||
content=content, | ||
event_type=sample_event_id, | ||
state_key=sample_state_key, | ||
room_id=sample_room_id, | ||
) | ||
) | ||
client.room_put_state = AsyncMock( | ||
return_value=RoomPutStateResponse(event_id=sample_event_id, room_id=sample_room_id) | ||
) | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
client.room_get_state_event.assert_called_once_with(sample_room_id, "m.room.power_levels") | ||
|
||
|
||
async def test_invite_room_get_state_event_error_when_has_message(): | ||
sample_user_id = "@sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
client = FractalAsyncClient() | ||
client.room_invite = AsyncMock() | ||
client.room_get_state_event = AsyncMock(return_value=RoomGetStateEventError("Error message")) | ||
with pytest.raises(Exception) as e: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
assert "Error message" in str(e.value) | ||
|
||
|
||
# @pytest.mark.skip("Having trouble reaching the else condition and testing the exception") | ||
async def test_invite_room_get_state_event_error_when_no_message(): | ||
sample_user_id = "@sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
sample_event_id = "event_id" | ||
sample_state_key = "state_key" | ||
client = FractalAsyncClient() | ||
client.room_invite = AsyncMock() | ||
client.room_get_state_event = AsyncMock( | ||
return_value=RoomGetStateEventResponse( | ||
content={"errcode": "sample_error"}, | ||
event_type=sample_event_id, | ||
state_key=sample_state_key, | ||
room_id=sample_room_id, | ||
) | ||
) | ||
with pytest.raises(Exception) as e: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
assert "error" in str(e.value) | ||
|
||
|
||
async def test_invite_room_put_state_error(): | ||
sample_user_id = "@sample_user:sample_domain" | ||
sample_room_id = "sample_id" | ||
sample_event_id = "event_id" | ||
sample_state_key = "state_key" | ||
client = FractalAsyncClient() | ||
client.room_invite = AsyncMock(return_value=RoomInviteResponse()) | ||
content = {"users": {}} | ||
client.room_get_state_event = AsyncMock( | ||
return_value=RoomGetStateEventResponse( | ||
content=content, | ||
event_type=sample_event_id, | ||
state_key=sample_state_key, | ||
room_id=sample_room_id, | ||
) | ||
) | ||
client.room_put_state = AsyncMock(return_value=RoomPutStateError("Room Put State Error")) | ||
with pytest.raises(Exception) as e: | ||
await client.invite(user_id=sample_user_id, room_id=sample_room_id, admin=True) | ||
assert "Room Put State Error" in str(e.value) |
82 changes: 82 additions & 0 deletions
82
tests/fractal-async-client/test_fractal_async_client_send_message.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from unittest.mock import AsyncMock, MagicMock, patch | ||
|
||
from fractal.matrix.async_client import FractalAsyncClient, RoomSendResponse | ||
from nio import RoomSendError | ||
|
||
|
||
async def test_send_message_contains_bytes(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_bytes = b"test_message" | ||
await test_fractal_client.send_message(room=room, message=test_bytes) | ||
expected_argument = {"msgtype": "taskiq.task", "body": "test_message"} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_contains_string(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_string = "test_string" | ||
await test_fractal_client.send_message(room=room, message=test_string) | ||
expected_argument = {"msgtype": "taskiq.task", "body": "test_string"} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_contains_list(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_list = ["testlist"] | ||
await test_fractal_client.send_message(room=room, message=test_list) | ||
expected_argument = {"msgtype": "taskiq.task", "body": test_list} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_contains_dictionary(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = MagicMock(spec=RoomSendResponse) | ||
room = "test_room" | ||
test_dic = {"val": "1"} | ||
await test_fractal_client.send_message(room=room, message=test_dic) | ||
expected_argument = {"msgtype": "taskiq.task", "body": test_dic} | ||
mock_room_send.assert_called_with(room, "taskiq.task", expected_argument) | ||
|
||
|
||
async def test_send_message_returns_error(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.return_value = RoomSendError(message="Test Error Message") | ||
with patch("fractal.matrix.async_client.logger") as mock_logger: | ||
room = "test_room" | ||
test_dic = {"val": "1"} | ||
await test_fractal_client.send_message(room=room, message=test_dic) | ||
mock_logger.error.assert_called_once() | ||
|
||
|
||
async def test_send_message_raises_exception(): | ||
test_fractal_client = FractalAsyncClient() | ||
with patch( | ||
"fractal.matrix.async_client.FractalAsyncClient.room_send", new=AsyncMock() | ||
) as mock_room_send: | ||
mock_room_send.side_effect = Exception() | ||
with patch("fractal.matrix.async_client.logger") as mock_logger: | ||
room = "test_room" | ||
test_dic = {"val": "1"} | ||
await test_fractal_client.send_message(room=room, message=test_dic) | ||
mock_logger.error.assert_called_once() |
Oops, something went wrong.