Fails to add with existing citekey.

Also adds add usecase test and improves repository __contains__.
main
Olivier Mangin 11 years ago
parent 39b2e4f912
commit 89bf370902

@ -81,8 +81,9 @@ def command(args):
if citekey is None:
base_key = bibstruct.extract_citekey(bibdata)
citekey = rp.unique_citekey(base_key)
else:
rp.databroker.exists(citekey, meta_check=False)
elif citekey in rp:
ui.error('citekey already exist {}.'.format(citekey))
ui.exit(1)
p = paper.Paper(bibdata, citekey=citekey)

@ -36,9 +36,10 @@ class Repository(object):
def __contains__(self, citekey):
""" Allows to use 'if citekey in repo' pattern
Warning: costly the first time.
The convention is that the paper is in the repository
if and only if a bibfile is in the repository.
"""
return citekey in self.citekeys
return self.databroker.exists(citekey)
def __len__(self):
"""Warning: costly the first time."""
@ -51,8 +52,7 @@ class Repository(object):
def pull_paper(self, citekey):
"""Load a paper by its citekey from disk, if necessary."""
if self.databroker.exists(citekey, meta_check=True):
#TODO meta_check=False and default meta generation
if citekey in self:
return Paper(self.databroker.pull_bibdata(citekey),
citekey=citekey,
metadata=self.databroker.pull_metadata(citekey))
@ -66,8 +66,7 @@ class Repository(object):
if True, mimick the behavior of updating a paper
"""
bibstruct.check_citekey(paper.citekey)
if (not overwrite) and (self.databroker.exists(paper.citekey, meta_check=False)
or (paper.citekey in self)):
if (not overwrite) and (paper.citekey in self):
raise CiteKeyCollision('citekey {} already in use'.format(paper.citekey))
if not paper.added:
paper.added = datetime.now()

@ -151,6 +151,14 @@ class TestAdd(DataCommandTestCase):
self.execute_cmds(cmds)
self.assertEqual(set(self.fs['os'].listdir('/not_default/doc')), {'Page99.pdf'})
def test_add_citekey(self):
cmds = ['pubs init',
'pubs add -k CustomCitekey /data/pagerank.bib',
]
self.execute_cmds(cmds)
bib_dir = self.fs['os'].path.join(self.default_pubs_dir, 'bib')
self.assertEqual(set(self.fs['os'].listdir(bib_dir)), {'CustomCitekey.bib'})
def test_add_doc_nocopy_does_not_copy(self):
cmds = ['pubs init',
'pubs add /data/pagerank.bib -C -d /data/pagerank.pdf',
@ -160,6 +168,14 @@ class TestAdd(DataCommandTestCase):
self.fs['os'].path.join(self.default_pubs_dir, 'doc')),
[])
def test_add_twice_fails(self):
cmds = ['pubs init',
'pubs add /data/pagerank.bib',
'pubs add -k Page99 /data/turing1950.bib',
]
with self.assertRaises(SystemExit):
self.execute_cmds(cmds)
class TestList(DataCommandTestCase):

Loading…
Cancel
Save