Exemplo n.º 1
0
    def make_assertion(_, data, timeout=DEFAULT_NONE, df_value=DEFAULT_NONE):
        expected_timeout = method_timeout if df_value == DEFAULT_NONE else df_value
        if timeout != expected_timeout:
            pytest.fail(
                f'Got value {timeout} for "timeout", expected {expected_timeout}'
            )

        for arg in (dkw for dkw in kwargs_need_default if dkw != 'timeout'):
            # 'None' should not be passed along to Telegram
            if df_value in [None, DEFAULT_NONE]:
                if arg in data:
                    pytest.fail(
                        f'Got value {data[arg]} for argument {arg}, expected it to be absent'
                    )
            else:
                value = data.get(arg, '`not passed at all`')
                if value != df_value:
                    pytest.fail(
                        f'Got value {value} for argument {arg} instead of {df_value}'
                    )

        if method.__name__ in ['get_file', 'get_small_file', 'get_big_file']:
            # This is here mainly for PassportFile.get_file, which calls .set_credentials on the
            # return value
            out = File(file_id='result', file_unique_id='result')
            nonlocal expected_return_values
            expected_return_values = [out]
            return out.to_dict()
        # Otherwise return None by default, as TGObject.de_json/list(None) in [None, []]
        # That way we can check what gets passed to Request.post without having to actually
        # make a request
        # Some methods expect specific output, so we allow to customize that
        return return_value
    async def make_assertion(url,
                             request_data: RequestData,
                             df_value=DEFAULT_NONE,
                             *args,
                             **kwargs):
        data = request_data.parameters

        # Check regular arguments that need defaults
        for arg in (dkw for dkw in kwargs_need_default if dkw != "timeout"):
            # 'None' should not be passed along to Telegram
            if df_value in [None, DEFAULT_NONE]:
                if arg in data:
                    pytest.fail(
                        f"Got value {data[arg]} for argument {arg}, expected it to be absent"
                    )
            else:
                value = data.get(arg, "`not passed at all`")
                if value != df_value:
                    pytest.fail(
                        f"Got value {value} for argument {arg} instead of {df_value}"
                    )

        # Check InputMedia (parse_mode can have a default)
        def check_input_media(m: Dict):
            parse_mode = m.get("parse_mode", None)
            if df_value is DEFAULT_NONE:
                if parse_mode is not None:
                    pytest.fail("InputMedia has non-None parse_mode")
            elif parse_mode != df_value:
                pytest.fail(
                    f"Got value {parse_mode} for InputMedia.parse_mode instead of {df_value}"
                )

        media = data.pop("media", None)
        if media:
            if isinstance(media, dict) and isinstance(media.get("type", None),
                                                      InputMediaType):
                check_input_media(media)
            else:
                for m in media:
                    check_input_media(m)

        # Check InlineQueryResults
        results = data.pop("results", [])
        for result in results:
            if df_value in [DEFAULT_NONE, None]:
                if "parse_mode" in result:
                    pytest.fail(
                        "ILQR has a parse mode, expected it to be absent")
            # Here we explicitly use that we only pass ILQRPhoto and ILQRArticle for testing
            # so ILQRPhoto is expected to have parse_mode if df_value is not in [DF_NONE, NONE]
            elif "photo" in result and result.get("parse_mode") != df_value:
                pytest.fail(f'Got value {result.get("parse_mode")} for '
                            f"ILQR.parse_mode instead of {df_value}")
            imc = result.get("input_message_content")
            if not imc:
                continue
            for attr in ["parse_mode", "disable_web_page_preview"]:
                if df_value in [DEFAULT_NONE, None]:
                    if attr in imc:
                        pytest.fail(
                            f"ILQR.i_m_c has a {attr}, expected it to be absent"
                        )
                # Here we explicitly use that we only pass InputTextMessageContent for testing
                # which has both attributes
                elif imc.get(attr) != df_value:
                    pytest.fail(
                        f"Got value {imc.get(attr)} for ILQR.i_m_c.{attr} instead of {df_value}"
                    )

        # Check datetime conversion
        until_date = data.pop("until_date", None)
        if until_date:
            if df_value == "non-None-value":
                if until_date != 946681200:
                    pytest.fail(
                        "Non-naive until_date was interpreted as Europe/Berlin."
                    )
            if df_value is DEFAULT_NONE:
                if until_date != 946684800:
                    pytest.fail("Naive until_date was not interpreted as UTC")
            if df_value == "custom_default":
                if until_date != 946702800:
                    pytest.fail(
                        "Naive until_date was not interpreted as America/New_York"
                    )

        if method.__name__ in ["get_file", "get_small_file", "get_big_file"]:
            # This is here mainly for PassportFile.get_file, which calls .set_credentials on the
            # return value
            out = File(file_id="result", file_unique_id="result")
            nonlocal expected_return_values
            expected_return_values = [out]
            return out.to_dict()
        # Otherwise return None by default, as TGObject.de_json/list(None) in [None, []]
        # That way we can check what gets passed to Request.post without having to actually
        # make a request
        # Some methods expect specific output, so we allow to customize that
        return return_value