pubs/tests/test_apis.py
Joe Antognini f3c83668f9 Allow bibtex to be added from arxiv id.
This allows the user to add a reference via an arXiv ID similarly to how a reference can be added
from a DOI or ISBN.  If the arXiv ID has a DOI associated with it (according to the arXiv server),
the DOI will be used.  If it does not (perhaps the paper is unpublished), then a bibtex entry will
automatically be generated from the reference's metadata.

Note that a potential issue with this addition is that if a paper is added before it is published
(i.e., there is no DOI associated with it), and the paper is later published, the updated
information will have to be manually added.
2018-07-23 18:23:36 -06:00

90 lines
2.8 KiB
Python

# coding: utf8
from __future__ import unicode_literals
import unittest
import dotdot
from pubs.p3 import ustr
from pubs.endecoder import EnDecoder
from pubs.apis import arxiv2bibtex, doi2bibtex, isbn2bibtex
class TestDOI2Bibtex(unittest.TestCase):
def setUp(self):
self.endecoder = EnDecoder()
def test_unicode(self):
bib = doi2bibtex('10.1007/BF01700692')
self.assertIsInstance(bib, ustr)
self.assertIn('Kurt Gödel', bib)
def test_parses_to_bibtex(self):
bib = doi2bibtex('10.1007/BF01700692')
b = self.endecoder.decode_bibdata(bib)
self.assertEqual(len(b), 1)
entry = b[list(b)[0]]
self.assertEqual(entry['author'][0], 'Gödel, Kurt')
self.assertEqual(entry['title'],
'Über formal unentscheidbare Sätze der Principia '
'Mathematica und verwandter Systeme I')
def test_parse_fails_on_incorrect_DOI(self):
bib = doi2bibtex('999999')
with self.assertRaises(ValueError):
self.endecoder.decode_bibdata(bib)
class TestISBN2Bibtex(unittest.TestCase):
def setUp(self):
self.endecoder = EnDecoder()
def test_unicode(self):
bib = isbn2bibtex('9782081336742')
self.assertIsInstance(bib, ustr)
self.assertIn('Poincaré, Henri', bib)
def test_parses_to_bibtex(self):
bib = isbn2bibtex('9782081336742')
b = self.endecoder.decode_bibdata(bib)
self.assertEqual(len(b), 1)
entry = b[list(b)[0]]
self.assertEqual(entry['author'][0], 'Poincaré, Henri')
self.assertEqual(entry['title'], 'La science et l\'hypothèse')
def test_parse_fails_on_incorrect_ISBN(self):
bib = doi2bibtex('9' * 13)
with self.assertRaises(ValueError):
self.endecoder.decode_bibdata(bib)
class TestArxiv2Bibtex(unittest.TestCase):
def setUp(self):
self.endecoder = EnDecoder()
def test_parses_to_bibtex_with_doi(self):
bib = arxiv2bibtex('astro-ph/9812133')
b = self.endecoder.decode_bibdata(bib)
self.assertEqual(len(b), 1)
entry = b[list(b)[0]]
self.assertEqual(entry['author'][0], 'Perlmutter, S.')
self.assertEqual(entry['year'], '1999')
def test_parses_to_bibtex_without_doi(self):
bib = arxiv2bibtex('math/0211159')
b = self.endecoder.decode_bibdata(bib)
self.assertEqual(len(b), 1)
entry = b[list(b)[0]]
self.assertEqual(entry['author'][0], 'Perelman, Grisha')
self.assertEqual(entry['year'], '2002')
self.assertEqual(
entry['title'],
'The entropy formula for the Ricci flow and its geometric applications')
# Note: apparently ottobib.com uses caracter modifiers for accents instead
# of the correct unicode characters. TODO: Should we convert them?