Blob Blame History Raw
From 762146ea0bd810020e17006ad957c57c18886af2 Mon Sep 17 00:00:00 2001
From: ctSkennerton <c.skennerton@gmail.com>
Date: Sat, 14 May 2016 15:28:30 -0700
Subject: [PATCH] Split online tests for BioSQL

We need internet access for NCBI Entrez to load taxonomy
information automatically.

Based on a pull request from @ctSkennerton which was updated
by @peterjc following changes on the master branch, with some
painful workarounds for global BioSQL configuration variables.
---
 Tests/common_BioSQL.py                      |  69 +++--------------
 Tests/common_BioSQL_online.py               | 111 ++++++++++++++++++++++++++++
 Tests/test_BioSQL_MySQLdb.py                |   2 +-
 Tests/test_BioSQL_MySQLdb_online.py         |  23 ++++++
 Tests/test_BioSQL_mysql_connector.py        |   2 +-
 Tests/test_BioSQL_mysql_connector_online.py |  23 ++++++
 Tests/test_BioSQL_psycopg2.py               |   2 +-
 Tests/test_BioSQL_psycopg2_online.py        |  22 ++++++
 Tests/test_BioSQL_sqlite3.py                |   2 +-
 Tests/test_BioSQL_sqlite3_online.py         |  27 +++++++
 10 files changed, 221 insertions(+), 62 deletions(-)
 create mode 100644 Tests/common_BioSQL_online.py
 create mode 100644 Tests/test_BioSQL_MySQLdb_online.py
 create mode 100644 Tests/test_BioSQL_mysql_connector_online.py
 create mode 100644 Tests/test_BioSQL_psycopg2_online.py
 create mode 100644 Tests/test_BioSQL_sqlite3_online.py

diff --git a/Tests/common_BioSQL.py b/Tests/common_BioSQL.py
index 5cc00eb..834bf64 100644
--- a/Tests/common_BioSQL.py
+++ b/Tests/common_BioSQL.py
@@ -174,7 +174,10 @@ def _do_db_create():
 
 
 def create_database():
-    """Delete any existing BioSQL test DB, then (re)create an empty BioSQL DB."""
+    """Delete any existing BioSQL test DB, then (re)create an empty BioSQL DB.
+
+    Returns TESTDB name which will change for for SQLite.
+    """
     if DBDRIVER in ["sqlite3"]:
         global TESTDB
         if os.path.exists(TESTDB):
@@ -208,6 +211,8 @@ def create_database():
         server.close()
         raise
 
+    return TESTDB
+
 
 def destroy_database():
     """Delete any temporary BioSQL sqlite3 database files."""
@@ -222,7 +227,7 @@ def load_database(gb_filename_or_handle):
     This is useful for running tests against a newly created database.
     """
 
-    create_database()
+    TESTDB = create_database()
     # now open a connection to load the database
     db_name = "biosql-test"
     server = BioSeqDatabase.open_database(driver=DBDRIVER,
@@ -245,7 +250,7 @@ def load_multi_database(gb_filename_or_handle, gb_filename_or_handle2):
     This is useful for running tests against a newly created database.
     """
 
-    create_database()
+    TESTDB = create_database()
     # now open a connection to load the database
     db_name = "biosql-test"
     db_name2 = "biosql-test2"
@@ -561,7 +566,7 @@ class LoaderTest(unittest.TestCase):
 
     def setUp(self):
         # create TESTDB
-        create_database()
+        TESTDB = create_database()
 
         # load the database
         db_name = "biosql-test"
@@ -610,58 +615,6 @@ def test_load_database(self):
                                     'M81224.1', 'X55053.1', 'X62281.1'])
 
 
-class TaxonomyTest(unittest.TestCase):
-    """Test proper insertion and retrieval of taxonomy data
-    """
-    def setUp(self):
-        from Bio import Entrez
-        Entrez.email = "biopython-dev@biopython.org"
-        # create TESTDB
-        create_database()
-
-        # load the database
-        db_name = "biosql-test"
-        self.server = BioSeqDatabase.open_database(driver=DBDRIVER,
-                                                   user=DBUSER, passwd=DBPASSWD,
-                                                   host=DBHOST, db=TESTDB)
-
-        # remove the database if it already exists
-        try:
-            self.server[db_name]
-            self.server.remove_database(db_name)
-        except KeyError:
-            pass
-
-        self.db = self.server.new_database(db_name)
-
-        # get the GenBank file we are going to put into it
-        self.iterator = SeqIO.parse("GenBank/cor6_6.gb", "gb")
-
-    def tearDown(self):
-        self.server.close()
-        destroy_database()
-        del self.db
-        del self.server
-
-    def test_taxon_left_right_values(self):
-        self.db.load(self.iterator, True)
-        sql = """SELECT DISTINCT include.ncbi_taxon_id FROM taxon
-                  INNER JOIN taxon AS include ON
-                      (include.left_value BETWEEN taxon.left_value
-                                  AND taxon.right_value)
-                  WHERE taxon.taxon_id IN
-                      (SELECT taxon_id FROM taxon_name
-                                  WHERE name = 'Brassicales')
-                      AND include.right_value - include.left_value = 1"""
-
-        rows = self.db.adaptor.execute_and_fetchall(sql)
-        self.assertEqual(4, len(rows))
-        values = set()
-        for row in rows:
-            values.add(row[0])
-        self.assertEqual(set([3704, 3711, 3708, 3702]), set(values))
-
-
 class DeleteTest(unittest.TestCase):
     """Test proper deletion of entries from a database."""
 
@@ -729,7 +682,7 @@ class DupLoadTest(unittest.TestCase):
 
     def setUp(self):
         # drop any old database and create a new one:
-        create_database()
+        TESTDB = create_database()
         # connect to new database:
         self.server = BioSeqDatabase.open_database(driver=DBDRIVER,
                                                    user=DBUSER, passwd=DBPASSWD,
@@ -880,7 +833,7 @@ class TransferTest(unittest.TestCase):
     # simply a new unique namespace is used for each test.
 
     def setUp(self):
-        create_database()
+        TESTDB = create_database()
 
     def test_NC_005816(self):
         """GenBank file to BioSQL, then again to a new namespace, NC_005816."""
diff --git a/Tests/common_BioSQL_online.py b/Tests/common_BioSQL_online.py
new file mode 100644
index 0000000..47db6c9
--- /dev/null
+++ b/Tests/common_BioSQL_online.py
@@ -0,0 +1,111 @@
+# This code is part of the Biopython distribution and governed by its
+# license.  Please see the LICENSE file that should have been included
+# as part of this package.
+"""Tests for dealing with storage of biopython objects in a relational db.
+"""
+from __future__ import print_function
+
+import os
+import platform
+import unittest
+import tempfile
+import time
+
+from Bio._py3k import StringIO
+from Bio._py3k import zip
+from Bio._py3k import basestring
+
+# Hide annoying warnings from things like bonds in GenBank features,
+# or PostgreSQL schema rules. TODO - test these warnings are raised!
+import warnings
+from Bio import BiopythonWarning
+
+# local stuff
+from Bio import MissingExternalDependencyError
+from Bio.Seq import Seq, MutableSeq
+from Bio.SeqFeature import SeqFeature
+from Bio import Alphabet
+from Bio import SeqIO
+from Bio.SeqRecord import SeqRecord
+
+from BioSQL import BioSeqDatabase
+from BioSQL import BioSeq
+from Bio import Entrez
+
+from common_BioSQL import create_database, destroy_database, check_config
+
+from seq_tests_common import compare_record, compare_records
+
+import requires_internet
+
+if __name__ == "__main__":
+    raise RuntimeError("Call this via test_BioSQL_*online.py not directly")
+
+# Sharing these with test_BioSQL_XXX_online.py files which import this file:
+# DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB, DBSCHEMA, SQL_FILE, SYSTEM
+SYSTEM = platform.system()
+
+
+def share_config(dbdriver, dbtype, dbhost, dbuser, dbpasswd, testdb):
+    """Make sure we can access the DB settings from this file."""
+    global DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB, DBSCHEMA
+    global SYSTEM, SQL_FILE
+    DBDRIVER = dbdriver
+    DBTYPE = dbtype
+    DBHOST = dbhost
+    DBUSER = dbuser
+    DBPASSWD = dbpasswd
+    TESTDB = testdb
+
+
+class TaxonomyTest(unittest.TestCase):
+    """Test proper insertion and retrieval of taxonomy data."""
+    def setUp(self):
+        global DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB, DBSCHEMA
+        global SYSTEM, SQL_FILE
+
+        Entrez.email = "biopython-dev@biopython.org"
+        # create TESTDB
+        TESTDB = create_database()
+
+        # load the database
+        db_name = "biosql-test"
+        self.server = BioSeqDatabase.open_database(driver=DBDRIVER,
+                                                   user=DBUSER, passwd=DBPASSWD,
+                                                   host=DBHOST, db=TESTDB)
+
+        # remove the database if it already exists
+        try:
+            self.server[db_name]
+            self.server.remove_database(db_name)
+        except KeyError:
+            pass
+
+        self.db = self.server.new_database(db_name)
+
+        # get the GenBank file we are going to put into it
+        self.iterator = SeqIO.parse("GenBank/cor6_6.gb", "gb")
+
+    def tearDown(self):
+        self.server.close()
+        destroy_database()
+        del self.db
+        del self.server
+
+    def test_taxon_left_right_values(self):
+        self.db.load(self.iterator, True)
+        sql = """SELECT DISTINCT include.ncbi_taxon_id FROM taxon
+                  INNER JOIN taxon AS include ON
+                      (include.left_value BETWEEN taxon.left_value
+                                  AND taxon.right_value)
+                  WHERE taxon.taxon_id IN
+                      (SELECT taxon_id FROM taxon_name
+                                  WHERE name = 'Brassicales')
+                      AND include.right_value - include.left_value = 1"""
+
+        rows = self.db.adaptor.execute_and_fetchall(sql)
+        self.assertEqual(4, len(rows))
+        values = set()
+        for row in rows:
+            values.add(row[0])
+        self.assertEqual(set([3704, 3711, 3708, 3702]), set(values))
diff --git a/Tests/test_BioSQL_MySQLdb.py b/Tests/test_BioSQL_MySQLdb.py
index 2996e0d..f6abf4f 100644
--- a/Tests/test_BioSQL_MySQLdb.py
+++ b/Tests/test_BioSQL_MySQLdb.py
@@ -19,7 +19,7 @@
 
 # Some of the unit tests don't create their own database,
 # so just in case there is no database already:
-create_database()
+TESTDB = create_database()
 
 if __name__ == "__main__":
     # Run the test cases
diff --git a/Tests/test_BioSQL_MySQLdb_online.py b/Tests/test_BioSQL_MySQLdb_online.py
new file mode 100644
index 0000000..69a4237
--- /dev/null
+++ b/Tests/test_BioSQL_MySQLdb_online.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# This code is part of the Biopython distribution and governed by its
+# license.  Please see the LICENSE file that should have been included
+# as part of this package.
+
+"""Run BioSQL tests using SQLite"""
+
+from common_BioSQL import load_biosql_ini
+from common_BioSQL_online import *
+
+DBDRIVER = 'MySQLdb'
+DBTYPE = 'mysql'
+
+DBHOST, DBUSER, DBPASSWD, TESTDB = load_biosql_ini(DBTYPE)
+
+# This will abort if driver not installed etc:
+check_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+share_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+
+if __name__ == "__main__":
+    # Run the test cases
+    runner = unittest.TextTestRunner(verbosity=2)
+    unittest.main(testRunner=runner)
diff --git a/Tests/test_BioSQL_mysql_connector.py b/Tests/test_BioSQL_mysql_connector.py
index fa1185d..0b3bda8 100644
--- a/Tests/test_BioSQL_mysql_connector.py
+++ b/Tests/test_BioSQL_mysql_connector.py
@@ -15,7 +15,7 @@
 DBHOST, DBUSER, DBPASSWD, TESTDB = load_biosql_ini(DBTYPE)
 
 # This will abort if driver not installed etc:
-check_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+TESTDB = check_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
 
 # Some of the unit tests don't create their own database,
 # so just in case there is no database already:
diff --git a/Tests/test_BioSQL_mysql_connector_online.py b/Tests/test_BioSQL_mysql_connector_online.py
new file mode 100644
index 0000000..a1a5fc3
--- /dev/null
+++ b/Tests/test_BioSQL_mysql_connector_online.py
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# This code is part of the Biopython distribution and governed by its
+# license.  Please see the LICENSE file that should have been included
+# as part of this package.
+
+"""Run BioSQL tests using SQLite"""
+
+from common_BioSQL import load_biosql_ini
+from common_BioSQL_online import *
+
+DBDRIVER = 'mysql.connector'
+DBTYPE = 'mysql'
+
+DBHOST, DBUSER, DBPASSWD, TESTDB = load_biosql_ini(DBTYPE)
+
+# This will abort if driver not installed etc:
+check_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+share_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+
+if __name__ == "__main__":
+    # Run the test cases
+    runner = unittest.TextTestRunner(verbosity=2)
+    unittest.main(testRunner=runner)
diff --git a/Tests/test_BioSQL_psycopg2.py b/Tests/test_BioSQL_psycopg2.py
index bc14248..76472a3 100644
--- a/Tests/test_BioSQL_psycopg2.py
+++ b/Tests/test_BioSQL_psycopg2.py
@@ -18,7 +18,7 @@
 
 # Some of the unit tests don't create their own database,
 # so just in case there is no database already:
-create_database()
+TESTDB = create_database()
 
 if __name__ == "__main__":
     # Run the test cases
diff --git a/Tests/test_BioSQL_psycopg2_online.py b/Tests/test_BioSQL_psycopg2_online.py
new file mode 100644
index 0000000..4e6ddbf
--- /dev/null
+++ b/Tests/test_BioSQL_psycopg2_online.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+# This code is part of the Biopython distribution and governed by its
+# license.  Please see the LICENSE file that should have been included
+# as part of this package.
+
+"""Run BioSQL tests using PostgreSQL"""
+
+from common_BioSQL import load_biosql_ini
+from common_BioSQL_online import *
+
+DBDRIVER = 'psycopg2'
+DBTYPE = 'pg'
+DBHOST, DBUSER, DBPASSWD, TESTDB = load_biosql_ini(DBTYPE)
+
+# This will abort if driver not installed etc:
+check_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+share_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+
+if __name__ == "__main__":
+    # Run the test cases
+    runner = unittest.TextTestRunner(verbosity=2)
+    unittest.main(testRunner=runner)
diff --git a/Tests/test_BioSQL_sqlite3.py b/Tests/test_BioSQL_sqlite3.py
index 0b03c91..678e066 100644
--- a/Tests/test_BioSQL_sqlite3.py
+++ b/Tests/test_BioSQL_sqlite3.py
@@ -26,7 +26,7 @@
 
 # Some of the unit tests don't create their own database,
 # so just in case there is no database already:
-create_database()
+TESTDB = create_database()
 
 
 if False:
diff --git a/Tests/test_BioSQL_sqlite3_online.py b/Tests/test_BioSQL_sqlite3_online.py
new file mode 100644
index 0000000..71dd935
--- /dev/null
+++ b/Tests/test_BioSQL_sqlite3_online.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# This code is part of the Biopython distribution and governed by its
+# license.  Please see the LICENSE file that should have been included
+# as part of this package.
+
+"""Run BioSQL tests using SQLite"""
+
+from common_BioSQL import temp_db_filename
+from common_BioSQL_online import *
+
+# Constants for the database driver
+DBDRIVER = 'sqlite3'
+DBTYPE = 'sqlite'
+
+DBHOST = None
+DBUSER = 'root'
+DBPASSWD = None
+TESTDB = temp_db_filename()
+
+# This will abort if driver not installed etc:
+check_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+share_config(DBDRIVER, DBTYPE, DBHOST, DBUSER, DBPASSWD, TESTDB)
+
+if __name__ == "__main__":
+    # Run the test cases
+    runner = unittest.TextTestRunner(verbosity=2)
+    unittest.main(testRunner=runner)
From 513192e0e88a09ac211263dea02fee82545cfa45 Mon Sep 17 00:00:00 2001
From: Peter Cock <p.j.a.cock@googlemail.com>
Date: Thu, 9 Jun 2016 10:11:59 +0100
Subject: [PATCH] Skip these test with run_tests.py --offline

---
 Tests/test_BioSQL_MySQLdb_online.py         | 3 +++
 Tests/test_BioSQL_mysql_connector_online.py | 3 +++
 Tests/test_BioSQL_psycopg2_online.py        | 3 +++
 Tests/test_BioSQL_sqlite3_online.py         | 3 +++
 4 files changed, 12 insertions(+)

diff --git a/Tests/test_BioSQL_MySQLdb_online.py b/Tests/test_BioSQL_MySQLdb_online.py
index 69a4237..fde9886 100644
--- a/Tests/test_BioSQL_MySQLdb_online.py
+++ b/Tests/test_BioSQL_MySQLdb_online.py
@@ -8,6 +8,9 @@
 from common_BioSQL import load_biosql_ini
 from common_BioSQL_online import *
 
+import requires_internet
+requires_internet.check()
+
 DBDRIVER = 'MySQLdb'
 DBTYPE = 'mysql'
 
diff --git a/Tests/test_BioSQL_mysql_connector_online.py b/Tests/test_BioSQL_mysql_connector_online.py
index a1a5fc3..5b71624 100644
--- a/Tests/test_BioSQL_mysql_connector_online.py
+++ b/Tests/test_BioSQL_mysql_connector_online.py
@@ -8,6 +8,9 @@
 from common_BioSQL import load_biosql_ini
 from common_BioSQL_online import *
 
+import requires_internet
+requires_internet.check()
+
 DBDRIVER = 'mysql.connector'
 DBTYPE = 'mysql'
 
diff --git a/Tests/test_BioSQL_psycopg2_online.py b/Tests/test_BioSQL_psycopg2_online.py
index 4e6ddbf..0fe3859 100644
--- a/Tests/test_BioSQL_psycopg2_online.py
+++ b/Tests/test_BioSQL_psycopg2_online.py
@@ -8,6 +8,9 @@
 from common_BioSQL import load_biosql_ini
 from common_BioSQL_online import *
 
+import requires_internet
+requires_internet.check()
+
 DBDRIVER = 'psycopg2'
 DBTYPE = 'pg'
 DBHOST, DBUSER, DBPASSWD, TESTDB = load_biosql_ini(DBTYPE)
diff --git a/Tests/test_BioSQL_sqlite3_online.py b/Tests/test_BioSQL_sqlite3_online.py
index 71dd935..d173f27 100644
--- a/Tests/test_BioSQL_sqlite3_online.py
+++ b/Tests/test_BioSQL_sqlite3_online.py
@@ -8,6 +8,9 @@
 from common_BioSQL import temp_db_filename
 from common_BioSQL_online import *
 
+import requires_internet
+requires_internet.check()
+
 # Constants for the database driver
 DBDRIVER = 'sqlite3'
 DBTYPE = 'sqlite'