Ejemplo n.º 1
0
    def test_str(self, mocker):
        mocked_uuid = mocker.Mock()
        mocked_uuid.uuid4().hex = 'some-id'
        with mocker.mock_module.patch('telegram.utils.uuid', mocked_uuid):
            ar = AsyncResult(client=None)

        assert ar.__str__() == f'AsyncResult <some-id>'
Ejemplo n.º 2
0
    def _send_data(
        self,
        data: Dict[Any, Any],
        result_id: Optional[str] = None,
        block: bool = False,
    ) -> AsyncResult:
        """
        Sends data to tdlib.

        If `block`is True, waits for the result
        """
        if '@extra' not in data:
            data['@extra'] = {}

        if not result_id and 'request_id' in data['@extra']:
            result_id = data['@extra']['request_id']

        async_result = AsyncResult(client=self, result_id=result_id)
        data['@extra']['request_id'] = async_result.id
        self._results[async_result.id] = async_result
        self._tdjson.send(data)
        async_result.request = data

        if block:
            async_result.wait(raise_exc=True)

        return async_result
Ejemplo n.º 3
0
    def test_str(self):
        mocked_uuid = Mock()
        mocked_uuid.uuid4().hex = 'some-id'
        with patch('telegram.utils.uuid', mocked_uuid):
            async_result = AsyncResult(client=None)

        assert async_result.__str__() == f'AsyncResult <some-id>'
Ejemplo n.º 4
0
        def _get_async_result(data, request_id=None):
            result = AsyncResult(client=telegram)

            result.update = data
            result.id = request_id

            return result
Ejemplo n.º 5
0
    def test_wait_with_timeout(self):
        ar = AsyncResult(client=None)
        try:
            ar.wait(timeout=0.01)
            raised = False
        except TimeoutError:
            raised = True

        assert raised is True
Ejemplo n.º 6
0
    def test_parse_update(self):
        async_result = AsyncResult(client=None)
        update = {'@type': 'some_type', 'some': 'data'}

        async_result.parse_update(update)

        assert async_result.error is False
        assert async_result.error_info is None
        assert async_result.update == update
        assert async_result.ok_received is False
Ejemplo n.º 7
0
    def test_parse_update_ok(self):
        ar = AsyncResult(client=None)
        update = {'@type': 'ok', 'some': 'data'}

        ar.parse_update(update)

        assert ar.error is False
        assert ar.error_info is None
        assert ar.update is None
        assert ar.ok_received is True
Ejemplo n.º 8
0
    def test_parse_update_ok(self):
        async_result = AsyncResult(client=None)
        update = {'@type': 'ok', 'some': 'data'}

        async_result.parse_update(update)

        assert async_result.error is False
        assert async_result.error_info is None
        assert async_result.update is None
        assert async_result.ok_received is True
        assert async_result._ready.is_set() is True
Ejemplo n.º 9
0
    def _wait_authorization_result(self, result: AsyncResult) -> AuthorizationState:
        authorization_state = None
        if result:
            result.wait(raise_exc=True)

            if result.update is None:
                raise RuntimeError('Something wrong, the result update is None')

            if result.id == 'getAuthorizationState':
                authorization_state = result.update['@type']
            else:
                authorization_state = result.update['authorization_state']['@type']

        return AuthorizationState(authorization_state)
Ejemplo n.º 10
0
    def _send_data(self, data: dict, result_id: str = None) -> AsyncResult:
        if '@extra' not in data:
            data['@extra'] = {}

        if not result_id and 'request_id' in data['@extra']:
            result_id = data['@extra']['request_id']

        async_result = AsyncResult(client=self, result_id=result_id)
        data['@extra']['request_id'] = async_result.id

        self._tdjson.send(data)
        self._results[async_result.id] = async_result
        async_result.request = data

        return async_result
Ejemplo n.º 11
0
    def test_parse_update_with_error(self):
        ar = AsyncResult(client=None)
        update = {
            '@type': 'error',
            'some': 'data',
        }

        assert ar.error is False
        assert ar.error_info is None

        ar.parse_update(update)

        assert ar.error is True
        assert ar.error_info == update
        assert ar.update is None
        assert ar.ok_received is False
Ejemplo n.º 12
0
    def test_initial_params(self, mocker):
        mocked_uuid = mocker.Mock()
        mocked_uuid.uuid4().hex = 'some-id'
        with mocker.mock_module.patch('telegram.utils.uuid', mocked_uuid):
            ar = AsyncResult(client='123')

        assert ar.client == '123'
        assert ar.id == 'some-id'
Ejemplo n.º 13
0
    def test_initial_params(self):
        mocked_uuid = Mock()
        mocked_uuid.uuid4().hex = 'some-id'
        with patch('telegram.utils.uuid', mocked_uuid):
            async_result = AsyncResult(client='123')

        assert async_result.client == '123'
        assert async_result.id == 'some-id'
Ejemplo n.º 14
0
    def test_parse_update_authorization_state_ok(self):
        # when id=updateAuthorizationState
        # and @type=ok
        # it should not set async_result._ready
        # because for updateAuthorizationState we want to wait for the
        # next message with result_id=updateAuthorizationState
        async_result = AsyncResult(
            client=None,
            result_id='updateAuthorizationState',
        )
        update = {'@type': 'ok', 'some': 'data'}

        async_result.parse_update(update)

        assert async_result.error is False
        assert async_result.error_info is None
        assert async_result.update is None
        assert async_result.ok_received is True
        assert async_result._ready.is_set() is False
    def test_result_id_should_be_replaced_if_it_is_auth_process(self, telegram):
        async_result = AsyncResult(client=telegram, result_id='updateAuthorizationState')
        telegram._results['updateAuthorizationState'] = async_result

        update = {
            '@type': 'updateAuthorizationState',
            '@extra': {
                'request_id': 'blablabla',
            }
        }
        new_async_result = telegram._update_async_result(update=update)

        assert new_async_result.id == 'updateAuthorizationState'
Ejemplo n.º 16
0
    def test_wait_with_error_and_raise_exc(self):
        async_result = AsyncResult(client=None)
        async_result.error = True
        async_result.error_info = 'some_error'
        async_result._ready.set()

        with pytest.raises(RuntimeError):
            async_result.wait(timeout=0.1, raise_exc=True)
Ejemplo n.º 17
0
    def test_wait_with_error_and_raise_exc(self):
        ar = AsyncResult(client=None)
        ar.error = True
        ar.error_info = 'some_error'
        try:
            ar.wait(timeout=0.1, raise_exc=True)
            raised = False
        except RuntimeError:
            raised = True

        assert raised is True
Ejemplo n.º 18
0
 def test_wait_with_error_and_without_raise_exc(self):
     ar = AsyncResult(client=None)
     ar.error = True
     ar.error_info = 'some_error'
     ar.wait(timeout=0.01)
Ejemplo n.º 19
0
 def test_wait_with_update(self):
     ar = AsyncResult(client=None)
     ar.update = '123'
     ar.wait(timeout=0.01)
Ejemplo n.º 20
0
 def test_wait_with_error_and_without_raise_exc(self):
     async_result = AsyncResult(client=None)
     async_result.error = True
     async_result.error_info = 'some_error'
     async_result._ready.set()
     async_result.wait(timeout=0.01)
        def _get_ar(data):
            ar = AsyncResult(client=telegram)

            ar.update = data

            return ar
Ejemplo n.º 22
0
    def test_wait_with_timeout(self):
        async_result = AsyncResult(client=None)

        with pytest.raises(TimeoutError):
            async_result.wait(timeout=0.01)
Ejemplo n.º 23
0
 def test_wait_with_update(self):
     async_result = AsyncResult(client=None)
     async_result.update = '123'
     async_result._ready.set()
     async_result.wait(timeout=0.01)