File: UnitTestDbMolSupplier.py

package info (click to toggle)
rdkit 202009.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 129,624 kB
  • sloc: cpp: 288,030; python: 75,571; java: 6,999; ansic: 5,481; sql: 1,968; yacc: 1,842; lex: 1,254; makefile: 572; javascript: 461; xml: 229; fortran: 183; sh: 134; cs: 93
file content (81 lines) | stat: -rw-r--r-- 2,975 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
73
74
75
76
77
78
79
80
81
"""unit testing code for the DbResultSet object

"""

import random
import unittest

from rdkit import RDConfig, Chem
from rdkit.Chem.Suppliers.DbMolSupplier import ForwardDbMolSupplier, RandomAccessDbMolSupplier
from rdkit.Chem.Suppliers.MolSupplier import MolSupplier
from rdkit.Dbase.DbConnection import DbConnect
from rdkit.Dbase.DbResultSet import DbResultSet, RandomAccessDbResultSet


class TestCase(unittest.TestCase):

    def setUp(self):
        self.dbName = RDConfig.RDTestDatabase
        self.conn = DbConnect(self.dbName)
        self.curs = self.conn.GetCursor()

    def test_MolSupplier(self):
        self.assertRaises(ValueError, MolSupplier)

    def test_general(self):
        # Check for a molecule column
        cmd = 'select * from ten_elements'
        results = DbResultSet(self.curs, self.conn, cmd)
        self.assertRaises(ValueError, ForwardDbMolSupplier, results)

    def test_ForwardDbMolSupplier(self):
        cmd = 'select * from simple_mols order by ID'
        results = DbResultSet(self.curs, self.conn, cmd)
        expected = list(results)

        results = DbResultSet(self.curs, self.conn, cmd)
        supp = ForwardDbMolSupplier(results)
        self.assertEqual(supp.GetColumnNames(), ('ID',))

        for smiles, mol in zip(expected, supp):
            self.assertEqual(Chem.MolToSmiles(Chem.MolFromSmiles(smiles[0])), Chem.MolToSmiles(mol))
            self.assertEqual(smiles[1], mol.GetProp('ID'))
        self.assertRaises(StopIteration, next, supp)

        # We can not use an index for ForwardDbMolSupplier
        with self.assertRaises(TypeError):
            supp[0]

    def test_RandomAccessDbMolSupplier(self):
        cmd = 'select * from simple_mols order by ID'
        results = RandomAccessDbResultSet(self.curs, self.conn, cmd)
        expected = list(results)

        results = RandomAccessDbResultSet(self.curs, self.conn, cmd)
        supp = RandomAccessDbMolSupplier(results)
        self.assertEqual(len(supp), len(expected))
        self.assertEqual(supp.GetColumnNames(), ('ID',))
        for smiles, mol in zip(expected, supp):
            self.assertEqual(Chem.MolToSmiles(Chem.MolFromSmiles(smiles[0])), Chem.MolToSmiles(mol))
            self.assertEqual(smiles[1], mol.GetProp('ID'))

        # Check that we can randomly access the data
        indices = list(range(len(expected)))
        random.shuffle(indices)
        for idx in indices:
            smiles = expected[idx]
            mol = supp[idx]
            self.assertEqual(Chem.MolToSmiles(Chem.MolFromSmiles(smiles[0])), Chem.MolToSmiles(mol))
            self.assertEqual(smiles[1], mol.GetProp('ID'))

        # We get an error if we access outside of the permitted range
        with self.assertRaises(IndexError):
            supp[len(expected)]

        # The DbMolSupplier doesn't support negative indices
        with self.assertRaises(IndexError):
            supp[-1]


if __name__ == '__main__':
    unittest.main()