diff --git a/soitool/database.py b/soitool/database.py index 316bff25a963dd7c049e6f15fbda11545e23862b..c6c3ed880c87de0a36075434527ff5865d76e76d 100644 --- a/soitool/database.py +++ b/soitool/database.py @@ -38,7 +38,7 @@ class Database: Holds a QTimer that requests an update of CodeBook on every timeout. """ - def __init__(self): + def __init__(self, auto_update=True): db_exists = os.path.exists(DBPATH) if db_exists: @@ -55,12 +55,13 @@ class Database: print("Tables filled with data.") # Initiate timer that triggers CodeBook update - # self.timer = QTimer() - # self.timer.setInterval( - # self.seconds_to_next_update(SECONDS_IN_24H * 1000) - # ) - # self.timer.timeout.connect(lambda: self.update_codebook_auto()) - # self.timer.start() + if auto_update: + self.timer = QTimer() + self.timer.setInterval( + self.seconds_to_next_update(SECONDS_IN_24H * 1000) + ) + self.timer.timeout.connect(lambda: self.update_codebook_auto()) + self.timer.start() self.conn.row_factory = sqlite3.Row # Enables row['columnName'] @@ -247,16 +248,16 @@ class Database: # Since QTimer does not handle negative values if seconds_to_update <= 0: return 0 - else: - return seconds_to_update + + return seconds_to_update def update_codebook_auto(self): """Update Codebook if needed and set new time for timer.""" if self.seconds_to_next_update(SECONDS_IN_24H) < 10: self.update_codebook() - # self.timer.setInterval( - # self.seconds_to_next_update(SECONDS_IN_24H * 1000) - # ) + self.timer.setInterval( + self.seconds_to_next_update(SECONDS_IN_24H * 1000) + ) def add_code_to(self, word, mode="ascii"): """ diff --git a/test/test_database.py b/test/test_database.py index d46977dfc96fa68a16dd8000cd5cdcff1ada4ec8..3af90bc2a6505b4cd4e641684319d86d2e18847e 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -16,7 +16,7 @@ class DatabaseTest(unittest.TestCase): def setUp(self): """Connect to/create database.""" - self.database = Database() + self.database = Database(auto_update=False) def test_connection(self): """Assert connection is not None.""" @@ -203,18 +203,17 @@ class DatabaseTest(unittest.TestCase): def test_update_codebook(self): """Test that the codes get updated.""" - # Get number of entries - stmt = "SELECT COUNT(*) FROM CodeBook" - number_of_entries = self.database.conn.execute(stmt).fetchall()[0][0] - # Get old and updated word-code combinations + # Get entries before and after update stmt = "SELECT Word, Code FROM CodeBook ORDER BY Word" - old = self.database.conn.execute(stmt).fetchall() + old_entries = self.database.conn.execute(stmt).fetchall() self.database.update_codebook() - updated = self.database.conn.execute(stmt).fetchall() - # Collect approximately score of not updated pairs + new_entries = self.database.conn.execute(stmt).fetchall() + # Collect approximately score of not updated pairs since there is a + # chance for a word to get the same code again pairs = 0 + number_of_entries = len(old_entries) for i in range(0, number_of_entries, 2): - if old[i]["Code"] == updated[i]["Code"]: + if old_entries[i]["Code"] == new_entries[i]["Code"]: pairs = pairs + 1 # Test that at least some of the test are new self.assertTrue(pairs < number_of_entries)