File: UnitTestFeatFinderCLI.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 (73 lines) | stat: -rw-r--r-- 2,752 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
from contextlib import contextmanager
import os
import sys
import unittest

from rdkit import RDConfig
from rdkit.Chem import FeatFinderCLI
from io import StringIO


class TestCase(unittest.TestCase):

    def test_FeatFinderCLI(self):
        smilesFile = os.path.join(RDConfig.RDDataDir, 'NCI', 'first_5K.smi')
        featureFile = os.path.join(RDConfig.RDCodeDir, 'Chem', 'Pharm2D', 'test_data',
                                   'BaseFeatures.fdef')
        parser = FeatFinderCLI.initParser()
        cmd = '-n 10 {0} {1}'.format(featureFile, smilesFile)
        with outputRedirect() as (out, err):
            args = parser.parse_args(cmd.split())
            FeatFinderCLI.processArgs(args, parser)
        out = out.getvalue()
        err = err.getvalue()
        self.assertIn('Mol-1', out)
        self.assertIn('Acceptor-SingleAtomAcceptor', out)
        self.assertIn('C(1)', out)
        self.assertNotIn('Mol-11', out)
        self.assertEqual(err, '')

        cmd = '-n 2 -r {0} {1}'.format(featureFile, smilesFile)
        with outputRedirect() as (out, err):
            args = parser.parse_args(cmd.split())
            FeatFinderCLI.processArgs(args, parser)
        out = out.getvalue()
        err = err.getvalue()
        self.assertIn('Mol-1', out)
        self.assertIn('Acceptor-SingleAtomAcceptor:', out)
        self.assertIn('2, 3, 4', out)
        self.assertNotIn('Mol-3', out)
        self.assertEqual(err, '')

    def test_FeatFinderCLIexceptions(self):
        smilesFile = os.path.join(RDConfig.RDDataDir, 'NCI', 'first_5K.smi')
        featureFile = os.path.join(RDConfig.RDCodeDir, 'Chem', 'Pharm2D', 'test_data',
                                   'BaseFeatures.fdef')
        parser = FeatFinderCLI.initParser()
        cmd = '-n 10 {0} {1}'.format(smilesFile, smilesFile)
        with self.assertRaises(SystemExit), outputRedirect() as (_, err):
            args = parser.parse_args(cmd.split())
            FeatFinderCLI.processArgs(args, parser)
        self.assertIn('error', err.getvalue())

        cmd = '-n 10 {0} {1}'.format(featureFile, 'incorrectFilename')
        with self.assertRaises(SystemExit), outputRedirect() as (_, err):
            args = parser.parse_args(cmd.split())
            FeatFinderCLI.processArgs(args, parser)
        self.assertIn('error', err.getvalue())


@contextmanager
def outputRedirect():
    """ Redirect standard output and error to String IO and return """
    try:
        _stdout, _stderr = sys.stdout, sys.stderr
        sys.stdout = sStdout = StringIO()
        sys.stderr = sStderr = StringIO()
        yield (sStdout, sStderr)
    finally:
        sys.stdout, sys.stderr = _stdout, _stderr


if __name__ == '__main__':  # pragma: nocover
    unittest.main()