# Path: pubs/utils.py
# def standardize_doi(doi):
#     """
#     Given a putative doi, attempts to always return it in the form of
#     10.XXXX/...  Specifically designed to handle these cases:
#     -   https://doi.org/<doi>
#     -   http://doi.org/<doi>
#     -   https://dx.doi.org/<doi>
#     -   http://dx.doi.org/<doi>
#     -   dx.doi.org/<doi>
#     -   doi.org/<doi>
#     and attempts to verify doi adherence to DOI handbook standards and
#     crossref.org advice:
#     https://www.doi.org/doi_handbook/2_Numbering.html
#     https://www.crossref.org/blog/dois-and-matching-regular-expressions/
# 
#         :returns standardized doi
#     """
# 
#     doi_regexes = (
#         r'(10\.\d{4,9}/[-._;()/:A-z0-9\>\<]+)',
#         r'(10.1002/[^\s]+)',
#         r'(10\.\d{4}/\d+-\d+X?(\d+)\d+<[\d\w]+:[\d\w]*>\d+.\d+.\w+;\d)',
#         r'(10\.1021/\w\w\d+\+)',
#         r'(10\.1207/[\w\d]+\&\d+_\d+)')
#     doi_pattern = re.compile('|'.join(doi_regexes))
# 
#     match = doi_pattern.search(doi)
#     if not match:
#         raise ValueError("Not a valid doi: {}".format(doi))
#     new_doi = match.group(0)
# 
#     return new_doi


import unittest
from pubs.utils import standardize_doi
class TestDOIStandardization(unittest.TestCase):

    def setUp(self):
        # some of these come from
        # https://stackoverflow.com/questions/27910/finding-a-doi-in-a-document-or-page
        self.crossref_dois = (
            '10.2310/JIM.0b013e31820bab4c',
            '10.1007/978-3-642-28108-2_19',
            '10.1016/S0735-1097(98)00347-7',
        )

        self.hard_dois = (
            '10.1175/1520-0485(2002)032<0870:CT>2.0.CO;2',
            '10.1002/(SICI)1522-2594(199911)42:5<952::AID-MRM16>3.0.CO;2-S',
            '10.1579/0044-7447(2006)35\[89:RDUICP\]2.0.CO;2',
        )

        self.currently_not_supported = (
            '10.1007.10/978-3-642-28108-2_19',
            '10.1000.10/123456',
            '10.1016.12.31/nature.S0735-1097(98)2000/12/31/34:7-7',
        )

    def test_http_dxdoi_org(self):
        doi = 'http://dx.doi.org/10.1109/5.771073'
        sdoi = standardize_doi(doi)
        self.assertEqual(sdoi, '10.1109/5.771073')

    def test_https_dxdoi_org(self):
        doi = 'https://dx.doi.org/10.1109/5.771073'

# Path: pubs/utils.py
# def standardize_doi(doi):
#     """
#     Given a putative doi, attempts to always return it in the form of
#     10.XXXX/...  Specifically designed to handle these cases:
#     -   https://doi.org/<doi>
#     -   http://doi.org/<doi>
#     -   https://dx.doi.org/<doi>
#     -   http://dx.doi.org/<doi>
#     -   dx.doi.org/<doi>
#     -   doi.org/<doi>
#     and attempts to verify doi adherence to DOI handbook standards and
#     crossref.org advice:
#     https://www.doi.org/doi_handbook/2_Numbering.html
#     https://www.crossref.org/blog/dois-and-matching-regular-expressions/
# 
#         :returns standardized doi
#     """
# 
#     doi_regexes = (
#         r'(10\.\d{4,9}/[-._;()/:A-z0-9\>\<]+)',
#         r'(10.1002/[^\s]+)',
#         r'(10\.\d{4}/\d+-\d+X?(\d+)\d+<[\d\w]+:[\d\w]*>\d+.\d+.\w+;\d)',
#         r'(10\.1021/\w\w\d+\+)',
#         r'(10\.1207/[\w\d]+\&\d+_\d+)')
#     doi_pattern = re.compile('|'.join(doi_regexes))
# 
#     match = doi_pattern.search(doi)
#     if not match:
#         raise ValueError("Not a valid doi: {}".format(doi))
#     new_doi = match.group(0)
# 
#     return new_doi


# Path: tests/test_doi.py
import unittest
from pubs.utils import standardize_doi

class TestDOIStandardization(unittest.TestCase):

    def setUp(self):
        # some of these come from
        # https://stackoverflow.com/questions/27910/finding-a-doi-in-a-document-or-page
        self.crossref_dois = (
            '10.2310/JIM.0b013e31820bab4c',
            '10.1007/978-3-642-28108-2_19',
            '10.1016/S0735-1097(98)00347-7',
        )

        self.hard_dois = (
            '10.1175/1520-0485(2002)032<0870:CT>2.0.CO;2',
            '10.1002/(SICI)1522-2594(199911)42:5<952::AID-MRM16>3.0.CO;2-S',
            '10.1579/0044-7447(2006)35\[89:RDUICP\]2.0.CO;2',
        )

        self.currently_not_supported = (
            '10.1007.10/978-3-642-28108-2_19',
            '10.1000.10/123456',
            '10.1016.12.31/nature.S0735-1097(98)2000/12/31/34:7-7',
        )

    def test_http_dxdoi_org(self):
        doi = 'http://dx.doi.org/10.1109/5.771073'
        sdoi = standardize_doi(doi)
        self.assertEqual(sdoi, '10.1109/5.771073')

    def test_https_dxdoi_org(self):
        doi = 'https://dx.doi.org/10.1109/5.771073'

        sdoi = standardize_doi(doi)