Ejemplo n.º 1
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]
Ejemplo n.º 2
0
 def test5(self):
     """ 
 """
     cmd = 'select * from ten_elements_dups'
     set = RandomAccessDbResultSet(self.curs, self.conn, cmd, removeDups=0)
     assert len(set) == 10
     for i in range(len(set)):
         val = set[i]
Ejemplo n.º 3
0
 def test1(self):
     """ test indexing in, ensure acceptable error conditions
 """
     cmd = 'select * from ten_elements'
     set = RandomAccessDbResultSet(self.curs, self.conn, cmd)
     for i in range(12):
         try:
             val = set[i]
         except IndexError:
             assert i >= 10
Ejemplo n.º 4
0
    def test5(self):
        cmd = 'select * from ten_elements_dups'
        resultSet = RandomAccessDbResultSet(self.curs, self.conn, cmd)
        self.assertEqual(len(resultSet), 20)
        r = [obj for obj in resultSet]
        self.assertEqual(len(r), 20)

        resultSet = RandomAccessDbResultSet(self.curs, self.conn, cmd)
        r = [obj for obj in resultSet]
        self.assertEqual(len(r), 20)
        self.assertEqual(len(resultSet), 20)

        resultSet = RandomAccessDbResultSet(self.curs,
                                            self.conn,
                                            cmd,
                                            removeDups=0)
        self.assertEqual(len(resultSet), 10)
        r = [obj for obj in resultSet]
        self.assertEqual(len(r), 10)
Ejemplo n.º 5
0
 def test2(self):
     """ 
 """
     cmd = 'select * from ten_elements'
     set = RandomAccessDbResultSet(self.curs, self.conn, cmd)
     assert len(set) == 10
     for i in range(len(set)):
         try:
             val = set[i]
         except:
             assert 0
Ejemplo n.º 6
0
 def test2(self):
     cmd = 'select * from ten_elements'
     resultSet = RandomAccessDbResultSet(self.curs, self.conn, cmd)
     assert len(resultSet) == 10
     for i in range(len(resultSet)):
         _ = resultSet[i]
Ejemplo n.º 7
0
    def test1(self):
        """ test indexing in, ensure acceptable error conditions """
        cmd = 'select * from ten_elements'
        resultSet = RandomAccessDbResultSet(self.curs, self.conn, cmd)
        self.assertRaises(IndexError, resultSet.__getitem__, -1)
        for i in range(12):
            try:
                _ = resultSet[i]
            except IndexError:
                assert i >= 10
        self.assertEqual(resultSet.GetColumnNames(), ('id', 'val'))
        self.assertEqual(resultSet.GetColumnTypes(), ('integer', 'integer'))
        self.assertEqual(resultSet.GetColumnNamesAndTypes(),
                         (('id', 'integer'), ('val', 'integer')))

        cmd = 'select * from ten_elements_dups'
        resultSet = RandomAccessDbResultSet(self.curs, self.conn, cmd)
        for i in range(22):
            try:
                _ = resultSet[i]
            except IndexError:
                assert i >= 20

        cmd = 'select * from ten_elements_dups'
        resultSet = RandomAccessDbResultSet(self.curs,
                                            self.conn,
                                            cmd,
                                            removeDups=0)
        for i in range(22):
            try:
                _ = resultSet[i]
            except IndexError:
                assert i >= 10

        # Test iterator
        resultSet = RandomAccessDbResultSet(self.curs,
                                            self.conn,
                                            cmd,
                                            removeDups=0)
        self.assertEqual(next(resultSet), resultSet[0])
        self.assertEqual(len(list(resultSet)), 10)