Пример #1
0
def test__get_question_top_answer_body__answers():
    Answer = namedtuple('Answer', ['id', 'score'])
    answers = [Answer(1, 1), Answer(2, 2)]

    so = Mock()
    fetched_answer = Mock()
    fetched_answer.body = "foobar"
    so.answer.return_value = fetched_answer
    question = Mock()
    question.answers = answers
    assert get_question_top_answer_body(so, question) == "foobar"
Пример #2
0
def soi():
    '''
    Displays Slack-formatted most-upvoted answer of the most upvoted
    question from the query.

    soi ~ StackOverflow-Inline

    Example:
        /soi python list comprehension
    '''
    # Get objects on the application context pushed on startup
    so = flask._app_ctx_stack.so

    text = request.values.get('text')

    # Perform google search
    sr = google_search_stackoverflow_query(text, pages=1)
    # Extract each question nid from results
    so_qnids = []
    for result in sr:
        qnid = extract_question_nid_from_url(result.link)
        if qnid is not None:
            so_qnids.append(qnid)
        else:
            logging.warning("Found invalid URL in search: "
                            "{}".format(result.link))
    if not so_qnids:
        return "*No questions for the given query were found! :(*"
    # Fetch Questions using SO API
    so_qs = so.questions(so_qnids)
    # Sort Questions by score
    so_qs = sorted(so_qs, key=lambda q: q.score, reverse=True)

    top_question = so_qs[0]
    answer_body = get_question_top_answer_body(so=so, question=top_question)
    question_str = get_response_string(top_question)

    resp_list = [question_str]
    if answer_body is None:
        resp_list.append("\n\n*The top question for your query is unanswered! "
                         ":(*")
    else:
        resp_list.append("\n\n")
        resp_list.append(html2slack(answer_body))

    return Response("".join(resp_list),
                    content_type='text/plain; charset=utf-8')
Пример #3
0
def test__get_question_top_answer_body__no_answers():
    so = Mock()
    question = Mock()
    question.answers = []
    assert get_question_top_answer_body(so, question) is None