pubs/tests/test_queries.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

103 lines
3.8 KiB
Python

import unittest
import dotdot
from pubs.commands.list_cmd import (_check_author_match,
_check_field_match,
_check_query_block,
filter_paper,
InvalidQuery)
from pubs.paper import Paper
import fixtures
doe_paper = Paper.from_bibentry(fixtures.doe_bibentry)
page_paper = Paper.from_bibentry(fixtures.page_bibentry)
turing_paper = Paper.from_bibentry(fixtures.turing_bibentry,
metadata=fixtures.turing_metadata)
class TestAuthorFilter(unittest.TestCase):
def test_fails_if_no_author(self):
no_doe = doe_paper.deepcopy()
no_doe.bibentry['author'] = []
self.assertTrue(not _check_author_match(no_doe, 'whatever'))
def test_match_case(self):
self.assertTrue(_check_author_match(doe_paper, 'doe'))
self.assertTrue(_check_author_match(doe_paper, 'doe',
case_sensitive=False))
def test_do_not_match_case(self):
self.assertFalse(_check_author_match(doe_paper, 'dOe'))
self.assertFalse(_check_author_match(doe_paper, 'doe',
case_sensitive=True))
def test_match_not_first_author(self):
self.assertTrue(_check_author_match(page_paper, 'motwani'))
def test_do_not_match_first_name(self):
self.assertTrue(not _check_author_match(page_paper, 'larry'))
class TestCheckTag(unittest.TestCase):
pass
class TestCheckField(unittest.TestCase):
def test_match_case(self):
self.assertTrue(_check_field_match(doe_paper, 'title', 'nice'))
self.assertTrue(_check_field_match(doe_paper, 'title', 'nice',
case_sensitive=False))
self.assertTrue(_check_field_match(doe_paper, 'year', '2013'))
def test_do_not_match_case(self):
self.assertTrue(_check_field_match(doe_paper, 'title',
'Title', case_sensitive=True))
self.assertFalse(_check_field_match(doe_paper, 'title', 'nice',
case_sensitive=True))
class TestCheckQueryBlock(unittest.TestCase):
def test_raise_invalid_if_no_value(self):
with self.assertRaises(InvalidQuery):
_check_query_block(doe_paper, 'title')
def test_raise_invalid_if_too_much(self):
with self.assertRaises(InvalidQuery):
_check_query_block(doe_paper, 'whatever:value:too_much')
class TestFilterPaper(unittest.TestCase):
def test_case(self):
self.assertTrue (filter_paper(doe_paper, ['title:nice']))
self.assertTrue (filter_paper(doe_paper, ['title:Nice']))
self.assertFalse(filter_paper(doe_paper, ['title:nIce']))
def test_fields(self):
self.assertTrue (filter_paper(doe_paper, ['year:2013']))
self.assertFalse(filter_paper(doe_paper, ['year:2014']))
self.assertTrue (filter_paper(doe_paper, ['author:doe']))
self.assertTrue (filter_paper(doe_paper, ['author:Doe']))
def test_tags(self):
self.assertTrue (filter_paper(turing_paper, ['tag:computer']))
self.assertFalse(filter_paper(turing_paper, ['tag:Ai']))
self.assertTrue (filter_paper(turing_paper, ['tag:AI']))
self.assertTrue (filter_paper(turing_paper, ['tag:ai']))
def test_multiple(self):
self.assertTrue (filter_paper(doe_paper,
['author:doe', 'year:2013']))
self.assertFalse(filter_paper(doe_paper,
['author:doe', 'year:2014']))
self.assertFalse(filter_paper(doe_paper,
['author:doee', 'year:2014']))
if __name__ == '__main__':
unittest.main()