Skip to content
Snippets Groups Projects
Commit b0aa994d authored by morkolai's avatar morkolai
Browse files

#52 modus for auto oppdatering i db-klasse og optimalisering av test_update_codebook + div

parent 022e7a5c
No related branches found
No related tags found
1 merge request!27#52 Automatisk oppdatering av kodebok basert på tid
Pipeline #74996 failed
......@@ -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"):
"""
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment