diff --git a/.gitignore b/.gitignore index 46e5469eeaa0cdc2749a696de34e248d8efb6aa5..66fea34192c933d69c316b53b3a234e995e4112b 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,7 @@ tags # Coc configuration directory .vim +# Database +soitool/Database + __pycache__/ diff --git a/soitool/Database b/soitool/Database deleted file mode 100644 index 14bb8755fa51dc052bd3fff0bb4eb48b11afbfc4..0000000000000000000000000000000000000000 Binary files a/soitool/Database and /dev/null differ diff --git a/soitool/database.py b/soitool/database.py index 23e7869de19142c2f1b6e1f25e5541bb1c47c724..bdcf53e4c9ce71411b54a8190a3d442aa3d70472 100644 --- a/soitool/database.py +++ b/soitool/database.py @@ -77,7 +77,7 @@ class Database(): # Loop through words on file and insert them into ByHeart-table stmt = 'INSERT INTO ByHeart(Word) VALUES(?)' for word in f: - self.conn.execute(stmt, (word[:-1],)) # Remove \n + self.conn.execute(stmt, (word.rstrip(),)) f.close() def fill_category_words(self): @@ -86,20 +86,19 @@ class Database(): f = open(file_path, "r", encoding='utf-8') # Get number of categories on file - no_of_categories = int(f.readline()[:-1]) # Remove \n + no_of_categories = int(f.readline().rstrip()) # Loop through categories on file for _ in range(no_of_categories): - line = f.readline().split(", ") - # Get category and number of words in category + line = f.readline().split(", ") category = line[0] - no_of_words = int(line[1][:-1]) # Remove \n + no_of_words = int(line[1].rstrip()) - # Loop through words in category + # 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()[:-1] # Remove \n - stmt = 'INSERT INTO CategoryWords(Word, Category) VALUES(?, ?)' + word = f.readline().rstrip() self.conn.execute(stmt, (word, category,)) f.close() @@ -113,12 +112,13 @@ class Database(): Categories """ stmt = 'SELECT DISTINCT Category FROM CategoryWords' - test = self.conn.execute(stmt) + queried = self.conn.execute(stmt) categories = [] - for row in test: + for row in queried: categories.append(row['Category']) return categories -# temp = Database() +if __name__ == '__main__': + temp = Database() diff --git a/test/test_database.py b/test/test_database.py index ff1b2a7d600887dca1d52fad5b5a5547de8fa7a4..6ba8d10642fa556d8b3d85eed80afb47d23bda96 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -5,7 +5,6 @@ import unittest from soitool.database import Database TESTDATA_PATH = Path(__file__).parent.parent/"soitool/testdata" -# testdata_path = __file__[:-21] + "soitool\\testdata\\" class DatabaseTest(unittest.TestCase): @@ -18,7 +17,7 @@ class DatabaseTest(unittest.TestCase): """ def setUp(self): - """Connect to database.""" + """Connect to/create database.""" self.database = Database() def test_connection(self): @@ -26,14 +25,17 @@ class DatabaseTest(unittest.TestCase): self.assertIsNotNone(self.database) def test_by_heart(self): - """Assert contents of table ByHeart in DB matches testdata-file.""" + """Assert contents of table ByHeart in DB matches testdata.""" + # Open and read file: file_path = os.path.join(TESTDATA_PATH, "ByHeart.txt") f = open(file_path, "r", encoding="utf-8") file_content = f.read() + # Retrieve number of expressions and expressions from file: no_of_expr = len(file_content.split("\n")) - 1 # ignore last \n expressions = file_content.split("\n")[:-1] # ignore last \n + # Retrieve expressions from DB: stmt = 'SELECT * FROM ByHeart' queried = self.database.conn.execute(stmt).fetchall() @@ -48,39 +50,58 @@ class DatabaseTest(unittest.TestCase): f.close() def test_category_words(self): - """Rai. - - Assert contents of table CategoryWords in DB matches testdata-file. - """ + """Assert contents of table CategoryWords in DB matches testdata.""" file_path = os.path.join(TESTDATA_PATH, "CategoryWords.txt") f = open(file_path, "r", encoding="utf-8") - file_content = f.read() - # Assert equal amount of categories in table and file - no_of_categories = int(file_content[0]) - categories_db = self.database.get_categories() - self.assertEqual(len(categories_db), no_of_categories) + categories_file = [] + words_file = [] + + # Get number of categories on file + no_of_categories = int(f.readline().rstrip("\\n")) - # Assert equal amount of words in table and file - # Ignore last \n and noOfCategories - no_of_words = len(file_content.split("\n")[:-1]) - no_of_categories - 1 - stmt = 'SELECT COUNT(*) FROM CategoryWords' - no_of_words_db = self.database.conn.execute(stmt).fetchone()[0] - self.assertEqual(no_of_words_db, no_of_words) + # Loop through categories on file + for _ in range(no_of_categories): + # Get category and number of words in category + line = f.readline().split(",") + categories_file.append(line[0]) + no_of_words = int(line[1][:-1]) - # Assert information is equal in table and file - # t do + # Loop through words in category + for _ in range(no_of_words): + words_file.append(f.readline().rstrip()) f.close() + # Assert equal categories in table and file + categories_db = self.database.get_categories() + self.assertEqual(categories_db, categories_file) + + # Assert equal words in table and file + stmt = 'SELECT * FROM CategoryWords' + queried = self.database.conn.execute(stmt).fetchall() + words_db = [queried[x][0] for x in range(len(queried))] + # words_db = queried[:][0] + self.assertEqual(words_db, words_file) + def test_get_categories(self): """Assert function get_categories works as expected.""" - categories = ['Hunderase', 'Bilmerke', 'Land', - 'Tresort', 'President', 'Some'] - categories_db = self.database.get_categories() + file_path = os.path.join(TESTDATA_PATH, "CategoryWords.txt") + f = open(file_path, "r", encoding="utf-8") + + # Get number of categories on file + no_of_categories = int(f.readline().rstrip()) + categories_file = [] + for _ in range(no_of_categories): + line = f.readline().split(", ") + categories_file.append(line[0]) + # Skip all words: + [f.readline() for _ in range(int(line[1].rstrip()))] + + f.close() # Assert categories are equal - # self.assertEqual(len(categories_db), len(categories)) - self.assertEqual(categories_db, categories) + categories_db = self.database.get_categories() + self.assertEqual(categories_file, categories_db) if __name__ == '__main__':