示例#1
0
def test_ambiguous_term_on_wikipedia(mocker):
    mock_requests = mocker.patch('tululbot.utils.leli.requests', autospec=True)

    class FakeResponse:
        def __init__(self, text):
            self.text = text

        def raise_for_status(self):
            pass

    response1_text = ('<html>'
                      '    <div id="mw-content-text">'
                      '        <p>Snowden may refer to:</p>'
                      '        <ul>'
                      '            <li><a href="/wiki/link1">Snowden1</a></li>'
                      '            <li><a href="/wiki/link2">Snowden2</a></li>'
                      '        </ul>'
                      '    </div>'
                      '</html>')
    response1 = FakeResponse(response1_text)

    response2_text = ('<html>'
                      '    <div id="mw-content-text">'
                      '        <p>Snowden is former CIA employee.</p>'
                      '    </div>'
                      '</html>')
    response2 = FakeResponse(response2_text)

    mock_requests.get.side_effect = [response1, response2]

    rv = search_on_wikipedia('snowden')

    assert rv == 'Snowden is former CIA employee.'
示例#2
0
def test_search_on_wikipedia_and_found(mocker):
    mock_requests = mocker.patch('tululbot.utils.leli.requests', autospec=True)

    mock_requests.get.return_value.text = (
        '<html>'
        '    <div id="mw-content-text">'
        '        <p>Tulul is the synonym of cool.</p>'
        '    </div>'
        '</html>')

    rv = search_on_wikipedia('tulul')

    assert rv == 'Tulul is the synonym of cool.'
示例#3
0
def test_search_on_wikipedia_and_found(mocker):
    mock_requests = mocker.patch('tululbot.utils.leli.requests', autospec=True)

    mock_requests.get.return_value.text = (
        '<html>'
        '    <div id="mw-content-text">'
        '        <p>Tulul is the synonym of cool.</p>'
        '    </div>'
        '</html>'
    )

    rv = search_on_wikipedia('tulul')

    assert rv == 'Tulul is the synonym of cool.'
示例#4
0
def leli(message):
    app.logger.debug('Detected leli command {!r}'.format(message.text))
    try:
        if message.text.startswith('/leli'):
            _, term = message.text.split(' ', maxsplit=1)
        else:
            term = message.text
    except ValueError:
        app.logger.debug('Cannot split text {!r}'.format(message.text))
        bot.reply_to(message, 'Apa yang mau dileli?', force_reply=True)
    else:
        app.logger.debug('Extracted leli term {!r}'.format(term))
        try:
            result = search_on_wikipedia(term)
        except HTTPError:
            bot.reply_to(message, 'Aduh ada error nich')
        except ConnectionError:
            bot.reply_to(message, "Koneksi lagi bapuk nih :'(")
        else:
            if result is None:
                result = search_on_google(term)
            bot.reply_to(message, result, disable_web_page_preview=True)
示例#5
0
def leli(message):
    app.logger.debug('Detected leli command {!r}'.format(message.text))
    try:
        if message.text.startswith('/leli'):
            _, term = message.text.split(' ', maxsplit=1)
        else:
            term = message.text
    except ValueError:
        app.logger.debug('Cannot split text {!r}'.format(message.text))
        bot.reply_to(message, 'Apa yang mau dileli?', force_reply=True)
    else:
        app.logger.debug('Extracted leli term {!r}'.format(term))
        try:
            result = search_on_wikipedia(term)
        except HTTPError:
            bot.reply_to(message, 'Aduh ada error nich')
        except ConnectionError:
            bot.reply_to(message, "Koneksi lagi bapuk nih :'(")
        else:
            if result is None:
                result = search_on_google(term)
            bot.reply_to(message, result, disable_web_page_preview=True)
示例#6
0
def test_ambiguous_term_on_wikipedia(mocker):
    mock_requests = mocker.patch('tululbot.utils.leli.requests', autospec=True)

    class FakeResponse:
        def __init__(self, text):
            self.text = text

        def raise_for_status(self):
            pass

    response1_text = (
        '<html>'
        '    <div id="mw-content-text">'
        '        <p>Snowden may refer to:</p>'
        '        <ul>'
        '            <li><a href="/wiki/link1">Snowden1</a></li>'
        '            <li><a href="/wiki/link2">Snowden2</a></li>'
        '        </ul>'
        '    </div>'
        '</html>'
    )
    response1 = FakeResponse(response1_text)

    response2_text = (
        '<html>'
        '    <div id="mw-content-text">'
        '        <p>Snowden is former CIA employee.</p>'
        '    </div>'
        '</html>'
    )
    response2 = FakeResponse(response2_text)

    mock_requests.get.side_effect = [response1, response2]

    rv = search_on_wikipedia('snowden')

    assert rv == 'Snowden is former CIA employee.'