# Path: pubs/p3.py
#     def input():
#     def _get_raw_stdout():
#     def _get_raw_stderr():
#     def u_maybe(s):
#         def __init__(self, *args, **kwargs):
#         def write(self, s):
#     def _get_fake_stdio_ucontent(stdio):
#         def _print_message(self, message, file=None):
#     def _get_raw_stdout():
#     def _get_raw_stderr():
#     def u_maybe(s):
#         def __init__(self, *args, **kwargs):
#         def write(self, s):
#     def _fake_stdio(additional_out=False):
#     def _get_fake_stdio_ucontent(stdio):
# def isbasestr(obj):
#     class StdIO(io.BytesIO):
#     class ArgumentParser(argparse.ArgumentParser):
#     class StdIO(io.BytesIO):
# 
# Path: pubs/apis.py
# class ReferenceNotFoundError(Exception):
# def get_bibentry_from_api(id_str, id_type, try_doi=True, ui=None, raw=False):
# def _get_request(url, headers=None):
# def doi2bibtex(doi, **kwargs):
# def isbn2bibtex(isbn, **kwargs):
# def _is_arxiv_oldstyle(arxiv_id):
# def _extract_arxiv_id(entry):
# def arxiv2bibtex(arxiv_id, try_doi=True, ui=None):
# 
# Path: pubs/apis.py
# def _is_arxiv_oldstyle(arxiv_id):
#     return re.match(r"(arxiv\:)?[a-z\-]+\/[0-9]+(v[0-9]+)?", arxiv_id.lower()) is not None
# 
# def _extract_arxiv_id(entry):
#     pattern = r"http[s]?://arxiv.org/abs/(?P<entry_id>.+)"
#     return re.search(pattern, entry['id']).groupdict()['entry_id']


import unittest
import mock
import dotdot
import mock_requests
from pubs.p3 import ustr
from pubs import apis
from pubs.apis import _is_arxiv_oldstyle, _extract_arxiv_id
        self.assertTrue(not 'arxiv_doi' in entry)
        self.assertEqual(entry['doi'], '10.1186/s12984-017-0305-3')
        self.assertEqual(entry['title'].lower(), 'on neuromechanical approaches for the study of biological and robotic grasp and manipulation')

    @mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
    def test_arxiv_good_doi_force_arxiv(self, reqget):
        bib = apis.get_bibentry_from_api('1710.08557', 'arXiv', try_doi=False)
        entry = bib[list(bib)[0]]
        self.assertEqual(entry['arxiv_doi'], '10.1186/s12984-017-0305-3')
        self.assertEqual(entry['title'].lower(), 'on neuromechanical approaches for the study of biological grasp and\nmanipulation')


class TestArxiv2BibtexLocal(unittest.TestCase):
    """Test arXiv 2 Bibtex connection; those tests don't require a connection"""

    def test_oldstyle_pattern(self):
        """Test that we can accurately differentiate between old and new style arXiv ids."""
        # old-style arXiv ids
        for arxiv_id in ['cs/9301113', 'math/9201277v3', 'astro-ph/9812133',
                         'cond-mat/0604612', 'hep-ph/0702007v10', 'arXiv:physics/9403001'
                        ]:
            self.assertTrue(_is_arxiv_oldstyle(arxiv_id))
        # new-style arXiv ids
        for arxiv_id in ['1808.00954', 'arXiv:1808.00953', '1808.0953',
                         '1808.00954v1', 'arXiv:1808.00953v2', '1808.0953v42']:
            self.assertFalse(_is_arxiv_oldstyle(arxiv_id))

    def test_extract_id(self):
        """Test that ids are correctly extracted"""
        self.assertEqual(_extract_arxiv_id({'id': "http://arxiv.org/abs/0704.0010v1"}), "0704.0010v1")

# Path: pubs/p3.py
#     def input():
#     def _get_raw_stdout():
#     def _get_raw_stderr():
#     def u_maybe(s):
#         def __init__(self, *args, **kwargs):
#         def write(self, s):
#     def _get_fake_stdio_ucontent(stdio):
#         def _print_message(self, message, file=None):
#     def _get_raw_stdout():
#     def _get_raw_stderr():
#     def u_maybe(s):
#         def __init__(self, *args, **kwargs):
#         def write(self, s):
#     def _fake_stdio(additional_out=False):
#     def _get_fake_stdio_ucontent(stdio):
# def isbasestr(obj):
#     class StdIO(io.BytesIO):
#     class ArgumentParser(argparse.ArgumentParser):
#     class StdIO(io.BytesIO):
# 
# Path: pubs/apis.py
# class ReferenceNotFoundError(Exception):
# def get_bibentry_from_api(id_str, id_type, try_doi=True, ui=None, raw=False):
# def _get_request(url, headers=None):
# def doi2bibtex(doi, **kwargs):
# def isbn2bibtex(isbn, **kwargs):
# def _is_arxiv_oldstyle(arxiv_id):
# def _extract_arxiv_id(entry):
# def arxiv2bibtex(arxiv_id, try_doi=True, ui=None):
# 
# Path: pubs/apis.py
# def _is_arxiv_oldstyle(arxiv_id):
#     return re.match(r"(arxiv\:)?[a-z\-]+\/[0-9]+(v[0-9]+)?", arxiv_id.lower()) is not None
# 
# def _extract_arxiv_id(entry):
#     pattern = r"http[s]?://arxiv.org/abs/(?P<entry_id>.+)"
#     return re.search(pattern, entry['id']).groupdict()['entry_id']


# Path: tests/test_apis.py
import unittest
import mock
import dotdot
import mock_requests
from pubs.p3 import ustr
from pubs import apis
from pubs.apis import _is_arxiv_oldstyle, _extract_arxiv_id

        self.assertTrue(not 'arxiv_doi' in entry)
        self.assertEqual(entry['doi'], '10.1186/s12984-017-0305-3')
        self.assertEqual(entry['title'].lower(), 'on neuromechanical approaches for the study of biological and robotic grasp and manipulation')

    @mock.patch('pubs.apis.requests.get', side_effect=mock_requests.mock_requests_get)
    def test_arxiv_good_doi_force_arxiv(self, reqget):
        bib = apis.get_bibentry_from_api('1710.08557', 'arXiv', try_doi=False)
        entry = bib[list(bib)[0]]
        self.assertEqual(entry['arxiv_doi'], '10.1186/s12984-017-0305-3')
        self.assertEqual(entry['title'].lower(), 'on neuromechanical approaches for the study of biological grasp and\nmanipulation')


class TestArxiv2BibtexLocal(unittest.TestCase):
    """Test arXiv 2 Bibtex connection; those tests don't require a connection"""

    def test_oldstyle_pattern(self):
        """Test that we can accurately differentiate between old and new style arXiv ids."""
        # old-style arXiv ids
        for arxiv_id in ['cs/9301113', 'math/9201277v3', 'astro-ph/9812133',
                         'cond-mat/0604612', 'hep-ph/0702007v10', 'arXiv:physics/9403001'
                        ]:
            self.assertTrue(_is_arxiv_oldstyle(arxiv_id))
        # new-style arXiv ids
        for arxiv_id in ['1808.00954', 'arXiv:1808.00953', '1808.0953',
                         '1808.00954v1', 'arXiv:1808.00953v2', '1808.0953v42']:
            self.assertFalse(_is_arxiv_oldstyle(arxiv_id))

    def test_extract_id(self):
        """Test that ids are correctly extracted"""
        self.assertEqual(_extract_arxiv_id({'id': "http://arxiv.org/abs/0704.0010v1"}), "0704.0010v1")

        self.assertEqual(_extract_arxiv_id({'id': "https://arxiv.org/abs/0704.0010v1"}), "0704.0010v1")