Exemplo n.º 1
0
 async def test_play_track_cancels_if_cancelled_error_is_raised_while_playing(
         self, example_music_manager, monkeypatch):
     """
     If a CancelledError is raised while the music is playing, catch it, set the volume to zero and
     re-raise it.
     """
     media_player_mock = MagicMock()
     media_player_mock.is_playing.return_value = True
     media_player_mock.get_time = MagicMock(
         side_effect=asyncio.CancelledError)  # some method in the try block
     set_master_volume_mock = CoroutineMock()
     monkeypatch.setattr("src.music.music_manager.vlc.MediaPlayer",
                         MagicMock(return_value=media_player_mock))
     monkeypatch.setattr("src.music.music_manager.utils.get_track_path",
                         MagicMock(return_value="url"))
     monkeypatch.setattr(
         "src.music.music_manager.MusicManager._wait_for_current_player_to_be_playing",
         CoroutineMock())
     monkeypatch.setattr(
         "src.music.music_manager.MusicManager._set_master_volume",
         set_master_volume_mock)
     monkeypatch.setattr("src.music.music_manager.asyncio.sleep",
                         CoroutineMock())
     group = example_music_manager.groups[0]
     track_list = group.track_lists[0]
     track = track_list.tracks[0]
     track.end_at = 1000
     with pytest.raises(asyncio.CancelledError):
         await example_music_manager._play_track(group=group,
                                                 track_list=track_list,
                                                 track=track)
     media_player_mock.get_time.assert_called_once(
     )  # this raised the CancelledError which got re-raised
     set_master_volume_mock.assert_awaited_with(0, set_global=False)
Exemplo n.º 2
0
 async def test_decrement_entry_count(self, *args):
     mock_id = 'some-value'
     mock_db = MagicMock()
     mock_hincrby = CoroutineMock()
     mock_db.hincrby = mock_hincrby
     await RateLimiter.decrement_entry_count(mock_id, mock_db)
     mock_hincrby.assert_awaited_with(mock_id, 'count', -1)
    async def test_call_rpc_with_kwargs(self):
        method = "method"
        kwargs = {"a": "b"}

        expected = json.dumps({"Action": method, "a": "b"})

        mock_send = CoroutineMock()
        self._client.send = mock_send

        call_task = self._client.call_rpc(method, **kwargs)
        await self._client.recv(expected)
        await call_task

        mock_send.assert_awaited_with(expected)
Exemplo n.º 4
0
 async def test_update_rule(self, *args):
     with patch('pydash.merge') as merge_mock:
         with asynctest.patch.object(RateLimiter,
                                     '_set_indexes') as _set_indexes_mock:
             mock_id = 'some-value'
             mock_ctx = {}
             mock_db = MagicMock()
             mock_hmset_dict = CoroutineMock()
             mock_db.hmset_dict = mock_hmset_dict
             await RateLimiter.update_rule(mock_id, mock_ctx, mock_db)
             expect(merge_mock.call_args[0][0]).to(equal(mock_ctx))
             expect(merge_mock.call_args[0][1]['_id']).to(equal(mock_id))
             _set_indexes_mock.assert_awaited()
             mock_hmset_dict.assert_awaited_with(mock_id, mock_ctx)
Exemplo n.º 5
0
 async def test_delete_entry(self, *args):
     with asynctest.patch.object(RateLimiter,
                                 '_clear_indexes') as _clear_indexes_mock:
         mock_id = 'some-value'
         mock_db = MagicMock()
         mock_delete = CoroutineMock()
         mock_sadd = CoroutineMock()
         mock_db.delete = mock_delete
         mock_db.srem = mock_sadd
         await RateLimiter.delete_entry(mock_id, mock_db)
         mock_delete.assert_awaited_with(mock_id)
         _clear_indexes_mock.assert_awaited_with(mock_id, mock_db)
         mock_sadd.assert_awaited()
         expect(mock_sadd.await_args[0][1]).to(equal(mock_id))
Exemplo n.º 6
0
    async def test_auth_guarded4(self):
        """ Assert no authentication is made if another one is pending
        """
        e = module.aiohttp.ClientResponseError(
                "request_info", "history", status=401)
        f = CoroutineMock(side_effect=(e, None))
        gf = module.auth_guarded(f)
        self.api._auth_free.is_set.return_value = False

        await gf(self.api, "args", kw="kwargs")

        self.api.login.assert_not_called()
        self.assertEqual(self.api._auth_free.wait.await_count, 2)
        self.assertEqual(f.await_count, 2)
        f.assert_awaited_with(self.api, "args", kw="kwargs")
Exemplo n.º 7
0
    async def test_auth_guarded3(self):
        """ Assert the guarded coroutine is awaited when awaiting for the
            guarder
        """
        e = module.aiohttp.ClientResponseError(
                "request_info", "history", status=401)
        f = CoroutineMock(side_effect=(e, None))
        gf = module.auth_guarded(f)

        await gf(self.api, "args", kw="kwargs")

        self.api.login.assert_awaited_once_with()
        self.assertEqual(self.api._auth_free.wait.await_count, 2)
        self.assertEqual(f.await_count, 2)
        f.assert_awaited_with(self.api, "args", kw="kwargs")
Exemplo n.º 8
0
 async def test_set_master_volume_sets_player_volume_in_steps_if_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._currently_playing = CurrentlyPlaying(
         0, 0, MagicMock())
     example_music_manager._current_player = MagicMock()
     example_music_manager._current_player.audio_get_volume.return_value = 0
     n_steps = 10
     seconds = 10
     await example_music_manager._set_master_volume(volume=100,
                                                    smooth=True,
                                                    n_steps=n_steps,
                                                    seconds=seconds)
     sleep_mock.assert_awaited_with(seconds / n_steps)  # seconds / n_steps
     step_size = 50 / n_steps  # 50 = abs(starting_volume(0) - new_volume(100*50//100))
     example_music_manager._current_player.audio_set_volume.assert_has_calls(
         [call(int((i + 1) * step_size))
          for i in range(n_steps)]  # 5, 10, 15, ..., 50
     )
Exemplo n.º 9
0
    async def test_auth_guarded5(self, m_open):
        """ Assert files descritors are reopen if they have been consumed
        """
        e = module.aiohttp.ClientResponseError(
                "request_info", "history", status=401)
        f = CoroutineMock(side_effect=(e, None))
        gf = module.auth_guarded(f)
        self.api._auth_free.is_set.return_value = False
        fd = Mock(spec=module.aiohttp.BufferedReaderPayload, _value=Mock())
        fd._value.name = "fd_name"

        data = Mock(
                spec=module.aiohttp.MultipartWriter,
                _parts=[(fd, 0, 1), (Mock(), 2, 3)])
        data_orig = copy.deepcopy(data)

        await gf(self.api, "args", data=data, kw="kwargs")

        m_open.assert_called_once_with("fd_name", 'rb')
        self.api.login.assert_not_called()
        self.assertEqual(self.api._auth_free.wait.await_count, 2)
        self.assertEqual(f.await_count, 2)
        f.assert_awaited_with(self.api, "args", data=data, kw="kwargs")
        self.assertNotEqual(data._parts, data_orig._parts)