Debian Codegraph @

test/io/testccio.py

raw ◀ ▶
1 Debian patch touch this file
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2017, the cclib development team
4 #
5 # This file is part of cclib (http://cclib.github.io) and is distributed under
6 # the terms of the BSD 3-Clause License.
7
8 """Unit tests for parser ccio module."""
9
10 import os
11 import sys
12 import tempfile
13 import unittest
14
15 import cclib
16
17 from six import add_move, MovedModule
18 add_move(MovedModule('mock', 'mock', 'unittest.mock'))
19 from six.moves import mock
20
21 from six import StringIO
22
23
24 __filedir__ = os.path.dirname(__file__)
25 __filepath__ = os.path.realpath(__filedir__)
26 __datadir__ = os.path.join(__filepath__, "..", "..")
27
28
29 class guess_fileypeTest(unittest.TestCase):
30
31 def setUp(self):
32 self.guess = cclib.io.ccio.guess_filetype
33
34 def test_fail(self):
35 """Does the function fail as expected?"""
36 self.assertIsNone(self.guess([]))
37 self.assertIsNone(self.guess(None))
38 self.assertIsNone(self.guess(os.devnull))
39 self.assertIsNone(self.guess(['test', 'random', 'quantum chemistry']))
40
41 def test_programs(self):
42 """Does the function catch programs as expected?"""
43 self.assertEqual(self.guess(["Amsterdam Density Functional"]), cclib.parser.ADF)
44 self.assertEqual(self.guess(['Dalton - An Electronic Structure Program']), cclib.parser.DALTON)
45 self.assertEqual(self.guess(['GAMESS']), cclib.parser.GAMESS)
46 self.assertEqual(self.guess(['G A M E S S - U K']), cclib.parser.GAMESSUK)
47 self.assertEqual(self.guess(['Gaussian, Inc.']), cclib.parser.Gaussian)
48 self.assertEqual(self.guess(['Jaguar']), cclib.parser.Jaguar)
49 self.assertEqual(self.guess(['PROGRAM SYSTEM MOLPRO']), cclib.parser.Molpro)
50 self.assertEqual(self.guess(['MOPAC2016']), cclib.parser.MOPAC)
51 self.assertEqual(self.guess(['Northwest Computational Chemistry Package']), cclib.parser.NWChem)
52 self.assertEqual(self.guess(['O R C A']), cclib.parser.ORCA)
53 self.assertEqual(self.guess(["PSI3: An Open-Source Ab Initio Electronic Structure Package"]), cclib.parser.Psi3)
54 self.assertEqual(self.guess(["Psi4: An Open-Source Ab Initio Electronic Structure Package"]), cclib.parser.Psi4)
55 self.assertEqual(self.guess(['A Quantum Leap Into The Future Of Chemistry']), cclib.parser.QChem)
56
57
58 class ccreadTest(unittest.TestCase):
59
60 def setUp(self):
61 self.ccread = cclib.io.ccio.ccread
62
63 def test_fail(self):
64 """Does the function fail as expected?"""
65 self.assertIsNone(self.ccread("", quiet=True))
66 self.assertIsNone(self.ccread([], quiet=True))
67 self.assertIsNone(self.ccread(None, quiet=True))
68
69
70 class ccopenTest(unittest.TestCase):
71
72 def setUp(self):
73 self.ccopen = cclib.io.ccio.ccopen
74
75 def test_ccopen_fail(self):
76 """Does the function fail as expected?"""
77 self.assertIsNone(self.ccopen("", quiet=True))
78 self.assertIsNone(self.ccopen([], quiet=True))
79 self.assertIsNone(self.ccopen(None, quiet=True))
80
81 def test_list_of_filenames(self):
82 """Can we ccopen a list of filenames (https://github.com/cclib/cclib/issues/350)?"""
83 absdir = os.path.dirname(os.path.abspath(__file__))
84 rootdir = os.path.join(os.sep, *absdir.split(os.sep)[:-2])
85 molprodir = os.path.join(rootdir, "data", "Molpro", "basicMolpro2012")
86 filenames = ["dvb_gopt.out", "dvb_gopt.log"]
87 filepaths = [os.path.join(molprodir, fn) for fn in filenames]
88 self.assertIsNotNone(self.ccopen(filepaths))
89 self.assertIsNotNone(self.ccopen(filepaths).parse())
90
91 def test_cjson_empty_tempfile(self):
92 """Do we get a CJSON object when the keyword argument used?"""
93 with tempfile.NamedTemporaryFile() as tf:
94 self.assertIsInstance(self.ccopen(tf.name, cjson=True), cclib.io.cjsonreader.CJSON)
95
•96 @unittest.skipIf(os.environ.get('CCLIB_SKIP_NETWORK_TESTS', False),
97 "Skipping network tests")
98 def test_url_io(self):
99 """Does the function works with URLs such good as with filenames?"""
100 fpath = os.path.join(__datadir__, "data")
101 base_url = "https://raw.githubusercontent.com/cclib/cclib/master/data/"
102 filenames = ["QChem/basicQChem5.1/dvb_td.out", "Molpro/basicMolpro2012/h2o_mp2.out"]
103 for fname in filenames:
104 self.assertEqual(self.ccopen(os.path.join(fpath, fname), quiet=True).parse().getattributes(tolists=True),
105 self.ccopen(base_url + fname, quiet=True).parse().getattributes(tolists=True))
106
•107 @unittest.skipIf(os.environ.get('CCLIB_SKIP_NETWORK_TESTS', False),
108 "Skipping network tests")
109 def test_multi_url_io(self):
110 """Does the function works with multiple URLs such good as with multiple filenames?"""
111 fpath = os.path.join(__datadir__, "data")
112 base_url = "https://raw.githubusercontent.com/cclib/cclib/master/data/"
113 filenames = ["Molpro/basicMolpro2012/dvb_gopt.out", "Molpro/basicMolpro2012/dvb_gopt.log"]
114 self.assertEqual(
115 self.ccopen([os.path.join(fpath, fname) for fname in filenames], quiet=True)
116 .parse().getattributes(tolists=True),
117 self.ccopen([base_url + fname for fname in filenames], quiet=True)
118 .parse().getattributes(tolists=True))
119
120 @unittest.skip("This should also work if cjsonreader supported streams.")
121 def test_cjson(self):
122 """Do we get a CJSON object then keyword argument used?"""
123 self.assertIsInstance(self.ccopen(StringIO(""), cjson=True), cclib.io.cjsonreader.CJSON)
124
125
126 class _determine_output_formatTest(unittest.TestCase):
127
128 def setUp(self):
129 self._determine_output_format = cclib.io.ccio._determine_output_format
130 self.UnknownOutputFormatError = cclib.io.ccio.UnknownOutputFormatError
131
132 def test_outputclass(self):
133 """Does the function determine output class as expected."""
134 outputtype = "xyz"
135 outputdest = "file.xyz"
136 self.assertEqual(self._determine_output_format(outputtype, outputdest),
137 cclib.io.xyzwriter.XYZ)
138 # Must raise a KeyError for unsuported extensions
139 self.assertRaises(self.UnknownOutputFormatError,
140 self._determine_output_format, 'ext', outputdest)
141 self.assertRaises(self.UnknownOutputFormatError,
142 self._determine_output_format, None, None)
143
144
145 class fallbackTest(unittest.TestCase):
146
147 def setUp(self):
148 self.fallback = cclib.io.ccio.fallback
149
150 def test_fallback_fail(self):
151 """Does the function fail as expected?"""
152 self.assertIsNone(self.fallback(None))
153
154
155 class ccframeTest(unittest.TestCase):
156
157 @mock.patch("cclib.io.ccio._has_pandas", False)
158 def test_ccframe_call_without_pandas(self):
159 """Does ccframe fails cleanly if Pandas can't be imported?"""
160 with self.assertRaisesRegexp(
161 ImportError, "You must install `pandas` to use this function"
162 ):
163 cclib.io.ccio.ccframe([])
164
165
166 if __name__ == "__main__":
167 unittest.main()