From 479aea4101826c02f726ae0958e3f76632993ae2 Mon Sep 17 00:00:00 2001 From: Phyks Date: Mon, 26 May 2014 16:50:58 +0200 Subject: [PATCH] Some unit test --- .gitignore | 3 ++ bmc.py | 2 +- fetcher.py | 5 ++- tests/test_backend.py | 10 ++++++ tests/test_fetcher.py | 83 +++++++++++++++++++++++++++++++++++++++++++ tests/test_tools.py | 36 +++++++++++++++++++ 6 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 tests/test_backend.py create mode 100644 tests/test_fetcher.py create mode 100644 tests/test_tools.py diff --git a/.gitignore b/.gitignore index 01afaaf..aef9d69 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ params.py translation-server +*.pdf +*.bib +*.djvu diff --git a/bmc.py b/bmc.py index db081c7..0ab2a05 100755 --- a/bmc.py +++ b/bmc.py @@ -94,7 +94,7 @@ def addFile(src, filetype, manual, autoconfirm, tag): arxiv = fetcher.findArXivId(src) if filetype == 'book' or (doi is False and arxiv is False and - filetype is None): + filetype is None): isbn = fetcher.findISBN(src) except KeyboardInterrupt: doi = False diff --git a/fetcher.py b/fetcher.py index 402e5b4..741a1c9 100644 --- a/fetcher.py +++ b/fetcher.py @@ -57,6 +57,9 @@ def download(url): continue return dl, contenttype + except ValueError: + tools.warning("Invalid URL") + return False except requests.exceptions.RequestException: tools.warning("Unable to get "+url+" using proxy "+proxy+". It " + "may not be available.") @@ -64,10 +67,10 @@ def download(url): return False -#isbn_re = re.compile(r"isbn (([0-9]{3}[ -])?[0-9][ -][0-9]{2}[ -][0-9]{6}[ -][0-9])", isbn_re = re.compile(r'isbn ((?:[0-9]{3}[ -]?)?[0-9]{1,5}[ -]?[0-9]{1,7}[ -]?[0-9]{1,6}[- ]?[0-9])', re.IGNORECASE) + def findISBN(src): """Search for a valid ISBN in src. diff --git a/tests/test_backend.py b/tests/test_backend.py new file mode 100644 index 0000000..0415fdf --- /dev/null +++ b/tests/test_backend.py @@ -0,0 +1,10 @@ +# -*- coding: utf8 -*- +# ----------------------------------------------------------------------------- +# "THE NO-ALCOHOL BEER-WARE LICENSE" (Revision 42): +# Phyks (webmaster@phyks.me) wrote this file. As long as you retain this notice +# you can do whatever you want with this stuff (and you can also do whatever +# you want with this stuff without retaining it, but that's not cool...). If we +# meet some day, and you think this stuff is worth it, you can buy me a +# beer soda in return. +# Phyks +# ----------------------------------------------------------------------------- diff --git a/tests/test_fetcher.py b/tests/test_fetcher.py new file mode 100644 index 0000000..77cfce0 --- /dev/null +++ b/tests/test_fetcher.py @@ -0,0 +1,83 @@ +# -*- coding: utf8 -*- +# ----------------------------------------------------------------------------- +# "THE NO-ALCOHOL BEER-WARE LICENSE" (Revision 42): +# Phyks (webmaster@phyks.me) wrote this file. As long as you retain this notice +# you can do whatever you want with this stuff (and you can also do whatever +# you want with this stuff without retaining it, but that's not cool...). If we +# meet some day, and you think this stuff is worth it, you can buy me a +# beer soda in return. +# Phyks +# ----------------------------------------------------------------------------- + +import unittest +from fetcher import * + + +class TestFetcher(unittest.TestCase): + def setUp(self): + with open("tests/src/doi.bib", 'r') as fh: + self.doi_bib = fh.read() + with open("tests/src/arxiv.bib", 'r') as fh: + self.arxiv_bib = fh.read() + with open("tests/src/isbn.bib", 'r') as fh: + self.isbn_bib = fh.read() + + def test_download(self): + dl, contenttype = download('http://arxiv.org/pdf/1312.4006.pdf') + self.assertIn(contenttype, ['pdf', 'djvu']) + self.assertNotEqual(dl, '') + + def test_download_invalid_type(self): + self.assertFalse(download('http://phyks.me/')) + + def test_download_invalid_url(self): + self.assertFalse(download('a')) + + def test_findISBN_DJVU(self): + self.assertEqual(findISBN("tests/src/test_book.djvu"), '0198507194') + + def test_findISBN_PDF(self): + self.assertEqual(findISBN("tests/src/test_book.pdf"), '9780521846516') + + def test_findISBN_False(self): + self.assertFalse(findISBN("tests/src/test.pdf")) + + def test_isbn2Bib(self): + self.assertEqual(isbn2Bib('0198507194'), self.isbn_bib) + + def test_isbn2Bib_False(self): + self.assertEqual(isbn2Bib('blabla'), '') + + def test_findDOI_PDF(self): + self.assertEqual(findDOI("tests/src/test.pdf"), + '10.1103/physreva.88.043630') + + def test_findDOI_DJVU(self): + self.assertEqual(findDOI("tests/src/test.djvu"), + '10.1103/physreva.88.043630') + + def test_findDOI_False(self): + self.assertFalse(findDOI("tests/src/test_arxiv_multi.pdf")) + + def test_doi2Bib(self): + self.assertEqual(doi2Bib('10.1103/physreva.88.043630'), self.doi_bib) + + def test_doi2Bib_False(self): + self.assertEqual(doi2Bib('blabla'), '') + + def test_findArXivId(self): + self.assertEqual(findArXivId("tests/src/test_arxiv_multi.pdf"), + '1303.3130v1') + + def test_arXiv2Bib(self): + self.assertEqual(arXiv2Bib('1303.3130v1'), self.arxiv_bib) + + def test_arXiv2Bib_False(self): + self.assertEqual(arXiv2Bib('blabla'), '') + + def test_findHALId(self): + self.assertTupleEqual(findHALId("tests/src/test_hal.pdf"), + ('hal-00750893', '3')) + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_tools.py b/tests/test_tools.py new file mode 100644 index 0000000..bda2cbd --- /dev/null +++ b/tests/test_tools.py @@ -0,0 +1,36 @@ +# -*- coding: utf8 -*- +# ----------------------------------------------------------------------------- +# "THE NO-ALCOHOL BEER-WARE LICENSE" (Revision 42): +# Phyks (webmaster@phyks.me) wrote this file. As long as you retain this notice +# you can do whatever you want with this stuff (and you can also do whatever +# you want with this stuff without retaining it, but that's not cool...). If we +# meet some day, and you think this stuff is worth it, you can buy me a +# beer soda in return. +# Phyks +# ----------------------------------------------------------------------------- + +import unittest +from tools import * + + +class TestTools(unittest.TestCase): + def test_slugify(self): + self.assertEqual(slugify(u"à&é_truc.pdf"), "ae_trucpdf") + + def test_parsed2Bibtex(self): + parsed = {'type': 'article', 'id': 'test', 'field1': 'test1', + 'field2': 'test2'} + expected = ('@article{test,\n\tfield1={test1},\n' + + '\tfield2={test2},\n}\n\n') + self.assertEqual(parsed2Bibtex(parsed), expected) + + def test_getExtension(self): + self.assertEqual(getExtension('test.ext'), '.ext') + + def test_replaceAll(self): + replace_dict = {"test": "bidule", "machin": "chose"} + self.assertEqual(replaceAll("test machin truc", replace_dict), + "bidule chose truc") + +if __name__ == '__main__': + unittest.main()