<reponame>thundergolfer/arXie-Bot
from mock import patch
import json

from bot.formatter import build_message, paper_snippet, make_pdf_link

def test_build_message_markdown_false():
    test_text = "This is some text, yay."

    assert test_text == build_message(test_text, markdown=False)


def test_build_message_markdown_true():
    test_markdown = "## Heading \n\n * This is a bullet-point."

    expected_msg = {"text": "## Heading \n\n * This is a bullet-point.", "mrkdwn": True}

    msg = build_message(test_markdown, markdown=True)

    assert expected_msg == json.loads(msg)

def test_make_pdf_link():
    paper_id = 12345
    expected_link = 'http://www.arxiv-sanity.com/pdf/12345.pdf'

    assert expected_link == make_pdf_link(paper_id)


def test_paper_snippet():
    paper = {
        'link': "www.paper_link.com",
        'title': "A Paper Title",
        'authors': ['<NAME>', '<NAME>', '<NAME>'],
        'originally_published_time': '3:30 pm',
        'pid': 123982409
    }

    expected_snippet = {
        'fallback': 'A Paper Title',
        'text': '<www.paper_link.com|1. A Paper Title>\nA. Scott, <NAME>, <NAME> - 3:30 pm\n<http://www.arxiv-sanity.com/pdf/123982409.pdf|PDF>\n'
    }

    assert expected_snippet == paper_snippet(paper, 1)

@patch('bot.formatter.reddit_conversations')
def test_paper_snippet_with_reddit_discussions(mock_reddit_conversations):
    mock_reddit_conversations.return_value = (
        'r/helloworld',
        'www.link.com',
        30
    )
    paper = {
        'link': "www.paper_link.com",
        'title': "A Paper Title",
        'authors': ['<NAME>', '<NAME>', '<NAME>'],
        'originally_published_time': '3:30 pm',
        'pid': 123982409
    }

    expected_snippet = {
        'fallback': 'A Paper Title',
        'text': '<www.paper_link.com|1. A Paper Title>\<NAME>, <NAME>, <NAME> - 3:30 pm\n<http://www.arxiv-sanity.com/pdf/123982409.pdf|PDF>\n<www.link.com|r/helloworld Discussion. 30 comments\n'
    }

    assert expected_snippet == paper_snippet(paper, 1, include_discussions=True)

from mock import patch
import json

from bot.formatter import build_message, paper_snippet, make_pdf_link

def test_build_message_markdown_false():
    test_text = "This is some text, yay."

    assert test_text == build_message(test_text, markdown=False)


def test_build_message_markdown_true():
    test_markdown = "## Heading \n\n * This is a bullet-point."

    expected_msg = {"text": "## Heading \n\n * This is a bullet-point.", "mrkdwn": True}

    msg = build_message(test_markdown, markdown=True)

    assert expected_msg == json.loads(msg)

def test_make_pdf_link():
    paper_id = 12345
    expected_link = 'http://www.arxiv-sanity.com/pdf/12345.pdf'

    assert expected_link == make_pdf_link(paper_id)


def test_paper_snippet():
    paper = {
        'link': "www.paper_link.com",
        'title': "A Paper Title",
        'authors': ['<NAME>', '<NAME>', '<NAME>'],
        'originally_published_time': '3:30 pm',
        'pid': 123982409
    }

    expected_snippet = {
        'fallback': 'A Paper Title',
        'text': '<www.paper_link.com|1. A Paper Title>\nA. Scott, <NAME>, <NAME> - 3:30 pm\n<http://www.arxiv-sanity.com/pdf/123982409.pdf|PDF>\n'
    }

    assert expected_snippet == paper_snippet(paper, 1)

@patch('bot.formatter.reddit_conversations')
def test_paper_snippet_with_reddit_discussions(mock_reddit_conversations):
    mock_reddit_conversations.return_value = (
        'r/helloworld',
        'www.link.com',
        30
    )
    paper = {
        'link': "www.paper_link.com",
        'title': "A Paper Title",
        'authors': ['<NAME>', '<NAME>', '<NAME>'],
        'originally_published_time': '3:30 pm',
        'pid': 123982409
    }

    expected_snippet = {
        'fallback': 'A Paper Title',
        'text': '<www.paper_link.com|1. A Paper Title>\<NAME>, <NAME>, <NAME> - 3:30 pm\n<http://www.arxiv-sanity.com/pdf/123982409.pdf|PDF>\n<www.link.com|r/helloworld Discussion. 30 comments\n'
    }

    assert expected_snippet == paper_snippet(paper, 1, include_discussions=True)
# Heading \n\n * This is a bullet-point." # Heading \n\n * This is a bullet-point.", "mrkdwn": True}