|
1 | | -"""Tests for `doi` package.""" |
2 | | - |
3 | 1 | import os |
4 | | -from pkg_resources import parse_version |
5 | 2 |
|
6 | 3 | import pytest |
7 | 4 |
|
8 | 5 | from doi import ( |
9 | | - validate_doi, find_doi_in_text, __version__, pdf_to_doi, |
| 6 | + validate_doi, find_doi_in_text, pdf_to_doi, |
10 | 7 | get_real_url_from_doi |
11 | 8 | ) |
12 | 9 |
|
13 | 10 |
|
14 | | -def test_valid_version() -> None: |
15 | | - """Check that the package defines a valid __version__""" |
16 | | - assert parse_version(__version__) >= parse_version("0.1.0") |
17 | | - |
18 | | - |
19 | 11 | @pytest.mark.net |
20 | 12 | def test_validate_doi() -> None: |
21 | 13 | data = [ |
22 | | - ('10.1063/1.5081715', |
23 | | - 'http://aip.scitation.org/doi/10.1063/1.5081715'), |
24 | | - ('10.1007%2FBF01451751', |
25 | | - 'http://link.springer.com/10.1007/BF01451751'), |
26 | | - ('10.1103/PhysRevLett.49.57', |
27 | | - 'https://link.aps.org/doi/10.1103/PhysRevLett.49.57'), |
28 | | - ('10.1080/14786442408634457', |
29 | | - 'https://www.tandfonline.com/doi/full/10.1080/14786442408634457'), |
30 | | - ('10.1021/jp003647e', 'https://pubs.acs.org/doi/10.1021/jp003647e'), |
31 | | - ('10.1016/S0009-2614(97)04014-1', |
32 | | - 'https://linkinghub.elsevier.com/retrieve/pii/S0009261497040141'), |
| 14 | + ("10.1063/1.5081715", |
| 15 | + "http://aip.scitation.org/doi/10.1063/1.5081715"), |
| 16 | + ("10.1007%2FBF01451751", |
| 17 | + "http://link.springer.com/10.1007/BF01451751"), |
| 18 | + ("10.1103/PhysRevLett.49.57", |
| 19 | + "https://link.aps.org/doi/10.1103/PhysRevLett.49.57"), |
| 20 | + ("10.1080/14786442408634457", |
| 21 | + "https://www.tandfonline.com/doi/full/10.1080/14786442408634457"), |
| 22 | + ("10.1021/jp003647e", "https://pubs.acs.org/doi/10.1021/jp003647e"), |
| 23 | + ("10.1016/S0009-2614(97)04014-1", |
| 24 | + "https://linkinghub.elsevier.com/retrieve/pii/S0009261497040141"), |
33 | 25 | ] |
34 | 26 | for doi, url in data: |
35 | 27 | assert url == validate_doi(doi) |
36 | 28 |
|
37 | | - for doi in ['', 'asdf']: |
| 29 | + for doi in ["", "asdf"]: |
38 | 30 | try: |
39 | 31 | validate_doi(doi) |
40 | 32 | except ValueError as e: |
41 | | - assert str(e) == 'HTTP 404: DOI not found' |
| 33 | + assert str(e) == "HTTP 404: DOI not found" |
42 | 34 |
|
43 | 35 |
|
44 | 36 | @pytest.mark.net |
45 | 37 | def test_get_real_url_from_doi() -> None: |
46 | 38 | data = [ |
47 | | - ('10.1016/S0009-2614(97)04014-1', |
48 | | - 'https://www.sciencedirect.com/science/' |
49 | | - 'article/abs/pii/S0009261497040141'), |
| 39 | + ("10.1016/S0009-2614(97)04014-1", |
| 40 | + "https://www.sciencedirect.com/science/" |
| 41 | + "article/abs/pii/S0009261497040141"), |
50 | 42 | ] |
51 | 43 | for doi, url in data: |
52 | 44 | assert url == get_real_url_from_doi(doi) |
53 | 45 |
|
54 | 46 |
|
55 | 47 | def test_find_doi_in_line() -> None: |
56 | 48 | test_data = [ |
57 | | - ('http://dx.doi.org/10.1063/1.881498', '10.1063/1.881498'), |
58 | | - ('http://dx.doi.org/10.1063%2F1.881498', '10.1063/1.881498'), |
59 | | - (2*'qer '+'var doi = "12345/12345.3"', '12345/12345.3'), |
60 | | - (2*'qer '+"var doi = '12345/12345.3';fas", '12345/12345.3'), |
61 | | - (2*'qer '+"var DoI = 12345%2F12345.3", '12345/12345.3'), |
62 | | - (2*'qer '+"var DoI : 12345%2F12345.3", '12345/12345.3'), |
63 | | - ('http://scitation.org/doi/10.1063/1.881498', '10.1063/1.881498'), |
64 | | - ('org/doi(10.1063/1.881498)', '10.1063/1.881498'), |
65 | | - ('/scitation.org/doi/10.1063/1.881498?234saf=34', '10.1063/1.881498'), |
66 | | - ('/scitation.org/doi/10.1063/1.88149 8?234saf=34', '10.1063/1.88149'), |
67 | | - ('/scitation.org/doi/10.1063/1.uniau12?as=234', |
68 | | - '10.1063/1.uniau12'), |
69 | | - ('https://doi.org/10.1093/analys/anw053', '10.1093/analys/anw053'), |
70 | | - ('http://.scitation.org/doi/10.1063/1.mart(88)1498?asdfwer', |
71 | | - '10.1063/1.mart(88)1498'), |
72 | | - ('@ibook{doi:10.1002/9780470125915.ch2,', '10.1002/9780470125915.ch2'), |
| 49 | + ("http://dx.doi.org/10.1063/1.881498", "10.1063/1.881498"), |
| 50 | + ("http://dx.doi.org/10.1063%2F1.881498", "10.1063/1.881498"), |
| 51 | + (2 * "qer " + "var doi = '12345/12345.3'", "12345/12345.3"), |
| 52 | + (2 * "qer " + "var doi = '12345/12345.3';fas", "12345/12345.3"), |
| 53 | + (2 * "qer " + "var DoI = 12345%2F12345.3", "12345/12345.3"), |
| 54 | + (2 * "qer " + "var DoI : 12345%2F12345.3", "12345/12345.3"), |
| 55 | + ("http://scitation.org/doi/10.1063/1.881498", "10.1063/1.881498"), |
| 56 | + ("org/doi(10.1063/1.881498)", "10.1063/1.881498"), |
| 57 | + ("/scitation.org/doi/10.1063/1.881498?234saf=34", "10.1063/1.881498"), |
| 58 | + ("/scitation.org/doi/10.1063/1.88149 8?234saf=34", "10.1063/1.88149"), |
| 59 | + ("/scitation.org/doi/10.1063/1.uniau12?as=234", |
| 60 | + "10.1063/1.uniau12"), |
| 61 | + ("https://doi.org/10.1093/analys/anw053", "10.1093/analys/anw053"), |
| 62 | + ("http://.scitation.org/doi/10.1063/1.mart(88)1498?asdfwer", |
| 63 | + "10.1063/1.mart(88)1498"), |
| 64 | + ("@ibook{doi:10.1002/9780470125915.ch2,", "10.1002/9780470125915.ch2"), |
73 | 65 | ('<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements' |
74 | 66 | '.1/"><dc:format>application/pdf</dc:format><dc:identifier>' |
75 | | - 'doi:10.1063/1.5079474</dc:identifier></rdf:Description>', |
76 | | - '10.1063/1.5079474'), |
77 | | - ('<(DOI:10.1002/9780470915.CH2)/S/URI,', '10.1002/9780470915.CH2'), |
78 | | - ('URL<(DOI:10.1002/9780470125915.CH2,', '10.1002/9780470125915.CH2'), |
79 | | - (r'A<</S/URI/URI(https://doi.org/10.1016/j.comptc.2018.10.004)>>/' |
80 | | - r'Border[0 0 0]/M(D:20181022082356+0530)/Rect[147.40158 594.36926' |
81 | | - r'347.24957 605.36926]/Subtype/Link/Type/A', |
82 | | - '10.1016/j.comptc.2018.10.004'), |
83 | | - ('doi(10.1038/s41535-018-0103-6;)', '10.1038/s41535-018-0103-6'), |
| 67 | + "doi:10.1063/1.5079474</dc:identifier></rdf:Description>", |
| 68 | + "10.1063/1.5079474"), |
| 69 | + ("<(DOI:10.1002/9780470915.CH2)/S/URI,", "10.1002/9780470915.CH2"), |
| 70 | + ("URL<(DOI:10.1002/9780470125915.CH2,", "10.1002/9780470125915.CH2"), |
| 71 | + (r"A<</S/URI/URI(https://doi.org/10.1016/j.comptc.2018.10.004)>>/" |
| 72 | + r"Border[0 0 0]/M(D:20181022082356+0530)/Rect[147.40158 594.36926" |
| 73 | + r"347.24957 605.36926]/Subtype/Link/Type/A", |
| 74 | + "10.1016/j.comptc.2018.10.004"), |
| 75 | + ("doi(10.1038/s41535-018-0103-6;)", "10.1038/s41535-018-0103-6"), |
84 | 76 | ] |
85 | 77 | for url, doi in test_data: |
86 | 78 | assert find_doi_in_text(url) == doi |
87 | 79 |
|
88 | 80 |
|
89 | 81 | def test_doi_from_pdf() -> None: |
90 | | - f = os.path.join(os.path.dirname(__file__), 'resources', 'doc.pdf') |
| 82 | + f = os.path.join(os.path.dirname(__file__), "resources", "doc.pdf") |
91 | 83 |
|
92 | 84 | assert os.path.exists(f) |
93 | | - assert pdf_to_doi(f) == '10.1103/PhysRevLett.50.1998' |
| 85 | + assert pdf_to_doi(f) == "10.1103/PhysRevLett.50.1998" |
0 commit comments