Merge pull request #61 from wheresmyjetpack/tests
2 New tests, DRYing out BukuDb unit tests
This commit is contained in:
commit
e233ebf2df
@ -4,6 +4,7 @@
|
|||||||
from genericpath import exists
|
from genericpath import exists
|
||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
import unittest, pytest
|
import unittest, pytest
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
@ -56,6 +57,7 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
os.remove(TEST_TEMP_DBFILE_PATH)
|
os.remove(TEST_TEMP_DBFILE_PATH)
|
||||||
|
|
||||||
self.bookmarks = TEST_BOOKMARKS
|
self.bookmarks = TEST_BOOKMARKS
|
||||||
|
self.bdb = BukuDb()
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
os.environ['XDG_DATA_HOME'] = TEST_TEMP_DIR_PATH
|
||||||
@ -105,44 +107,40 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_get_bookmark_by_index(self):
|
def test_get_bookmark_by_index(self):
|
||||||
bdb = BukuDb()
|
|
||||||
for bookmark in self.bookmarks:
|
for bookmark in self.bookmarks:
|
||||||
# adding bookmark from self.bookmarks
|
# adding bookmark from self.bookmarks
|
||||||
bdb.add_bookmark(*bookmark)
|
self.bdb.add_bookmark(*bookmark)
|
||||||
|
|
||||||
# the expected bookmark
|
# the expected bookmark
|
||||||
expected = (1, 'http://slashdot.org', 'SLASHDOT', ',news,old,',
|
expected = (1, 'http://slashdot.org', 'SLASHDOT', ',news,old,',
|
||||||
"News for old nerds, stuff that doesn't matter")
|
"News for old nerds, stuff that doesn't matter")
|
||||||
bookmark_from_db = bdb.get_bookmark_by_index(1)
|
bookmark_from_db = self.bdb.get_bookmark_by_index(1)
|
||||||
# asserting bookmark matches expected
|
# asserting bookmark matches expected
|
||||||
self.assertEqual(expected, bookmark_from_db)
|
self.assertEqual(expected, bookmark_from_db)
|
||||||
# asserting None returned if index out of range
|
# asserting None returned if index out of range
|
||||||
self.assertIsNone(bdb.get_bookmark_by_index( len(self.bookmarks[0]) + 1 ))
|
self.assertIsNone(self.bdb.get_bookmark_by_index( len(self.bookmarks[0]) + 1 ))
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_get_bookmark_index(self):
|
def test_get_bookmark_index(self):
|
||||||
bdb = BukuDb()
|
|
||||||
for idx, bookmark in enumerate(self.bookmarks):
|
for idx, bookmark in enumerate(self.bookmarks):
|
||||||
# adding bookmark from self.bookmarks to database
|
# adding bookmark from self.bookmarks to database
|
||||||
bdb.add_bookmark(*bookmark)
|
self.bdb.add_bookmark(*bookmark)
|
||||||
# asserting index is in order
|
# asserting index is in order
|
||||||
idx_from_db = bdb.get_bookmark_index(bookmark[0])
|
idx_from_db = self.bdb.get_bookmark_index(bookmark[0])
|
||||||
self.assertEqual(idx + 1, idx_from_db)
|
self.assertEqual(idx + 1, idx_from_db)
|
||||||
|
|
||||||
# asserting -1 is returned for nonexistent url
|
# asserting -1 is returned for nonexistent url
|
||||||
idx_from_db = bdb.get_bookmark_index("http://nonexistent.url")
|
idx_from_db = self.bdb.get_bookmark_index("http://nonexistent.url")
|
||||||
self.assertEqual(-1, idx_from_db)
|
self.assertEqual(-1, idx_from_db)
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_add_bookmark(self):
|
def test_add_bookmark(self):
|
||||||
bdb = BukuDb()
|
|
||||||
|
|
||||||
for bookmark in self.bookmarks:
|
for bookmark in self.bookmarks:
|
||||||
# adding bookmark from self.bookmarks to database
|
# adding bookmark from self.bookmarks to database
|
||||||
bdb.add_bookmark(*bookmark)
|
self.bdb.add_bookmark(*bookmark)
|
||||||
# retrieving bookmark from database
|
# retrieving bookmark from database
|
||||||
index = bdb.get_bookmark_index(bookmark[0])
|
index = self.bdb.get_bookmark_index(bookmark[0])
|
||||||
from_db = bdb.get_bookmark_by_index(index)
|
from_db = self.bdb.get_bookmark_by_index(index)
|
||||||
self.assertIsNotNone(from_db)
|
self.assertIsNotNone(from_db)
|
||||||
# comparing data
|
# comparing data
|
||||||
for pair in zip(from_db[1:], bookmark):
|
for pair in zip(from_db[1:], bookmark):
|
||||||
@ -152,17 +150,16 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_update_bookmark(self):
|
def test_update_bookmark(self):
|
||||||
bdb = BukuDb()
|
|
||||||
old_values = self.bookmarks[0]
|
old_values = self.bookmarks[0]
|
||||||
new_values = self.bookmarks[1]
|
new_values = self.bookmarks[1]
|
||||||
|
|
||||||
# adding bookmark and getting index
|
# adding bookmark and getting index
|
||||||
bdb.add_bookmark(*old_values)
|
self.bdb.add_bookmark(*old_values)
|
||||||
index = bdb.get_bookmark_index(old_values[0])
|
index = self.bdb.get_bookmark_index(old_values[0])
|
||||||
# updating with new values
|
# updating with new values
|
||||||
bdb.update_bookmark(index, *new_values)
|
self.bdb.update_bookmark(index, *new_values)
|
||||||
# retrieving bookmark from database
|
# retrieving bookmark from database
|
||||||
from_db = bdb.get_bookmark_by_index(index)
|
from_db = self.bdb.get_bookmark_by_index(index)
|
||||||
self.assertIsNotNone(from_db)
|
self.assertIsNotNone(from_db)
|
||||||
# checking if values are updated
|
# checking if values are updated
|
||||||
for pair in zip(from_db[1:], new_values):
|
for pair in zip(from_db[1:], new_values):
|
||||||
@ -170,16 +167,15 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_append_tag_at_index(self):
|
def test_append_tag_at_index(self):
|
||||||
bdb = BukuDb()
|
|
||||||
for bookmark in self.bookmarks:
|
for bookmark in self.bookmarks:
|
||||||
bdb.add_bookmark(*bookmark)
|
self.bdb.add_bookmark(*bookmark)
|
||||||
|
|
||||||
# tags to add
|
# tags to add
|
||||||
old_tags = bdb.get_bookmark_by_index(1)[3]
|
old_tags = self.bdb.get_bookmark_by_index(1)[3]
|
||||||
new_tags = ",foo,bar,baz"
|
new_tags = ",foo,bar,baz"
|
||||||
bdb.append_tag_at_index(1, new_tags)
|
self.bdb.append_tag_at_index(1, new_tags)
|
||||||
# updated list of tags
|
# updated list of tags
|
||||||
from_db = bdb.get_bookmark_by_index(1)[3]
|
from_db = self.bdb.get_bookmark_by_index(1)[3]
|
||||||
|
|
||||||
# checking if new tags were added to the bookmark
|
# checking if new tags were added to the bookmark
|
||||||
self.assertTrue(all( x in from_db.split(',') for x in new_tags.split(',') ))
|
self.assertTrue(all( x in from_db.split(',') for x in new_tags.split(',') ))
|
||||||
@ -187,12 +183,50 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
self.assertTrue(all( x in from_db.split(',') for x in old_tags.split(',') ))
|
self.assertTrue(all( x in from_db.split(',') for x in old_tags.split(',') ))
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_refreshdb(self):
|
def test_append_tag_at_all_indices(self):
|
||||||
bdb = BukuDb()
|
for bookmark in self.bookmarks:
|
||||||
|
self.bdb.add_bookmark(*bookmark)
|
||||||
|
|
||||||
bdb.add_bookmark("https://www.google.com/ncr", "?")
|
inclusive_range = lambda start, end: range(start, end + 1)
|
||||||
bdb.refreshdb(1)
|
# tags to add
|
||||||
from_db = bdb.get_bookmark_by_index(1)
|
new_tags = ",foo,bar,baz"
|
||||||
|
# record of original tags for each bookmark
|
||||||
|
old_tagsets = { i: self.bdb.get_bookmark_by_index(i)[3] for i in inclusive_range(1, len(self.bookmarks)) }
|
||||||
|
|
||||||
|
with mock.patch('builtins.input', return_value='y'):
|
||||||
|
self.bdb.append_tag_at_index(0, new_tags)
|
||||||
|
# updated tags for each bookmark
|
||||||
|
from_db = [ (i, self.bdb.get_bookmark_by_index(i)[3]) for i in inclusive_range(1, len(self.bookmarks)) ]
|
||||||
|
for index, tagset in from_db:
|
||||||
|
# checking if new tags added to bookmark
|
||||||
|
self.assertTrue(all( x in tagset.split(',') for x in new_tags.split(',') ))
|
||||||
|
# checking if old tags still exist for boomark
|
||||||
|
self.assertTrue(all( x in tagset.split(',') for x in old_tagsets[index].split(',') ))
|
||||||
|
|
||||||
|
|
||||||
|
# @unittest.skip('skipping')
|
||||||
|
def test_delete_tag_at_index(self):
|
||||||
|
# adding bookmarks
|
||||||
|
for bookmark in self.bookmarks:
|
||||||
|
self.bdb.add_bookmark(*bookmark)
|
||||||
|
|
||||||
|
inclusive_range = lambda start, end: range(start, end + 1)
|
||||||
|
# dictionary of db bookmark index: tags
|
||||||
|
tags_by_index = { i: self.bdb.get_bookmark_by_index(i)[3] for i in inclusive_range(1, len(self.bookmarks)) }
|
||||||
|
|
||||||
|
for i, tags in tags_by_index.items():
|
||||||
|
# get the first tag from the bookmark
|
||||||
|
to_delete = re.match(',.*?,', tags).group(0)
|
||||||
|
self.bdb.delete_tag_at_index(i, to_delete)
|
||||||
|
# get updated tags from db
|
||||||
|
from_db = self.bdb.get_bookmark_by_index(i)[3]
|
||||||
|
self.assertNotIn(to_delete, from_db)
|
||||||
|
|
||||||
|
# @unittest.skip('skipping')
|
||||||
|
def test_refreshdb(self):
|
||||||
|
self.bdb.add_bookmark("https://www.google.com/ncr", "?")
|
||||||
|
self.bdb.refreshdb(1)
|
||||||
|
from_db = self.bdb.get_bookmark_by_index(1)
|
||||||
self.assertEqual(from_db[2], "Google")
|
self.assertEqual(from_db[2], "Google")
|
||||||
|
|
||||||
# def test_searchdb(self):
|
# def test_searchdb(self):
|
||||||
@ -203,67 +237,62 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_delete_bookmark(self):
|
def test_delete_bookmark(self):
|
||||||
bdb = BukuDb()
|
|
||||||
# adding bookmark and getting index
|
# adding bookmark and getting index
|
||||||
bdb.add_bookmark(*self.bookmarks[0])
|
self.bdb.add_bookmark(*self.bookmarks[0])
|
||||||
index = bdb.get_bookmark_index(self.bookmarks[0][0])
|
index = self.bdb.get_bookmark_index(self.bookmarks[0][0])
|
||||||
# deleting bookmark
|
# deleting bookmark
|
||||||
bdb.delete_bookmark(index)
|
self.bdb.delete_bookmark(index)
|
||||||
# asserting it doesn't exist
|
# asserting it doesn't exist
|
||||||
from_db = bdb.get_bookmark_by_index(index)
|
from_db = self.bdb.get_bookmark_by_index(index)
|
||||||
self.assertIsNone(from_db)
|
self.assertIsNone(from_db)
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_delete_bookmark_yes(self):
|
def test_delete_bookmark_yes(self):
|
||||||
bdb = BukuDb()
|
|
||||||
# checking that "y" response causes delete_bookmark to return True
|
# checking that "y" response causes delete_bookmark to return True
|
||||||
with mock.patch('builtins.input', return_value='y'):
|
with mock.patch('builtins.input', return_value='y'):
|
||||||
self.assertTrue(bdb.delete_bookmark(0))
|
self.assertTrue(self.bdb.delete_bookmark(0))
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_delete_bookmark_no(self):
|
def test_delete_bookmark_no(self):
|
||||||
bdb = BukuDb()
|
|
||||||
# checking that non-"y" response causes delete_bookmark to return None
|
# checking that non-"y" response causes delete_bookmark to return None
|
||||||
with mock.patch('builtins.input', return_value='n'):
|
with mock.patch('builtins.input', return_value='n'):
|
||||||
self.assertIsNone(bdb.delete_bookmark(0))
|
self.assertIsNone(self.bdb.delete_bookmark(0))
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_delete_all_bookmarks(self):
|
def test_delete_all_bookmarks(self):
|
||||||
bdb = BukuDb()
|
|
||||||
# adding bookmarks
|
# adding bookmarks
|
||||||
bdb.add_bookmark(*self.bookmarks[0])
|
self.bdb.add_bookmark(*self.bookmarks[0])
|
||||||
# deleting all bookmarks
|
# deleting all bookmarks
|
||||||
bdb.delete_all_bookmarks()
|
self.bdb.delete_all_bookmarks()
|
||||||
# assert table has been dropped
|
# assert table has been dropped
|
||||||
with self.assertRaises(sqlite3.OperationalError):
|
with self.assertRaises(sqlite3.OperationalError):
|
||||||
bdb.get_bookmark_by_index(0)
|
self.bdb.get_bookmark_by_index(0)
|
||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_replace_tag(self):
|
def test_replace_tag(self):
|
||||||
bdb = BukuDb()
|
|
||||||
indices = []
|
indices = []
|
||||||
for bookmark in self.bookmarks:
|
for bookmark in self.bookmarks:
|
||||||
# adding bookmark, getting index
|
# adding bookmark, getting index
|
||||||
bdb.add_bookmark(*bookmark)
|
self.bdb.add_bookmark(*bookmark)
|
||||||
index = bdb.get_bookmark_index(bookmark[0])
|
index = self.bdb.get_bookmark_index(bookmark[0])
|
||||||
indices += [index]
|
indices += [index]
|
||||||
# replacing tags
|
# replacing tags
|
||||||
bdb.replace_tag("news", ["__01"])
|
self.bdb.replace_tag("news", ["__01"])
|
||||||
bdb.replace_tag("zażółć", ["__02,__03"])
|
self.bdb.replace_tag("zażółć", ["__02,__03"])
|
||||||
# replacing tag which is also a substring of other tag
|
# replacing tag which is also a substring of other tag
|
||||||
bdb.replace_tag("es", ["__04"])
|
self.bdb.replace_tag("es", ["__04"])
|
||||||
# removing tags
|
# removing tags
|
||||||
bdb.replace_tag("gęślą")
|
self.bdb.replace_tag("gęślą")
|
||||||
bdb.replace_tag("old")
|
self.bdb.replace_tag("old")
|
||||||
# removing nonexistent tag
|
# removing nonexistent tag
|
||||||
bdb.replace_tag("_")
|
self.bdb.replace_tag("_")
|
||||||
# removing nonexistent tag which is also a substring of other tag
|
# removing nonexistent tag which is also a substring of other tag
|
||||||
bdb.replace_tag("e")
|
self.bdb.replace_tag("e")
|
||||||
|
|
||||||
for url, title, _, _ in self.bookmarks:
|
for url, title, _, _ in self.bookmarks:
|
||||||
# retrieving from db
|
# retrieving from db
|
||||||
index = bdb.get_bookmark_index(url)
|
index = self.bdb.get_bookmark_index(url)
|
||||||
from_db = bdb.get_bookmark_by_index(index)
|
from_db = self.bdb.get_bookmark_by_index(index)
|
||||||
# asserting tags were replaced
|
# asserting tags were replaced
|
||||||
if title == "SLASHDOT":
|
if title == "SLASHDOT":
|
||||||
self.assertEqual(from_db[3], parse_tags(["__01"]))
|
self.assertEqual(from_db[3], parse_tags(["__01"]))
|
||||||
@ -277,15 +306,14 @@ class TestBukuDb(unittest.TestCase):
|
|||||||
|
|
||||||
# @unittest.skip('skipping')
|
# @unittest.skip('skipping')
|
||||||
def test_close_quit(self):
|
def test_close_quit(self):
|
||||||
bdb = BukuDb()
|
|
||||||
# quitting with no args
|
# quitting with no args
|
||||||
try:
|
try:
|
||||||
bdb.close_quit()
|
self.bdb.close_quit()
|
||||||
except SystemExit as err:
|
except SystemExit as err:
|
||||||
self.assertEqual(err.args[0], 0)
|
self.assertEqual(err.args[0], 0)
|
||||||
# quitting with custom arg
|
# quitting with custom arg
|
||||||
try:
|
try:
|
||||||
bdb.close_quit(1)
|
self.bdb.close_quit(1)
|
||||||
except SystemExit as err:
|
except SystemExit as err:
|
||||||
self.assertEqual(err.args[0], 1)
|
self.assertEqual(err.args[0], 1)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user