def test_answer_if_parser_succeeds_and_only_wiki_api_with_singled_key_words_which_succeeds(
        monkeypatch):
    def mock_parse_question(quest):
        mock_parse_question.called = True
        return {
            'formated_original_key_words': "fkw",
            'singled_key_words': "skw"
        }

    def mock_try_original_key_words(parsed_question_str):
        mock_try_original_key_words.called = True
        return {"map_resp": True, "wiki_resp": False}

    def mock_try_singled_key_words(key_words_tuple, map_ok=True):
        mock_try_singled_key_words.called = True
        return {"wiki_resp": True}

    monkeypatch.setattr('app.grandpy_bot.parse_question', mock_parse_question)
    monkeypatch.setattr('app.grandpy_bot.try_original_key_words',
                        mock_try_original_key_words)
    monkeypatch.setattr('app.grandpy_bot.try_singled_key_words',
                        mock_try_singled_key_words)

    mock_parse_question.called = False
    mock_try_original_key_words.called = False
    mock_try_singled_key_words.called = False

    assert gpy.answer("whole_question") == {
        "map_resp": True,
        "wiki_resp": True
    }
    assert mock_parse_question.called
    assert mock_try_original_key_words.called
    assert mock_try_singled_key_words.called
def test_answer_if_parser_succeeds_but_only_map_api_succeeds_with_singled_key_words(
        monkeypatch):
    def mock_parse_question(quest):
        mock_parse_question.called = True
        return {
            'formated_original_key_words': "fkw",
            'singled_key_words': "skw"
        }

    def mock_try_original_key_words(parsed_question_str):
        mock_try_original_key_words.called = True
        return {"map_resp": False}

    def mock_try_singled_key_words(key_words_tuple):
        mock_try_singled_key_words.called = True
        return {"map_resp": True, "wiki_resp": False}

    monkeypatch.setattr('app.grandpy_bot.parse_question', mock_parse_question)
    monkeypatch.setattr('app.grandpy_bot.try_original_key_words',
                        mock_try_original_key_words)
    monkeypatch.setattr('app.grandpy_bot.try_singled_key_words',
                        mock_try_singled_key_words)

    mock_parse_question.called = False
    mock_try_original_key_words.called = False
    mock_try_singled_key_words.called = False

    assert gpy.answer("whole_question") == {
        "error":
        "Failing wiki API requesting even last chance try... "
        "map API result is probably not pertinent..."
    }
    assert mock_parse_question.called
    assert mock_try_original_key_words.called
    assert mock_try_singled_key_words.called
def test_answer_if_parser_fails(monkeypatch):
    def mock_parse_question(quest):
        mock_parse_question.called = True
        return None

    monkeypatch.setattr('app.grandpy_bot.parse_question', mock_parse_question)

    mock_parse_question.called = False

    assert gpy.answer("whole_question") == {"error": "Parser failed..."}
    assert mock_parse_question.called
Exemplo n.º 4
0
def ajax():
    text = request.form["question"]
    return (gpy.answer(text))