File: test_BioSQL_sqlite3.py

package info (click to toggle)
python-biopython 1.68%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 46,860 kB
  • ctags: 13,237
  • sloc: python: 160,306; xml: 93,216; ansic: 9,118; sql: 1,208; makefile: 155; sh: 63
file content (72 lines) | stat: -rw-r--r-- 2,462 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/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"""
import os

from Bio import MissingExternalDependencyError
from Bio import SeqIO
from BioSQL import BioSeqDatabase

from common_BioSQL 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)

# Some of the unit tests don't create their own database,
# so just in case there is no database already:
TESTDB = create_database()


if False:
    # This is how I generated test file Tests/BioSQL/cor6_6.db
    # which is test cross-checked with the latest bindings to
    # catch any regressions in how we map GenBank entries to
    # the database.
    assert not os.path.isfile("BioSQL/cor6_6.db")
    server = BioSeqDatabase.open_database(driver=DBDRIVER,
                                          db="BioSQL/cor6_6.db")
    DBSCHEMA = "biosqldb-" + DBTYPE + ".sql"
    SQL_FILE = os.path.join(os.getcwd(), "BioSQL", DBSCHEMA)
    assert os.path.isfile(SQL_FILE), SQL_FILE
    server.load_database_sql(SQL_FILE)
    server.commit()
    db = server.new_database("OLD")
    count = db.load(SeqIO.parse("GenBank/cor6_6.gb", "gb"))
    assert count == 6
    server.commit()
    assert len(db) == 6
    server.close()


class BackwardsCompatibilityTest(unittest.TestCase):
    def test_backwards_compatibility(self):
        """Check can re-use an old BioSQL SQLite3 database."""
        original_records = list(SeqIO.parse("GenBank/cor6_6.gb", "gb"))
        # now open a connection to load the database
        server = BioSeqDatabase.open_database(driver=DBDRIVER,
                                              db="BioSQL/cor6_6.db")
        db = server["OLD"]
        self.assertEqual(len(db), len(original_records))
        # Now read them back...
        biosql_records = [db.lookup(name=rec.name)
                          for rec in original_records]
        # And check they agree
        self.assertTrue(compare_records(original_records, biosql_records))
        server.close()

if __name__ == "__main__":
    # Run the test cases
    runner = unittest.TextTestRunner(verbosity=2)
    unittest.main(testRunner=runner)