# Path: cliche/services/tvtropes/crawler.py
# def fetch_link(url, session, *, log_prefix=''):
#     '''Returns result, tree, namespace, name, final_url.'''
#     logger = get_task_logger(__name__ + '.fetch_link')
#     if not is_wiki_page(url):
#         return False, None, None, None, url
#     r = requests.get(url)
#     try:
#         final_url = r.url[:r.url.index('?')]
#     except ValueError:
#         final_url = r.url
#     if not is_wiki_page(final_url):
#         return False, None, None, None, final_url
#     tree = document_fromstring(r.text)
#     try:
#         name = tree.find_class('article_title')[0].text_content()
#     except (AttributeError, AssertionError, IndexError):
#         logger.warning('%sWarning on url %s: '
#                        'There is no pagetitle on this page. Ignoring.',
#                        log_prefix, url)
#         return False, tree, None, None, final_url
#     else:
#         *namespace, name = name.split(':')
#         name = name.strip()
#         namespace = 'Main' if not namespace else namespace[0]
#         type = determine_type(namespace)
#         if type == 'Administrivia':
#             return False, tree, namespace, name, final_url
#         upsert_entity(session, namespace, name, type, final_url)
#         process_redirections(session, url, final_url, namespace, name)
#         return True, tree, namespace, name, final_url


import requests
from cliche.services.tvtropes.crawler import fetch_link



def test_fetch_link(monkeypatch, fx_session, fx_celery_app):

    url = 'http://tvtropes.org/pmwiki/pmwiki.php/Main/GodJob'
    text = '<div class="pagetitle"><div class="article_title"><h1>' \
           '<span>God Job</span></h1></div></div>'

    def mockreturn(path):
        req = requests.Request()
        req.url = url
        req.text = text
        return req

    monkeypatch.setattr(requests, "get", mockreturn)


# Path: cliche/services/tvtropes/crawler.py
# def fetch_link(url, session, *, log_prefix=''):
#     '''Returns result, tree, namespace, name, final_url.'''
#     logger = get_task_logger(__name__ + '.fetch_link')
#     if not is_wiki_page(url):
#         return False, None, None, None, url
#     r = requests.get(url)
#     try:
#         final_url = r.url[:r.url.index('?')]
#     except ValueError:
#         final_url = r.url
#     if not is_wiki_page(final_url):
#         return False, None, None, None, final_url
#     tree = document_fromstring(r.text)
#     try:
#         name = tree.find_class('article_title')[0].text_content()
#     except (AttributeError, AssertionError, IndexError):
#         logger.warning('%sWarning on url %s: '
#                        'There is no pagetitle on this page. Ignoring.',
#                        log_prefix, url)
#         return False, tree, None, None, final_url
#     else:
#         *namespace, name = name.split(':')
#         name = name.strip()
#         namespace = 'Main' if not namespace else namespace[0]
#         type = determine_type(namespace)
#         if type == 'Administrivia':
#             return False, tree, namespace, name, final_url
#         upsert_entity(session, namespace, name, type, final_url)
#         process_redirections(session, url, final_url, namespace, name)
#         return True, tree, namespace, name, final_url


# Path: tests/tvtropes_crawler_test.py
import requests
from cliche.services.tvtropes.crawler import fetch_link




def test_fetch_link(monkeypatch, fx_session, fx_celery_app):

    url = 'http://tvtropes.org/pmwiki/pmwiki.php/Main/GodJob'
    text = '<div class="pagetitle"><div class="article_title"><h1>' \
           '<span>God Job</span></h1></div></div>'

    def mockreturn(path):
        req = requests.Request()
        req.url = url
        req.text = text
        return req

    monkeypatch.setattr(requests, "get", mockreturn)


    result = fetch_link(url, fx_session)