From 33a38b00038a1fd8729ce16b848bb54d11f12e30 Mon Sep 17 00:00:00 2001
From: unknown <anderhre@stud.ntnu.no>
Date: Tue, 25 Feb 2020 20:51:43 +0100
Subject: [PATCH] =?UTF-8?q?#7=20Sm=C3=A5forbedringer,=20#9=20tester=20for?=
 =?UTF-8?q?=20CategoryWords()=20og=20get=5Fcategories()?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore            |   3 ++
 soitool/Database      | Bin 28672 -> 0 bytes
 soitool/database.py   |  22 ++++++-------
 test/test_database.py |  71 +++++++++++++++++++++++++++---------------
 4 files changed, 60 insertions(+), 36 deletions(-)
 delete mode 100644 soitool/Database

diff --git a/.gitignore b/.gitignore
index 46e5469..66fea34 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
GIT binary patch
literal 0
HcmV?d00001

literal 28672
zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU|?ZjU|?rJ0A{EdOoS1}z(cbz=t-R5
z<^RFJ#%I95AIyK8ZzaD3p8;PL9zCN{qaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3@?
z8UlkL1Uy*T#HFPfGfNVaa#EctJyH{kN}v>*bC9cJh^s<~qmz%T0#r~VJijPKA<Qwz
z*~2kNAt1=p*D)wk!P_-bQ<sfR+)<j5Au}%}wW7E%C$l6qKC!eUAIy)3YL7RBGT3vO
z*~Qh>85<?RHajPlq^9Q=Rf0?}M&YoaxB^8Ar(1LsVCrBl)wE<`6Zcie<vN&hu=VkV
zC|uSeMs{&oS;j_lh)eTRQl0Yivz?(d6RP{5q9iy?M<JxLAXOnVuSCJc)y*-~Cq%(O
zQ=f@V+*uZ<bD_4x8^T-x>dS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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 ff1b2a7..6ba8d10 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__':
-- 
GitLab