async def test_manage_retry_limit_reached(self, sleep_mock: CoroutineMock): send_emails_mock = CoroutineMock() service = SendEmailService( self.web_client, self.email_client, self.settings_storage, 10, 3, 2 ) with patch.object(service, "send_emails", send_emails_mock): await service.manage_retry( [ {"id": 3, "email": "*****@*****.**", "name": "Guy"}, {"id": 4, "email": "*****@*****.**", "name": "Guy"}, ], "Hi {name}! Have a nice day", "Subject", 4, ) sleep_mock.assert_not_awaited() send_emails_mock.assert_not_awaited() self.web_client.report_job_status.assert_awaited_once_with( { EmailResult.FAILURE: [ {"id": 3, "message_id": ""}, {"id": 4, "message_id": ""}, ] } )
async def test_on_queue_message_bulk_size_bigger_than_one(self): """ Confere que o handler real só é chamado quando o bucket atinge o limite máximo de tamanho. E que o handler é chamado com a lista de mensagens. * Bucket deve estart vazio após o "flush" """ class MyBucket(Bucket): def pop_all(self): return self._items handler_mock = CoroutineMock(__name__="handler") self.one_route_fixture["handler"] = handler_mock self.one_route_fixture["options"]["bulk_size"] = 2 consumer = Consumer( self.one_route_fixture, *self.connection_parameters, bucket_class=MyBucket, ) msgs = [ self._make_msg(delivery_tag=10), self._make_msg(delivery_tag=20), ] await consumer.on_queue_message(msg=msgs[0]) handler_mock.assert_not_awaited() await consumer.on_queue_message(msg=msgs[1]) handler_mock.assert_awaited_once_with(consumer.bucket._items) msgs[0].ack.assert_awaited_once() msgs[1].ack.assert_awaited_once() msgs[0].reject.assert_not_awaited() msgs[1].reject.assert_not_awaited()
async def test_shutdown_doest_not_closes_handlers_if_not_initialized(self): handler_factory = CoroutineMock(return_value=[ Mock(flush=CoroutineMock()), Mock(flush=CoroutineMock()) ]) logger = Logger(handler_factory=handler_factory) await logger.shutdown() handler_factory.assert_not_awaited() self.assertCountEqual([], logger.handlers)
async def test_set_master_volume_instantly_sets_player_volume_if_no_smooth_parameter( self, example_music_manager, monkeypatch): example_music_manager.groups[0].track_lists[0].volume = 50 sleep_mock = CoroutineMock() monkeypatch.setattr("src.music.music_manager.asyncio.sleep", sleep_mock) example_music_manager._current_player = MagicMock() example_music_manager._currently_playing = CurrentlyPlaying( 0, 0, MagicMock()) await example_music_manager._set_master_volume(volume=50, smooth=False) sleep_mock.assert_not_awaited() # volume set on the player is the multiplication of the master and individual volume divided by 100 (the max) example_music_manager._current_player.audio_set_volume.assert_called_once_with( 25)
async def test_play_next_track_list_does_nothing_if_next_is_none( self, example_music_manager, monkeypatch): """ The method `_play_next_track_list()` should only look for the next track list, if the `next` attribute of the `current_track_list` is set. """ play_track_list_mock = CoroutineMock() monkeypatch.setattr( "src.music.music_manager.MusicManager.play_track_list", play_track_list_mock) track_list = example_music_manager.groups[0].track_lists[0] track_list.name = "Next Track List" track_list.next = None await example_music_manager._play_next_track_list(None, track_list) play_track_list_mock.assert_not_awaited()
async def test_play_track_list_does_not_start_next_track_list_if_cancelled( self, example_music_manager, monkeypatch): """ If `_play_track_list()` is cancelled for some reason, `_play_next_track_list()` should NOT be called since the request has been cancelled. """ monkeypatch.setattr("src.music.music_manager.MusicManager._play_track", CoroutineMock(side_effect=asyncio.CancelledError)) play_next_track_list_mock = CoroutineMock() monkeypatch.setattr( "src.music.music_manager.MusicManager._play_next_track_list", play_next_track_list_mock) track_list = example_music_manager.groups[0].track_lists[0] track_list.loop = False track_list.next = "Next Track List" with pytest.raises(asyncio.CancelledError): await example_music_manager._play_track_list(request=None, group_index=0, track_list_index=0) play_next_track_list_mock.assert_not_awaited()
async def test_bulk_flushes_on_timeout_even_with_bucket_not_full(self): class MyBucket(Bucket): def pop_all(self): global items items = self._items self._items = [] return items handler_mock = CoroutineMock(__name__="handler") self.one_route_fixture["handler"] = handler_mock self.one_route_fixture["options"]["bulk_size"] = 3 consumer = Consumer( self.one_route_fixture, *self.connection_parameters, bucket_class=MyBucket, ) msgs = [ self._make_msg(delivery_tag=10), self._make_msg(delivery_tag=11), ] await consumer.on_queue_message(msgs[0]) handler_mock.assert_not_awaited() await consumer.on_queue_message(msgs[1]) handler_mock.assert_not_awaited() self.loop.create_task(consumer._flush_clocked()) # Realizando sleep para devolver o loop para o clock await asyncio.sleep(0.1) handler_mock.assert_awaited_once_with(items) msgs[0].ack.assert_awaited_once() msgs[1].ack.assert_awaited_once() msgs[0].reject.assert_not_awaited() msgs[1].reject.assert_not_awaited()
async def test_call_syntaxes(self): _set_limits = CoroutineMock() _show_limits = CoroutineMock() self.patch(RateLimitCmd, _set_limits=_set_limits, _show_limits=_show_limits) await self.execute(RateLimitCmd, 'up', '1Mb', 'limit-rate-up<1k') _set_limits.assert_awaited_once_with(['limit-rate-up<1k'], ('up', ), '1Mb', adjust=False, quiet=False) _show_limits.assert_not_awaited() _set_limits.reset_mock() _show_limits.reset_mock() await self.execute(RateLimitCmd, 'up', '1Mb') _set_limits.assert_awaited_once_with([], ('up', ), '1Mb', adjust=False, quiet=False) _show_limits.assert_not_awaited() _set_limits.reset_mock() _show_limits.reset_mock() await self.execute(RateLimitCmd, 'up') _set_limits.assert_not_awaited() _show_limits.assert_awaited_once_with([], ('up', )) _set_limits.reset_mock() _show_limits.reset_mock() await self.execute(RateLimitCmd, 'up', 'show', 'limit-rate-up<1k') _set_limits.assert_not_awaited() _show_limits.assert_awaited_once_with(['limit-rate-up<1k'], ('up', )) _set_limits.reset_mock() _show_limits.reset_mock()