pubs/tests/test_repo.py
Olivier Mangin de2a50eeb2 Fix tests and terminology.
First this brings a change to the paper API: the terminology of bibdata
and bibentry is clarified: now bibentry is a dictionary of the form
{citekey: bibdata} where bibdata corresponds to the actual dictionary of
bibliographic fields and values {author: ..., year: ...}.
Now bibentry is an attribute of the paper object that is generated from
citekey and bibdata.

This commit fixes all tests in particular an issue with citekey in
bibentry not updated.

Also removes prints in tests and deprecated assertEquals.

Usecase tests now fail if the command ask for unexpected inputs.

Removes queries for user input in attach and add commands (for deletion
of a copied document file). The input was not coherent with tests and is
annoying.
2015-05-07 13:17:44 +02:00

93 lines
3.3 KiB
Python

import unittest
from datetime import datetime
import dotdot
import fake_env
import fixtures
from pubs.repo import Repository, _base27, CiteKeyCollision, InvalidReference
from pubs.paper import Paper
from pubs import configs
class TestRepo(fake_env.TestFakeFs):
def setUp(self):
super(TestRepo, self).setUp()
self.repo = Repository(configs.Config(), create=True)
self.repo.push_paper(Paper.from_bibentry(fixtures.turing_bibentry))
class TestCitekeyGeneration(TestRepo):
def test_string_increment(self):
self.assertEqual(_base27(0), '')
for i in range(26):
self.assertEqual(_base27(i + 1), chr(97 + i))
self.assertEqual(_base27(26 + i + 1), 'a' + chr(97 + i))
def test_generated_key_is_unique(self):
self.repo.push_paper(Paper.from_bibentry(fixtures.doe_bibentry))
c = self.repo.unique_citekey('Doe2013')
self.repo.push_paper(Paper.from_bibentry(fixtures.doe_bibentry,
citekey='Doe2013a'))
c = self.repo.unique_citekey('Doe2013')
self.assertEqual(c, 'Doe2013b')
class TestPushPaper(TestRepo):
def test_raises_value_error_on_existing_key(self):
with self.assertRaises(CiteKeyCollision):
self.repo.push_paper(Paper.from_bibentry(fixtures.turing_bibentry))
def test_pushes_paper_bibdata(self):
orig = fixtures.doe_bibentry
self.repo.push_paper(Paper.from_bibentry(orig))
retrieved = self.repo.databroker.pull_bibentry('Doe2013')
self.assertEqual(orig, retrieved)
def test_pushes_paper_metadata(self):
orig = {'docfile': 'dummy', 'tags': set(['tag', 'another']),
'added': datetime(2012, 12, 12, 12, 12, 12, 12)}
self.repo.push_paper(Paper.from_bibentry(fixtures.doe_bibentry,
metadata=orig))
retrieved = self.repo.databroker.pull_metadata('Doe2013')
self.assertEqual(orig, retrieved)
def test_pushes_paper_metadata_set_added(self):
orig = {'docfile': 'dummy', 'tags': set(['tag', 'another'])}
now = datetime.now()
self.repo.push_paper(Paper.from_bibentry(fixtures.doe_bibentry,
metadata=orig))
retrieved = self.repo.databroker.pull_metadata('Doe2013')
self.assertIn('added', retrieved)
self.assertTrue(now < retrieved['added'])
class TestUpdatePaper(TestRepo):
def test_updates_same_key(self):
new = self.repo.pull_paper('turing1950computing')
new.bibentry['year'] = '51'
self.repo.push_paper(new, overwrite=True)
self.assertEqual(new, self.repo.pull_paper('turing1950computing'))
def test_update_new_key_removes_old(self):
paper = self.repo.pull_paper('turing1950computing')
self.repo.rename_paper(paper, 'Turing1950')
with self.assertRaises(InvalidReference):
self.repo.pull_paper('turing1950computing')
self.assertNotIn('turing1950computing', self.repo)
def test_update_new_key_updates(self):
paper = self.repo.pull_paper('turing1950computing')
self.repo.rename_paper(paper, 'Turing1950')
self.assertEqual(paper, self.repo.pull_paper('Turing1950'))
# TODO: should also check that associated files are updated
if __name__ == '__main__':
unittest.main()