Skip to content
Snippets Groups Projects
Commit 33a38b00 authored by Anders H. Rebner's avatar Anders H. Rebner
Browse files

#7 Småforbedringer, #9 tester for CategoryWords() og get_categories()

parent 16fb1513
No related branches found
No related tags found
1 merge request!10Database setup
Pipeline #70504 failed
......@@ -29,4 +29,7 @@ tags
# Coc configuration directory
.vim
# Database
soitool/Database
__pycache__/
File deleted
......@@ -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()
......@@ -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__':
......
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