From 6e0c63684dffc523930acf3b7814c17ecf322132 Mon Sep 17 00:00:00 2001 From: Karl Voit Date: Sun, 10 Jan 2016 19:23:43 +0100 Subject: [PATCH] added more unit-tests --- filetags.py | 15 +++---- tests/unit_tests.py | 98 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 9 deletions(-) diff --git a/filetags.py b/filetags.py index 09d8e49..003ecfb 100755 --- a/filetags.py +++ b/filetags.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Time-stamp: <2016-01-10 18:35:09 vk> +# Time-stamp: <2016-01-10 19:18:10 vk> ## TODO: ## * fix parts marked with «FIXXME» @@ -434,6 +434,7 @@ def get_tags_from_files_and_subfolders(startdir=os.getcwdu(), starttags=False, r return tags + def find_similar_tags(tag, tags): """ Returns a list of entries of tags that are similar to tag (but not same as tag) @@ -450,7 +451,7 @@ def find_similar_tags(tag, tags): similar_tags = difflib.get_close_matches(tag, tags, n=999, cutoff=0.7) close_but_not_exact_matches = [] - ## omit exact matches + ## omit exact matches FIXXME: this can be done in one eloquent line -> refactor for match in similar_tags: if match != tag: close_but_not_exact_matches.append(match) @@ -744,9 +745,9 @@ def check_for_possible_shortcuts_in_entered_tags(tags, list_of_shortcut_tags): Returns tags if the only tag is not a shortcut (entered as integer). Returns a list of corresponding tags if it's an integer. - @param tags: list of entered tags from the user - @param list_of_shortcut_tags: list of possible shortcut tags - @param return: list of tags which were meant by the user + @param tags: list of entered tags from the user, e.g., [u'23'] + @param list_of_shortcut_tags: list of possible shortcut tags, e.g., [u'bar', u'folder1', u'baz'] + @param return: list of tags which were meant by the user, e.g., [u'bar', u'baz'] """ assert tags.__class__ == list @@ -762,7 +763,7 @@ def check_for_possible_shortcuts_in_entered_tags(tags, list_of_shortcut_tags): try: tags.append(list_of_shortcut_tags[int(character)-1]) except IndexError: - error_exit(9, u'Index number exceeds list: Entered shortcut \"' + character + '\" was not provided by list: ' + str(list_of_shortcut_tags)) + return potential_shortcut_string except ValueError: logging.debug('single entered tag is a normal tag') tags = potential_shortcut_string @@ -775,7 +776,7 @@ def get_upto_nine_keys_of_dict_with_highest_value(mydict): Takes a dict, sorts it according to their values, and returns up to nine values with the highest values. - Example1: { "key2":45; "key1": 33} -> [ "key1", "key2" ] + Example1: { "key2":45, "key1": 33} -> [ "key1", "key2" ] @param mydict: dictionary holding keys and values @param return: list of up to top nine keys according to the rank of their values diff --git a/tests/unit_tests.py b/tests/unit_tests.py index aa3a29e..ad08a0f 100755 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Time-stamp: <2015-01-02 14:17:50 vk> +# Time-stamp: <2016-01-10 19:23:23 vk> ## invoke tests using following command line: ## ~/src/vktag % PYTHONPATH="~/src/filetags:" tests/unit_tests.py --verbose @@ -68,6 +68,26 @@ class TestMethods(unittest.TestCase): self.assertEqual(filetags.add_tag_to_countdict(u'newtag', {u'oldtag': 1}), {u'oldtag': 1, u'newtag': 1}) self.assertEqual(filetags.add_tag_to_countdict(u'newtag', {u'oldtag': 2}), {u'oldtag': 2, u'newtag': 1}) + def test_find_similar_tags(self): + + self.assertEqual(filetags.find_similar_tags(u'xxx', [u'foobar', u'bar', u'baz', u'Frankenstein', u'parabol', u'Bah', u'paR', u'por', u'Schneewittchen']), []) + + self.assertEqual(filetags.find_similar_tags(u'Simpson', [u'foobar', u'Simson', u'simpson', u'Frankenstein', u'sumpson', u'Simpso', u'impson', u'mpson', u'Schneewittchen']), \ + [u'impson', u'Simson', u'Simpso', u'simpson', u'mpson', u'sumpson']) + + def test_check_for_possible_shortcuts_in_entered_tags(self): + + self.assertEqual(filetags.check_for_possible_shortcuts_in_entered_tags([u'bar'], [u'Frankenstein', u'Schneewittchen']), [u'bar']) + self.assertEqual(filetags.check_for_possible_shortcuts_in_entered_tags([u'34'], [u'Frankenstein', u'Schneewittchen', u'baz', u'bar']), [u'baz', u'bar']) + self.assertEqual(filetags.check_for_possible_shortcuts_in_entered_tags([u'12'], [u'Frankenstein', u'Schneewittchen', u'baz', u'bar']), [u'Frankenstein', u'Schneewittchen']) + self.assertEqual(filetags.check_for_possible_shortcuts_in_entered_tags([u'39'], [u'Frankenstein', u'Schneewittchen', u'baz', u'bar']), [u'39']) + + def test_get_upto_nine_keys_of_dict_with_highest_value(self): + + self.assertEqual(filetags.get_upto_nine_keys_of_dict_with_highest_value({ "key2":45, "key1": 33}), [ "key1", "key2" ]) + self.assertEqual(filetags.get_upto_nine_keys_of_dict_with_highest_value({ "key1":45, "key2": 33, "key3": 3, "key4": 1, "key5": 5, "key6": 159, "key7": 0, "key8": 999, "key9": 42, "key10": 4242}), \ + [ "key1", "key10", "key2", "key3", "key4", "key5", "key6", "key8", "key9"]) + def tearDown(self): pass @@ -83,7 +103,7 @@ class TestFileWithoutTags(unittest.TestCase): ## create temporary directory: self.tempdir = tempfile.mkdtemp() os.chdir(self.tempdir) - print "\ntemporary directory: " + self.tempdir + print "\nTestFileWithoutTags: temporary directory: " + self.tempdir ## create set of test files: self.create_tmp_file(self.testfilename) @@ -130,6 +150,27 @@ class TestFileWithoutTags(unittest.TestCase): [u'two'], do_remove=True, dryrun=False) self.assertEqual(self.file_exists(u'a test file . for you.txt'), True) + def test_unique_labels(self): + + ## adding a unique label to a file without any tags: + new_filename = filetags.handle_file(os.path.join(self.tempdir, self.testfilename), [u'labelgreen'], False, False) + self.assertEqual(self.file_exists(u'a test file . for you -- labelgreen.txt'), True) + + ## adding a second unique label - first one should be gone: + filetags.handle_file(os.path.join(self.tempdir, u'a test file . for you -- labelgreen.txt'), + [u'labelyellow'], do_remove=False, dryrun=False) + self.assertEqual(self.file_exists(u'a test file . for you -- labelyellow.txt'), True) + + ## adding non-unique tags: + filetags.handle_file(os.path.join(self.tempdir, u'a test file . for you -- labelyellow.txt'), + [u'one', u'two'], do_remove=False, dryrun=False) + self.assertEqual(self.file_exists(u'a test file . for you -- labelyellow one two.txt'), True) + + ## removing unique label: + filetags.handle_file(os.path.join(self.tempdir, u'a test file . for you -- labelyellow one two.txt'), + [u'labelyellow', u'one'], do_remove=True, dryrun=False) + self.assertEqual(self.file_exists(u'a test file . for you -- two.txt'), True) + def test_adding_a_tag_to_file_without_extension(self): filename = u"file without extension" @@ -190,5 +231,58 @@ class TestFileWithoutTags(unittest.TestCase): rmtree(self.tempdir) +class TestHierarchyWithFilesAndFolders(unittest.TestCase): + + tempdir = None + + def setUp(self): + + ## create temporary directory: + self.tempdir = tempfile.mkdtemp() + os.chdir(self.tempdir) + print "\nTestHierarchyWithFilesAndFolders: temporary directory: " + self.tempdir + + ## initial tests without files: + self.assertEqual(filetags.get_tags_from_files_and_subfolders(self.tempdir, False, False), {}) + + ## create set of test files: + self.create_tmp_file("foo1 -- bar.txt") + self.create_tmp_file("foo2 -- bar baz.txt") + self.create_tmp_file("foo3 -- bar baz labelgreen.txt") + + def create_tmp_file(self, name): + + open(os.path.join(self.tempdir, name), 'w') + + def file_exists(self, name): + + return os.path.isfile(os.path.join(self.tempdir, name)) + + def test_vocabulary_in_real_world_example(self): + + print "FIXXME: test_vocabulary_in_real_world_example needs vocabulary + tests" + + def test_get_tags_from_files_and_subfolders(self): + + self.assertEqual(filetags.get_tags_from_files_and_subfolders(self.tempdir, False, False), {u'baz': 2, u'bar': 3, u'labelgreen': 1}) + + def test_list_unknown_tags(self): + + print "FIXXME: test_list_unknown_tags() not implemented yet" + + def test_handle_tag_gardening(self): + + print "FIXXME: test_handle_tag_gardening() not implemented yet" + + def test_locate_and_parse_controlled_vocabulary(self): + + print "FIXXME: test_locate_and_parse_controlled_vocabulary() not implemented yet" + + + def tearDown(self): + + rmtree(self.tempdir) + + if __name__ == '__main__': unittest.main()