Skip to content

Commit

Permalink
Increase from 60% to 61% test coverage ;) #48
Browse files Browse the repository at this point in the history
- dry-run as part of add bib
- test_biblio to tests specific Biblio methods (here __eq__)
- papers uninstall in teaddown => needed to fix one test with
  --relative-path  !
  • Loading branch information
perrette committed Apr 25, 2023
1 parent e65a404 commit bc9b06b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
3 changes: 2 additions & 1 deletion papers/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ def savebib(my_bib, config):
"""
Given a Biblio object and its configuration, save them to disk. If you're using the git bib tracker, will trigger a git commit there.
"""
logger.info('Saving '+config.bibtex)
if papers.config.DRYRUN:
logger.info(f'DRYRUN: NOT saving {config.bibtex}')
return
logger.info(f'Saving {config.bibtex}')
if my_bib is not None:
my_bib.save(config.bibtex)
if config.git:
Expand Down
35 changes: 27 additions & 8 deletions tests/test_add.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ def test_fails_without_install(self):

def test_add(self):
self.assertTrue(os.path.exists(self.mybib))
print("bibtex", self.mybib, 'exists?', os.path.exists(self.mybib))
paperscmd(f'add --bibtex {self.mybib} {self.pdf}')

file_ = self._checkbib(dismiss_key=True)
Expand Down Expand Up @@ -144,17 +143,38 @@ def setUp(self):
self.pdf1, self.doi, self.key1, self.newkey1, self.year, self.bibtex1, self.file_rename1 = prepare_paper()
self.pdf2, self.si, self.doi, self.key2, self.newkey2, self.year, self.bibtex2, self.file_rename2 = prepare_paper2()
bib = '\n'.join([self.bibtex1, self.bibtex2])
open(self.somebib,'w').write(bib)
self.my = Biblio.newbib(self.mybib, '')
open(self.mybib,'w').write(self.bibtex1)
open(self.somebib,'w').write(self.bibtex2)
self.my = Biblio.load(self.mybib, '')

def test_addbib(self):
self.assertTrue(self.key1 not in [e['ID'] for e in self.my.db.entries])
def test_addbib_method(self):
self.assertTrue(self.key1 in [e['ID'] for e in self.my.db.entries])
self.assertTrue(self.key2 not in [e['ID'] for e in self.my.db.entries])
self.my.add_bibtex_file(self.somebib)
self.assertEqual(len(self.my.db.entries), 2)
self.assertEqual(self.my.db.entries[0]['ID'], self.key1)
self.assertEqual(self.my.db.entries[1]['ID'], self.key2)

def test_addbib_cmd(self):
bib = Biblio.load(self.mybib, '')
self.assertEqual(len(bib.db.entries), 1)
self.assertEqual(bib.db.entries[0]['ID'], self.key1)
paperscmd(f'add {self.somebib} --bibtex {self.mybib}')
bib = Biblio.load(self.mybib, '')
self.assertEqual(len(bib.db.entries), 2)
self.assertEqual(bib.db.entries[0]['ID'], self.key1)
self.assertEqual(bib.db.entries[1]['ID'], self.key2)

def test_addbib_cmd_dryrun(self):
bib = Biblio.load(self.mybib, '')
self.assertEqual(len(bib.db.entries), 1)
self.assertEqual(bib.db.entries[0]['ID'], self.key1)
paperscmd(f'add {self.somebib} --bibtex {self.mybib} --dry-run')
bib = Biblio.load(self.mybib, '')
self.assertEqual(len(bib.db.entries), 1)
self.assertEqual(bib.db.entries[0]['ID'], self.key1)
self.assertTrue(self.key2 not in [e['ID'] for e in self.my.db.entries])

def tearDown(self):
os.remove(self.mybib)
os.remove(self.somebib)
Expand Down Expand Up @@ -193,8 +213,7 @@ def test_adddir_pdf_cmd(self):
def tearDown(self):
os.remove(self.mybib)
shutil.rmtree(self.somedir)
if os.path.exists('.papersconfig.json'):
os.remove('.papersconfig.json')
paperscmd(f'uninstall')



Expand Down Expand Up @@ -340,7 +359,7 @@ def test_add_same_but_key_fails(self):

def test_add_same_but_file(self):
open(self.otherbib, 'w').write(self.bibtex_hasfile)
paperscmd(f'add {self.otherbib} --bibtex {self.mybib} -u')
paperscmd(f'add {self.otherbib} --bibtex {self.mybib} -u --relative-path')
self.assertMultiLineEqual(open(self.mybib).read().strip(), self.bibtex_hasfile) # entries did not change


Expand Down
21 changes: 21 additions & 0 deletions tests/test_biblio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
import unittest
import tempfile
from papers.bib import Biblio
from tests.common import prepare_paper

class TestBiblio(unittest.TestCase):

def setUp(self):
self.mybib = tempfile.mktemp(prefix='papers.bib')
# self.somebib = tempfile.mktemp(prefix='papers.somebib.bib')
self.pdf, self.doi, self.key, self.newkey, self.year, self.bibtex, self.file_rename = prepare_paper()
open(self.mybib,'w').write(self.bibtex)
self.biblio = Biblio.load(self.mybib, '')

def test_bib_equal(self):
self.assertTrue(self.biblio == self.biblio)

def tearDown(self):
os.remove(self.mybib)
# os.remove(self.somebib)
Empty file added tests/test_misc.py
Empty file.

0 comments on commit bc9b06b

Please sign in to comment.