def test_remember_curr_place(self): # Test whether the curr place in history will be remembered between runs. # Make some interesting history. a = "file:///home/trentm/a.txt" b = "file:///home/trentm/current.txt" locs = [ self.history.note_loc(Location(a, 10 * (i + 1), 0)) for i in range(10) ] locs.insert(0, None) # Make locs 1-based current_loc = Location(b, 55, 0) loc_back = self.history.go_back(current_loc, 4) self.assertEqual(loc_back, locs[7]) # Test history before the restart: h = list(self.history.recent_history(loc_back)) for i in range(1, 10): self.assertEqual(h[i][1], locs[11 - i]) # Simulate a restart. self.history.close() self.history = History(self._db_path_) h = list(self.history.recent_history(loc_back)) # Test boundary conditions, then check all in a loop. self.assertEqual(h[0][1], current_loc) # 4 fwd self.assertEqual(h[1][1], locs[10]) # 3 fwd self.assertEqual(h[3][1], locs[8]) # 1 fwd self.assertEqual(h[4][1], locs[7]) # here self.assertEqual(h[5][1], locs[6]) # 1 back h = list(self.history.recent_history(loc_back)) for i in range(1, 10): self.assertEqual(h[i][1], locs[11 - i])
def test_reloading_db(self): """ Build a database with locations from two URIs interleaved. Step one hop at a time until we find a "volatile" URI, and remove it. Verify the remaining visits. Reload the database, verifying that we find all the same items. """ uri_stable = "file:///home/tester/stable.txt" uri_volatile = "file:///home/tester/volatile.txt" uri_current = "file:///home/tester/current.txt" # Create 50 entries in the database, every 3rd is the volatile URI num_items_to_create = 50 volatile_factor = 3 uris = [uri_stable, uri_stable, uri_volatile] locs = [ self.history.note_loc( Location(uris[i % volatile_factor], 10 * (i + 1), 0)) for i in range(num_items_to_create) ] current_loc = Location(uri_current, num_items_to_create + 1, 0) locs.append(current_loc) loc = self.history.go_back(current_loc, 2) self.assertEqual(loc.uri, uri_stable) self.assertEqual(loc.line, 490) current_loc = loc # We want to make 1 step back into a location we'll remove. loc = self.history.go_back(current_loc, 1) self.assertEqual(loc.uri, uri_volatile) self.assertEqual(loc.line, 480) #self.history.debug_dump_recent_history(loc) self.history.obsolete_uri(loc.uri, 1, True) # => #490 #self.history.debug_dump_recent_history(current_loc) v = list(self.history.recent_history(current_loc)) # Make assertions about the culled list vlen = len(v) for i in range((vlen - 1) / 2): self.assertEqual(v[1 + (2 * i)][1], locs[49 - 0 - (3 * i)]) self.assertEqual(v[2 + (2 * i)][1], locs[49 - 1 - (3 * i)]) self.assertEqual(current_loc.line, 490) # Now save the database, reload, and verify some facts about the # new database self.history.close() self.history = History(self._db_path_) self.assertEqual(current_loc.line, 490) #self.history.debug_dump_recent_history(current_loc) # Recheck history v = list(self.history.recent_history(current_loc)) # Make the same assertions about the culled list vlen = len(v) for i in range((vlen - 1) / 2): self.assertEqual(v[1 + (2 * i)][1], locs[49 - 0 - (3 * i)]) self.assertEqual(v[2 + (2 * i)][1], locs[49 - 1 - (3 * i)])
def setUp(self): frame = sys._getframe(1) meth = frame.f_locals["testMethod"] name = meth.__name__ self._db_path_ = join(dirname(self._db_path_), name + ".sqlite") if not exists(dirname(self._db_path_)): os.makedirs(dirname(self._db_path_)) if exists(self._db_path_): os.remove(self._db_path_) self.history = History(self._db_path_)
def test_two_sessions_basic(self): a = "file:///home/tester/a.txt" b = "file:///home/tester/b.txt" locs = ([ self.history.note_loc( Location(a, (i + 1) * 10, 1, session_name="1")) for i in range(10) ] + [ self.history.note_loc( Location(b, 100 + (i + 1) * 10, 1, session_name="2")) for i in range(10) ]) cw1 = "file:///home/tester/cw1.txt" cw2 = "file:///home/tester/cw2.txt" curr_loc_cw1 = Location(cw1, 110, 1, session_name="1") curr_loc_cw2 = Location(cw2, 210, 1, session_name="2") new_loc_w1 = self.history.go_back(curr_loc_cw1, 3) self.assertEqual(new_loc_w1, locs[7]) new_loc_w2 = self.history.go_back(curr_loc_cw2, 7) self.assertEqual(new_loc_w2, locs[13]) self.assertNotEqual(new_loc_w2, locs[14]) # Verify they're different visits_pre_close = [ self.history.get_session("1").recent_back_visits, self.history.get_session("1").forward_visits, self.history.get_session("2").recent_back_visits, self.history.get_session("2").forward_visits, ] self.assertEqual(len(visits_pre_close[0]), 7) self.assertEqual(len(visits_pre_close[1]), 3) self.assertEqual(len(visits_pre_close[2]), 3) self.assertEqual(len(visits_pre_close[3]), 7) # And verify that after we do a close and reopen, the caches are maintained # correctly for each session. self.history.close() self.history = History(self._db_path_) visits_post_close = [ self.history.get_session("1").recent_back_visits, self.history.get_session("1").forward_visits, self.history.get_session("2").recent_back_visits, self.history.get_session("2").forward_visits, ] self.assertEqual(visits_post_close, visits_pre_close)
def test_cull_unvisited_locations(self): # Assumes that we keep the first 500 locations. uri_base = "file:///home/tester/file%04x" num_items_to_create = 505 for i in range(num_items_to_create): uri = uri_base % i self.history.note_loc(Location(uri, i + 1, 0)) self.history.close() # Now reopen self.history = History(self._db_path_) # Verify that both history_uri and history_visit contain # 500 items limit = self.history.db.URIS_TO_KEEP with self.history.db.connect(True) as cu: cu.execute("select count(*) from history_visit") count = int(cu.fetchone()[0]) self.assertEqual( count, limit, "Expected %d locs in mem:history_visit, got %d" % (limit, count)) cu.execute("select count(*) from history_uri") count = int(cu.fetchone()[0]) self.assertEqual( count, limit, "Expected %d locs in mem:history_uri, got %d" % (limit, count)) with self.history.db.connect(True, inMemory=False) as cu: cu.execute("select count(*) from history_visit") count = int(cu.fetchone()[0]) self.assertEqual( count, limit, "Expected %d locs in file:history_visit, got %d" % (limit, count)) cu.execute("select count(*) from history_uri") count = int(cu.fetchone()[0]) self.assertEqual( count, limit, "Expected %d locs in file:history_uri, got %d" % (limit, count))