From 038e822b490d362317d17da0c78d307788ff85e9 Mon Sep 17 00:00:00 2001 From: morkolai <nikolai-mork@live.no> Date: Wed, 18 Mar 2020 10:55:00 +0100 Subject: [PATCH] #52 konsistente variabelnavn + fjerning av import --- soitool/database.py | 19 +++++++++---------- test/test_database.py | 14 ++++++++------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/soitool/database.py b/soitool/database.py index 1339f91..98f79d4 100644 --- a/soitool/database.py +++ b/soitool/database.py @@ -3,7 +3,6 @@ import os import sqlite3 import json from datetime import datetime -from PySide2.QtCore import QTimer import soitool.coder # Set name and path to (future) database @@ -101,35 +100,35 @@ class Database: def fill_by_heart(self): """Read data from ByHeart.txt and fill DB-table ByHeart.""" file_path = os.path.join(CURDIR, "testdata/ByHeart.txt") - f = open(file_path, "r", encoding="utf-8") + file = open(file_path, "r", encoding="utf-8") # Loop through words on file and insert them into ByHeart-table stmt = "INSERT INTO ByHeart(Word) VALUES(?)" - for expr in f: + for expr in file: self.conn.execute(stmt, (expr.rstrip(),)) - f.close() + file.close() def fill_category_words(self): """Read data from CategoryWords.txt and fill DB-table CategoryWords.""" file_path = os.path.join(CURDIR, "testdata/CategoryWords.txt") - f = open(file_path, "r", encoding="utf-8") + file = open(file_path, "r", encoding="utf-8") # Get number of categories on file - no_of_categories = int(f.readline().rstrip()) + no_of_categories = int(file.readline().rstrip()) # Loop through categories on file for _ in range(no_of_categories): # Get category and number of words in category - line = f.readline().split(", ") + line = file.readline().split(", ") category = line[0] no_of_words = int(line[1].rstrip()) # Loop through words in category and add rows to DB stmt = "INSERT INTO CategoryWords(Word, Category) VALUES(?, ?)" for _ in range(no_of_words): - word = f.readline().rstrip() + word = file.readline().rstrip() self.conn.execute(stmt, (word, category,)) - f.close() + file.close() def fill_last_updated(self): """Fill table with current date and time.""" @@ -247,7 +246,7 @@ class Database: def update_codebook_auto(self, timer): """ Update Codebook if needed and update time for timer. - + Parameters ---------- timer : QTimer diff --git a/test/test_database.py b/test/test_database.py index d9c5723..3ed421a 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -222,11 +222,11 @@ class DatabaseTest(unittest.TestCase): """Test code length gets extended when number of entries makes it.""" three_letter_len = 26 ** 2 stmt_count = "SELECT COUNT(*) FROM CodeBook" - numb_of_entries = self.database.conn.execute(stmt_count).fetchall()[0][ + number_of_entries = self.database.conn.execute(stmt_count).fetchall()[ 0 - ] + ][0] # Codes only gets extended when number of entries pass 26**x - if numb_of_entries == (three_letter_len): + if number_of_entries == (three_letter_len): # Get length of current codes stmt_code = "SELECT Code FROM CodeBook" code_len = len( @@ -238,15 +238,17 @@ class DatabaseTest(unittest.TestCase): self.database.conn.execute(stmt, ("676", "676", None)) self.database.update_codebook() # Check that entry got inserted - numb_of_entries = self.database.conn.execute( + number_of_entries = self.database.conn.execute( stmt_count ).fetchall()[0][0] - self.assertEqual(numb_of_entries, three_letter_len + 1) + self.assertEqual(number_of_entries, three_letter_len + 1) # Test that codes got extended length code_len = len( self.database.conn.execute(stmt_code).fetchall()[0][0] ) - self.assertEqual(code_len, get_code_length_needed(numb_of_entries)) + self.assertEqual( + code_len, get_code_length_needed(number_of_entries) + ) else: print("ERROR: Database is not 675 entries long, cant run test") -- GitLab