[tor-commits] [gettor/master] Expand tests for database functions in gettor

cohosh at torproject.org cohosh at torproject.org
Fri Feb 21 18:37:12 UTC 2020


commit 64df8b27b36bca24f7612b83697e52becbe3dc32
Author: Cecylia Bocovich <cohosh at torproject.org>
Date:   Wed Feb 19 14:30:19 2020 -0500

    Expand tests for database functions in gettor
---
 README.md        |  4 +++-
 tests/test_db.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index f31d113..498825c 100644
--- a/README.md
+++ b/README.md
@@ -82,5 +82,7 @@ GetTor includes PyTest unit tests. To run the tests, first install the dependenc
 
 
 ```
-$ pytest-3 tests/
+$ python3 scripts/create_db -n -c -o -f tests/gettor.db
+$ python3 scripts/add_links_to_db -f tests/gettor.db
+$ pytest-3 -s -v tests/
 ```
diff --git a/tests/test_db.py b/tests/test_db.py
index e04ca39..7d5634b 100644
--- a/tests/test_db.py
+++ b/tests/test_db.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python3
 import pytest
 import pytest_twisted
+from datetime import datetime
 from twisted.trial import unittest
 
 from . import conftests
@@ -10,7 +11,7 @@ class DatabaseTests(unittest.TestCase):
     # Fail any tests which take longer than 15 seconds.
     timeout = 15
     def setUp(self):
-        self.settings = conftests.options.parse_settings("en","./gettor.conf.json")
+        self.settings = conftests.options.parse_settings("en","tests/test.conf.json")
         self.locales = conftests.strings.get_locales()
 
         self.conn = conftests.SQLite3(self.settings.get("dbname"))
@@ -20,8 +21,55 @@ class DatabaseTests(unittest.TestCase):
         del self.conn
 
     @pytest_twisted.inlineCallbacks
+    def add_dummy_requests(self, num):
+        now_str = datetime.now().strftime("%Y%m%d")
+        for i in (0, num):
+            yield self.conn.new_request(
+                id='testid',
+                command='links',
+                platform='linux',
+                language='en',
+                service='email',
+                date=now_str,
+                status="ONHOLD",
+            )
+
+    @pytest_twisted.inlineCallbacks
     def test_stored_locales(self):
-        locales = yield self.conn.get_locales()
+        locales = []
+        ls = yield self.conn.get_locales()
+        for l in ls:
+            locales.append(l[0])
+        self.assertIn('en-US', locales)
+
+    @pytest_twisted.inlineCallbacks
+    def test_requests(self):
+        now_str = datetime.now().strftime("%Y%m%d")
+        yield self.add_dummy_requests(2)
+        num = yield self.conn.get_num_requests("testid", "email")
+        self.assertEqual(num[0][0], 2)
+
+        requests = yield self.conn.get_requests("ONHOLD", "links", "email")
+        for request in requests:
+            self.assertEqual(request[1], "links")
+            self.assertEqual(request[4], "email")
+            self.assertEqual(request[5], now_str)
+            self.assertEqual(request[6], "ONHOLD")
+        self.assertEqual(len(requests), 2)
+
+        yield self.conn.remove_request("testid", "email", now_str)
+        num = yield self.conn.get_num_requests("testid", "email")
+        self.assertEqual(num[0][0], 0)
+
+    @pytest_twisted.inlineCallbacks
+    def test_links(self):
+        links = yield self.conn.get_links("linux", "en-US", "ACTIVE")
+
+        for link in links:
+            self.assertEqual(link[1], "linux")
+            self.assertEqual(link[2], "en-US")
+            self.assertEqual(link[6], "ACTIVE")
+            self.assertIn(link[5], ["github", "gitlab"])
 
 if __name__ == "__main__":
     unittest.main()



More information about the tor-commits mailing list