| 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 |
def test_url_io(self): |
| 97 |
"""Does the function works with URLs such good as with filenames?""" |
| 98 |
fpath = os.path.join(__datadir__, "data") |
| 99 |
base_url = "https://raw.githubusercontent.com/cclib/cclib/master/data/" |
| 100 |
filenames = ["QChem/basicQChem5.1/dvb_td.out", "Molpro/basicMolpro2012/h2o_mp2.out"] |
| 101 |
for fname in filenames: |
| 102 |
self.assertEqual(self.ccopen(os.path.join(fpath, fname), quiet=True).parse().getattributes(tolists=True), |
| 103 |
self.ccopen(base_url + fname, quiet=True).parse().getattributes(tolists=True)) |
| 104 |
|
| 105 |
def test_multi_url_io(self): |
| 106 |
"""Does the function works with multiple URLs such good as with multiple filenames?""" |
| 107 |
fpath = os.path.join(__datadir__, "data") |
| 108 |
base_url = "https://raw.githubusercontent.com/cclib/cclib/master/data/" |
| 109 |
filenames = ["Molpro/basicMolpro2012/dvb_gopt.out", "Molpro/basicMolpro2012/dvb_gopt.log"] |
| 110 |
self.assertEqual( |
| 111 |
self.ccopen([os.path.join(fpath, fname) for fname in filenames], quiet=True) |
| 112 |
.parse().getattributes(tolists=True), |
| 113 |
self.ccopen([base_url + fname for fname in filenames], quiet=True) |
| 114 |
.parse().getattributes(tolists=True)) |
| 115 |
|
| 116 |
@unittest.skip("This should also work if cjsonreader supported streams.") |
| 117 |
def test_cjson(self): |
| 118 |
"""Do we get a CJSON object then keyword argument used?""" |
| 119 |
self.assertIsInstance(self.ccopen(StringIO(""), cjson=True), cclib.io.cjsonreader.CJSON) |
| 120 |
|
| 121 |
|
| 122 |
class _determine_output_formatTest(unittest.TestCase): |
| 123 |
|
| 124 |
def setUp(self): |
| 125 |
self._determine_output_format = cclib.io.ccio._determine_output_format |
| 126 |
self.UnknownOutputFormatError = cclib.io.ccio.UnknownOutputFormatError |
| 127 |
|
| 128 |
def test_outputclass(self): |
| 129 |
"""Does the function determine output class as expected.""" |
| 130 |
outputtype = "xyz" |
| 131 |
outputdest = "file.xyz" |
| 132 |
self.assertEqual(self._determine_output_format(outputtype, outputdest), |
| 133 |
cclib.io.xyzwriter.XYZ) |
| 134 |
# Must raise a KeyError for unsuported extensions |
| 135 |
self.assertRaises(self.UnknownOutputFormatError, |
| 136 |
self._determine_output_format, 'ext', outputdest) |
| 137 |
self.assertRaises(self.UnknownOutputFormatError, |
| 138 |
self._determine_output_format, None, None) |
| 139 |
|
| 140 |
|
| 141 |
class fallbackTest(unittest.TestCase): |
| 142 |
|
| 143 |
def setUp(self): |
| 144 |
self.fallback = cclib.io.ccio.fallback |
| 145 |
|
| 146 |
def test_fallback_fail(self): |
| 147 |
"""Does the function fail as expected?""" |
| 148 |
self.assertIsNone(self.fallback(None)) |
| 149 |
|
| 150 |
|
| 151 |
class ccframeTest(unittest.TestCase): |
| 152 |
|
| 153 |
@mock.patch("cclib.io.ccio._has_pandas", False) |
| 154 |
def test_ccframe_call_without_pandas(self): |
| 155 |
"""Does ccframe fails cleanly if Pandas can't be imported?""" |
| 156 |
with self.assertRaisesRegexp( |
| 157 |
ImportError, "You must install `pandas` to use this function" |
| 158 |
): |
| 159 |
cclib.io.ccio.ccframe([]) |
| 160 |
|
| 161 |
|
| 162 |
if __name__ == "__main__": |
| 163 |
unittest.main() |