diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +""" +Declare this directory a package so that 'python setup.py test' finds +the unittests here. +""" diff --git a/tests/test_File.py b/tests/test_File.py new file mode 100644 --- /dev/null +++ b/tests/test_File.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +""" +Test File Class +""" + +import unittest +# import code to be tested here (add to your python path!) +import ppf.jabref as dut + + +class Test_File(unittest.TestCase): + """Test File class.""" + + def test_File_init(self): + """Test File creation.""" + link1 = dut.Link(name='x', path='/bla/di/bla', filetype='DOCX') + link2 = dut.Link(name='whatever', path=r'..\what\ever', filetype='PDF') + + list_of_links = [link1, link2] + file = dut.File(list_of_links) + + self.assertTrue(file._links is list_of_links) + + def test_File_init_rejects_non_links(self): + """Test File creation rejects non-Link entries.""" + with self.assertRaises(TypeError): + dut.File([dut.Link('x', 'p', 'PDF'), 'not-a-link']) + + def test_Link_from_string(self): + """Test Link creation from string.""" + string = 'Pref:Bks\\Chpt0.pdf:PDF;Chapter 1:Bks\\Chpt1.pdf:PDF' + file = dut.File.from_string(string) + + self.assertTrue(file._links[0].name == 'Pref') + self.assertTrue(file._links[0].path == 'Bks\\Chpt0.pdf') + self.assertTrue(file._links[0].filetype == 'PDF') + self.assertTrue(file._links[1].name == 'Chapter 1') + self.assertTrue(file._links[1].path == 'Bks\\Chpt1.pdf') + self.assertTrue(file._links[1].filetype == 'PDF') + + def test_File_repr(self): + """Test File string representation.""" + link1 = dut.Link(name='a', path='b:c', filetype='d') + link2 = dut.Link(name='e', path='f', filetype='g;h') + file = dut.File([link1, link2]) + self.assertTrue(str(file) == r'a:b\:c:d;e:f:g\;h') + + +class Test_File_List_Behavior(unittest.TestCase): + """Test list operations on File""" + + def setUp(self): + self.list_of_links = [dut.Link('A', './docs/A.docx', 'DOCX'), + dut.Link('B', 'pdfs/B.pdf', 'PDF')] + self.file = dut.File(self.list_of_links) + + def test_indexing(self): + self.assertTrue(self.file[0] == self.list_of_links[0]) + + def test_len(self): + self.assertTrue(len(self.file) == 2) + + def test_append(self): + new_link = dut.Link('C', r'.\presentations\C.pptx', 'PPTX') + self.file.append(new_link) + self.assertTrue(len(self.file) == 3) + + def test_append_rejects_non_link(self): + with self.assertRaises(TypeError): + self.file.append('not-a-link') + + def test_setitem_rejects_non_link(self): + with self.assertRaises(TypeError): + self.file[0] = 'not-a-link' + + def test_setitem(self): + new_link = dut.Link('C', 'docs/C.pdf', 'PDF') + self.file[1] = new_link + self.assertTrue(self.file[1] is new_link) + + def test_remove(self): + original_link = self.list_of_links[0] + popped_link = self.file.pop(0) + self.assertTrue(len(self.file) == 1) + self.assertTrue(popped_link is original_link) + + +if __name__ == '__main__': + # This enables running the unit tests by running this script which is + # much more convenient than 'python setup.py test' while developing tests. + unittest.main() diff --git a/tests/test_Link.py b/tests/test_Link.py new file mode 100644 --- /dev/null +++ b/tests/test_Link.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- +""" +Test Link Class +""" + +import unittest +# import code to be tested here (add to your python path!) +import ppf.jabref as dut + + +class Test_Link(unittest.TestCase): + """Test Link class.""" + + def test_Link_init(self): + """Test Link creation.""" + link = dut.Link(name='Preface', + path='/There/is/a/preface/somewhere.pdf', + filetype='PDF') + self.assertTrue(link.name == 'Preface') + self.assertTrue(link.path == '/There/is/a/preface/somewhere.pdf') + self.assertTrue(link.filetype == 'PDF') + + def test_Link_from_string(self): + """Test Link creation from string.""" + string = 'Preface:/There/is/a/preface/somewhere.pdf:PDF' + link = dut.Link.from_string(string) + self.assertTrue(link.name == 'Preface') + self.assertTrue(link.path == '/There/is/a/preface/somewhere.pdf') + self.assertTrue(link.filetype == 'PDF') + + def test_escape(self): + """Test Link.escape() for special characters.""" + self.assertTrue(dut.Link.escape('a:b') == r'a\:b') + self.assertTrue(dut.Link.escape('a;b') == r'a\;b') + self.assertTrue(dut.Link.escape(r'a\b') == r'a\\b') + self.assertTrue(dut.Link.escape(r'a\:b') == r'a\\\:b') + self.assertTrue(dut.Link.escape(r'a\;b') == r'a\\\;b') + self.assertTrue(dut.Link.escape(r'a\:b\;c') == r'a\\\:b\\\;c') + + def test_repr(self): + """Test Link string representation.""" + link = dut.Link(name='A', path='B:C', filetype='D;E') + self.assertTrue(str(link) == r'A:B\:C:D\;E') + + +if __name__ == '__main__': + # This enables running the unit tests by running this script which is + # much more convenient than 'python setup.py test' while developing tests. + unittest.main() diff --git a/tests/test_citationkeymapping.py b/tests/test_citationkeymapping.py new file mode 100644 --- /dev/null +++ b/tests/test_citationkeymapping.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Test citationkey mapping Functions. +""" + +import unittest +# import code to be tested here (add to your python path!) +import ppf.jabref as dut +from functools import reduce + + +class Test_CitationkeyMapping(unittest.TestCase): + """Test counter2citationkey() and citationkey2counter functions.""" + + def test_counter2citationkey(self): + """Test counter2citationkey with known values.""" + numbers = [0, 26, 26 + 26**2] + ckeys_true = ['a', 'aa', 'aaa'] + ckeys = [dut.counter2citationkey(c) for c in numbers] + + equal = reduce(lambda a, b: a and b, + map(lambda p, q: p == q, ckeys_true, ckeys), + True) + self.assertTrue(equal) + + def test_citationkey2counter(self): + """Test citationkey2counter with known values.""" + numbers_true = [0, 26, 26 + 26**2] + ckeys = ['a', 'aa', 'aaa'] + numbers = [dut.citationkey2counter(c) for c in ckeys] + + equal = reduce(lambda a, b: a and b, + map(lambda p, q: p == q, numbers_true, numbers), + True) + self.assertTrue(equal) + + +if __name__ == '__main__': + # This enables running the unit tests by running this script which is + # much more convenient than 'python setup.py test' while developing tests. + unittest.main() diff --git a/tests/test_split_by_unescaped_sep.py b/tests/test_split_by_unescaped_sep.py new file mode 100644 --- /dev/null +++ b/tests/test_split_by_unescaped_sep.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +""" +Test Link Class +""" + +import unittest +# import code to be tested here (add to your python path!) +import ppf.jabref as dut + + +class Test_split_by_unescaped_sep(unittest.TestCase): + """Test split_by_unescaped_sep() function.""" + + def test_harmless(self): + """Test string without escaped chars.""" + str = 'bla:di:bla' + split = dut.split_by_unescaped_sep(str, sep=':') + self.assertTrue(split[0] == 'bla') + self.assertTrue(split[1] == 'di') + self.assertTrue(split[2] == 'bla') + + def test_escaped_colon(self): + """Test string with escaped colon.""" + str = r'bla\:bla:di:bla' + split = dut.split_by_unescaped_sep(str, sep=':') + self.assertTrue(split[0] == r'bla\:bla') + self.assertTrue(split[1] == 'di') + self.assertTrue(split[2] == 'bla') + + def test_backslash_before_colon(self): + """Test string with backslash before colon.""" + str = r'bla:di\\:bla' + split = dut.split_by_unescaped_sep(str, sep=':') + self.assertTrue(split[0] == 'bla') + self.assertTrue(split[1] == r'di\\') + self.assertTrue(split[2] == 'bla') + + def test_multibackslash(self): + """Test string with multiple backslashs.""" + str = r'bla:di\\\::\\bla' + split = dut.split_by_unescaped_sep(str, sep=':') + self.assertTrue(split[0] == 'bla') + self.assertTrue(split[1] == r'di\\\:') + self.assertTrue(split[2] == r'\\bla') + + +if __name__ == '__main__': + # This enables running the unit tests by running this script which is + # much more convenient than 'python setup.py test' while developing tests. + unittest.main()