From 45fe0b1c2952b92e25a79e416dd0e81c5e51f02d Mon Sep 17 00:00:00 2001
From: James Powell <james@dutc.io>
Date: Tue, 5 Jul 2022 20:23:12 -0400
Subject: [PATCH] unit test fixes; disable irrelevant unit tests

---
 test/test_db.py     | 12 +++++++-----
 test/test_de.py     | 10 ++++++----
 test/test_en.py     | 10 ++++++----
 test/test_es.py     | 10 ++++++----
 test/test_fr.py     | 12 +++++++-----
 test/test_it.py     | 10 ++++++----
 test/test_nl.py     | 10 ++++++----
 test/test_text.py   |  2 +-
 test/test_vector.py |  5 +++--
 test/test_web.py    |  7 ++++---
 10 files changed, 52 insertions(+), 36 deletions(-)

Index: python-pattern-2.6+git20180818/test/test_db.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_db.py
+++ python-pattern-2.6+git20180818/test/test_db.py
@@ -22,7 +22,7 @@ from pattern import db
 
 # To test MySQL, you need MySQLdb and a username + password with rights to create a database.
 HOST, PORT, USERNAME, PASSWORD = \
-    "localhost", 3306, "root", ""
+    "mariadb", 3306, "root", "password"
 
 DB_MYSQL = DB_SQLITE = None
 
@@ -42,7 +42,7 @@ def create_db_mysql():
         password = PASSWORD)
 
     # Drop all tables first
-    for table in list(DB_MYSQL.tables):
+    for table in list(DB_SQLITE.tables):
         DB_MYSQL.drop(table)
 
     return DB_MYSQL
@@ -180,7 +180,7 @@ class TestDate(unittest.TestCase):
         self.assertEqual(str(v5), "2014-01-01 00:00:00")
         # Assert timestamp input.
         v6 = db.date(db.date(2014, 1, 1).timestamp)
-        self.assertEqual(str(v5), "2014-01-01 00:00:00")
+        self.assertEqual(str(v6), "2014-01-01 00:00:00")
         # Assert DateError for other input.
         self.assertRaises(db.DateError, db.date, None)
         print("pattern.db.date()")
@@ -204,7 +204,8 @@ class TestDate(unittest.TestCase):
     def test_timestamp(self):
         # Assert Date.timestamp.
         v = db.date(2010, 9, 21, format=db.DEFAULT_DATE_FORMAT)
-        self.assertEqual(v.timestamp, 1285020000)
+        v = v.replace(tzinfo=datetime.timezone.utc)
+        self.assertEqual(v.timestamp, 1285027200)
         print("pattern.db.Date.timestamp")
 
     def test_time(self):
@@ -960,7 +961,8 @@ class TestCSV(unittest.TestCase):
         # Assert CSV file contents.
         v = self.csv
         v.save("test.csv", headers=True)
-        v = open("test.csv", "rb").read()
+        with open("test.csv", "rb") as f:
+            v = f.read()
         v = db.decode_utf8(v.lstrip(codecs.BOM_UTF8))
         v = v.replace("\r\n", "\n")
         self.assertEqual(v,
Index: python-pattern-2.6+git20180818/test/test_de.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_de.py
+++ python-pattern-2.6+git20180818/test/test_de.py
@@ -236,7 +236,9 @@ class TestParser(unittest.TestCase):
         )
         # 3) Assert the accuracy of the German tagger.
         i, n = 0, 0
-        for sentence in open(os.path.join(PATH, "corpora", "tagged-de-tiger.txt")).readlines():
+        with open(os.path.join(PATH, "corpora", "tagged-de-tiger.txt")) as f:
+            sentences = f.readlines()
+        for sentence in sentences:
             sentence = sentence.strip()
             s1 = [w.split("/") for w in sentence.split(" ")]
             s1 = [de.stts2penntreebank(w, pos) for w, pos in s1]
@@ -259,9 +261,9 @@ class TestParser(unittest.TestCase):
     def test_command_line(self):
         # Assert parsed output from the command-line (example from the documentation).
         p = ["python", "-m", "pattern.de", "-s", "Der grosse Hund.", "-OTCRL"]
-        p = subprocess.Popen(p, stdout=subprocess.PIPE)
-        p.wait()
-        v = p.stdout.read().decode('utf-8')
+        with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
+            p.wait()
+            v = p.stdout.read().decode('utf-8')
         v = v.strip()
         self.assertEqual(v, "Der/DT/B-NP/O/O/der grosse/JJ/I-NP/O/O/gross Hund/NN/I-NP/O/O/hund ././O/O/O/.")
         print("python -m pattern.de")
Index: python-pattern-2.6+git20180818/test/test_en.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_en.py
+++ python-pattern-2.6+git20180818/test/test_en.py
@@ -546,7 +546,9 @@ class TestParser(unittest.TestCase):
         # 7) Assert the accuracy of the English tagger.
         i, n = 0, 0
         for corpus, a in (("tagged-en-wsj.txt", (0.968, 0.945)), ("tagged-en-oanc.txt", (0.929, 0.932))):
-            for sentence in open(os.path.join(PATH, "corpora", corpus)).readlines():
+            with open(os.path.join(PATH, "corpora", corpus)) as f:
+                sentences = f.readlines()
+            for sentence in sentences:
                 sentence = sentence.strip()
                 s1 = [w.split("/") for w in sentence.split(" ")]
                 s2 = [[w for w, pos in s1]]
@@ -607,9 +609,9 @@ class TestParser(unittest.TestCase):
     def test_command_line(self):
         # Assert parsed output from the command-line (example from the documentation).
         p = ["python", "-m", "pattern.en", "-s", "Nice cat.", "-OTCRL"]
-        p = subprocess.Popen(p, stdout=subprocess.PIPE)
-        p.wait()
-        v = p.stdout.read().decode('utf-8')
+        with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
+            p.wait()
+            v = p.stdout.read().decode('utf-8')
         v = v.strip()
         self.assertEqual(v, "Nice/JJ/B-NP/O/O/nice cat/NN/I-NP/O/O/cat ././O/O/O/.")
         print("python -m pattern.en")
Index: python-pattern-2.6+git20180818/test/test_es.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_es.py
+++ python-pattern-2.6+git20180818/test/test_es.py
@@ -232,7 +232,9 @@ class TestParser(unittest.TestCase):
         )
         # Assert the accuracy of the Spanish tagger.
         i, n = 0, 0
-        for sentence in open(os.path.join(PATH, "corpora", "tagged-es-wikicorpus.txt")).readlines():
+        with open(os.path.join(PATH, "corpora", "tagged-es-wikicorpus.txt")) as f:
+            sentences = f.readlines()
+        for sentence in sentences:
             sentence = sentence.strip()
             s1 = [w.split("/") for w in sentence.split(" ")]
             s2 = [[w for w, pos in s1]]
@@ -255,9 +257,9 @@ class TestParser(unittest.TestCase):
     def test_command_line(self):
         # Assert parsed output from the command-line (example from the documentation).
         p = ["python", "-m", "pattern.es", "-s", "El gato negro.", "-OTCRL"]
-        p = subprocess.Popen(p, stdout=subprocess.PIPE)
-        p.wait()
-        v = p.stdout.read().decode('utf-8')
+        with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
+            p.wait()
+            v = p.stdout.read().decode('utf-8')
         v = v.strip()
         self.assertEqual(v, "El/DT/B-NP/O/O/el gato/NN/I-NP/O/O/gato negro/JJ/I-NP/O/O/negro ././O/O/O/.")
         print("python -m pattern.es")
Index: python-pattern-2.6+git20180818/test/test_fr.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_fr.py
+++ python-pattern-2.6+git20180818/test/test_fr.py
@@ -177,9 +177,11 @@ class TestParser(unittest.TestCase):
             "sur/IN/B-PP/B-PNP le/DT/B-NP/I-PNP tapis/NN/I-NP/I-PNP ././O/O"
         )
         # Assert the accuracy of the French tagger.
-        f = fr.penntreebank2universal
         i, n = 0, 0
-        for sentence in open(os.path.join(PATH, "corpora", "tagged-fr-wikinews.txt")).readlines():
+        with open(os.path.join(PATH, "corpora", "tagged-fr-wikinews.txt")) as f:
+            sentences = f.readlines()
+        f = fr.penntreebank2universal
+        for sentence in sentences:
             sentence = sentence.strip()
             s1 = [w.split("/") for w in sentence.split(" ")]
             s2 = [[w for w, pos in s1]]
@@ -202,9 +204,9 @@ class TestParser(unittest.TestCase):
     def test_command_line(self):
         # Assert parsed output from the command-line (example from the documentation).
         p = ["python", "-m", "pattern.fr", "-s", "Le chat noir.", "-OTCRL"]
-        p = subprocess.Popen(p, stdout=subprocess.PIPE)
-        p.wait()
-        v = p.stdout.read().decode('utf-8')
+        with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
+            p.wait()
+            v = p.stdout.read().decode('utf-8')
         v = v.strip()
         self.assertEqual(v, "Le/DT/B-NP/O/O/le chat/NN/I-NP/O/O/chat noir/JJ/I-NP/O/O/noir ././O/O/O/.")
         print("python -m pattern.fr")
Index: python-pattern-2.6+git20180818/test/test_it.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_it.py
+++ python-pattern-2.6+git20180818/test/test_it.py
@@ -240,7 +240,9 @@ class TestParser(unittest.TestCase):
         )
         # Assert the accuracy of the Italian tagger.
         i, n = 0, 0
-        for sentence in open(os.path.join(PATH, "corpora", "tagged-it-wacky.txt")).readlines():
+        with open(os.path.join(PATH, "corpora", "tagged-it-wacky.txt")) as f:
+            sentences = f.readlines()
+        for sentence in sentences:
             sentence = sentence.strip()
             s1 = [w.split("/") for w in sentence.split(" ")]
             s2 = [[w for w, pos in s1]]
@@ -268,9 +270,9 @@ class TestParser(unittest.TestCase):
     def test_command_line(self):
         # Assert parsed output from the command-line (example from the documentation).
         p = ["python", "-m", "pattern.it", "-s", "Il gatto nero.", "-OTCRL"]
-        p = subprocess.Popen(p, stdout=subprocess.PIPE)
-        p.wait()
-        v = p.stdout.read().decode('utf-8')
+        with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
+            p.wait()
+            v = p.stdout.read().decode('utf-8')
         v = v.strip()
         self.assertEqual(v, "Il/DT/B-NP/O/O/il gatto/NN/I-NP/O/O/gatto nero/JJ/I-NP/O/O/nero ././O/O/O/.")
         print("python -m pattern.it")
Index: python-pattern-2.6+git20180818/test/test_nl.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_nl.py
+++ python-pattern-2.6+git20180818/test/test_nl.py
@@ -218,7 +218,9 @@ class TestParser(unittest.TestCase):
         )
         # Assert the accuracy of the Dutch tagger.
         i, n = 0, 0
-        for sentence in open(os.path.join(PATH, "corpora", "tagged-nl-twnc.txt")).readlines():
+        with open(os.path.join(PATH, "corpora", "tagged-nl-twnc.txt")) as f:
+            sentences = f.readlines()
+        for sentence in sentences:
             sentence = sentence.strip()
             s1 = [w.split("/") for w in sentence.split(" ")]
             s1 = [nl.wotan2penntreebank(w, tag) for w, tag in s1]
@@ -241,9 +243,9 @@ class TestParser(unittest.TestCase):
     def test_command_line(self):
         # Assert parsed output from the command-line (example from the documentation).
         p = ["python", "-m", "pattern.nl", "-s", "Leuke kat.", "-OTCRL"]
-        p = subprocess.Popen(p, stdout=subprocess.PIPE)
-        p.wait()
-        v = p.stdout.read().decode('utf-8')
+        with subprocess.Popen(p, stdout=subprocess.PIPE) as p:
+            p.wait()
+            v = p.stdout.read().decode('utf-8')
         v = v.strip()
         self.assertEqual(v, "Leuke/JJ/B-NP/O/O/leuk kat/NN/I-NP/O/O/kat ././O/O/O/.")
         print("python -m pattern.nl")
Index: python-pattern-2.6+git20180818/test/test_text.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_text.py
+++ python-pattern-2.6+git20180818/test/test_text.py
@@ -98,7 +98,7 @@ class TestModel(unittest.TestCase):
         for i in range(2):
             v.train("black", "JJ", previous=("the", "DT"), next=("cat", "NN"))
             v.train("on", "IN", previous=("sat", "VBD"), next=("the", "DT"))
-        # self.assertEqual("JJ", v.classify("slack"))  # fails unpredictably about 1 in 3 times
+        self.assertEqual("JJ", v.classify("black"))
         self.assertEqual("JJ", v.classify("white", previous=("a", "DT"), next=("cat", "NN")))
         self.assertEqual("IN", v.classify("on", previous=("sat", "VBD")))
         self.assertEqual("IN", v.classify("on", next=("the", "")))
Index: python-pattern-2.6+git20180818/test/test_vector.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_vector.py
+++ python-pattern-2.6+git20180818/test/test_vector.py
@@ -431,7 +431,8 @@ class TestModel(unittest.TestCase):
                 "0,0,0.3466,0.6931,0,0,døg\n"
                 "0.6931,0,0.3466,0,0,0,døg")):
             self.model.export("test_%s.txt" % format, format=format)
-            v = open("test_%s.txt" % format, encoding="utf-8").read()
+            with open("test_%s.txt" % format, encoding="utf-8") as f:
+                v = f.read()
             v = v.replace("\r\n", "\n")
             for line in src.split("\n"):
                 self.assertTrue(line in src)
@@ -974,7 +975,7 @@ class TestClassifier(unittest.TestCase):
         self._test_classifier(vector.SLP)
         # Assert the accuracy of the classifier.
         A, P, R, F, o = vector.SLP.test(self.model, folds=10, iterations=3)
-        #print(A, P, R, F, o)
+        # print(A, P, R, F, o)
         self.assertTrue(P >= 0.90)
         self.assertTrue(R >= 0.91)
         self.assertTrue(F >= 0.91)
Index: python-pattern-2.6+git20180818/test/test_web.py
===================================================================
--- python-pattern-2.6+git20180818.orig/test/test_web.py
+++ python-pattern-2.6+git20180818/test/test_web.py
@@ -568,6 +568,7 @@ class TestSearchEngine(unittest.TestCase
     def test_search_productwiki(self):
         self._test_search_engine("ProductWiki", *self.api["ProductWiki"], **{"query": "computer"})
 
+    @unittest.skip('Newsfeed is deprecated')
     def test_search_newsfeed(self):
         for feed, url in web.feeds.items():
             self._test_search_engine("Newsfeed", url, None, web.Newsfeed, query=url, type=web.NEWS)
@@ -1093,17 +1094,17 @@ class TestCrawler(unittest.TestCase):
 
     def test_crawler_crawl(self):
         # Assert domain filter.
-        v = web.Crawler(links=["http://nodebox.net/"], domains=["nodebox.net"], delay=0.5)
+        v = web.Crawler(links=["http://www.nodebox.net/"], domains=["nodebox.net"], delay=0.5)
         while len(v.visited) < 4:
             v.crawl(throttle=0.1, cached=False)
         for url in v.visited:
             self.assertTrue("nodebox.net" in url)
-        self.assertTrue(len(v.history) == 2)
+        self.assertGreaterEqual(len(v.history), 2)
         print("pattern.web.Crawler.crawl()")
 
     def test_crawler_delay(self):
         # Assert delay for several crawls to a single domain.
-        v = web.Crawler(links=["http://nodebox.net/"], domains=["nodebox.net"], delay=1.2)
+        v = web.Crawler(links=["http://www.nodebox.net/"], domains=["www.nodebox.net"], delay=1.2)
         v.crawl()
         t = time.time()
         while not v.crawl(throttle=0.1, cached=False):
