Debian Codegraph @

test/parser/testlogfileparser.py

raw ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (c) 2019, 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 the logfileparser module."""
9
10 import io
11 import os
12 import sys
13 import tempfile
14 import unittest
15
16 from six import add_move, MovedModule
17 add_move(MovedModule('mock', 'mock', 'unittest.mock'))
18 from six.moves import mock
19 from six.moves.urllib.request import urlopen
20
21 import numpy
22
23 import cclib
24
25
26 __filedir__ = os.path.dirname(__file__)
27 __filepath__ = os.path.realpath(__filedir__)
28 __datadir__ = os.path.join(__filepath__, "..", "..")
29
30 class FileWrapperTest(unittest.TestCase):
31
32 def test_file_seek(self):
33 """Can we seek anywhere in a file object?"""
34 fpath = os.path.join(__datadir__,"data/ADF/basicADF2007.01/dvb_gopt.adfout")
35 with open(fpath, 'r') as fobject:
36 wrapper = cclib.parser.logfileparser.FileWrapper(fobject)
37 wrapper.seek(0, 0)
38 self.assertEqual(wrapper.pos, 0)
39 wrapper.seek(10, 0)
40 self.assertEqual(wrapper.pos, 10)
41 wrapper.seek(0, 2)
42 self.assertEqual(wrapper.pos, wrapper.size)
43
44 def test_url_seek(self):
45 """Can we seek only to the end of an url stream?"""
46
47 url = "https://raw.githubusercontent.com/cclib/cclib/master/data/ADF/basicADF2007.01/dvb_gopt.adfout"
48 stream = urlopen(url)
49 wrapper = cclib.parser.logfileparser.FileWrapper(stream)
50
51 # Unfortunately, the behavior of this wrapper differs between Python 2 and 3,
52 # so we need to diverge the assertions. We should try to keep the code as
53 # consistent as possible, but the Errors raised are actually different.
54 wrapper.seek(0, 2)
55 self.assertEqual(wrapper.pos, wrapper.size)
56 if sys.version_info[0] == 2:
57 self.assertRaises(AttributeError, wrapper.seek, 0, 0)
58 self.assertRaises(AttributeError, wrapper.seek, 0, 1)
59 else:
60 self.assertRaises(io.UnsupportedOperation, wrapper.seek, 0, 0)
61 self.assertRaises(io.UnsupportedOperation, wrapper.seek, 0, 1)
62
63 def test_stdin_seek(self):
64 """We shouldn't be able to seek anywhere in standard input."""
65 wrapper = cclib.parser.logfileparser.FileWrapper(sys.stdin)
66 self.assertRaises(IOError, wrapper.seek, 0, 0)
67 self.assertRaises(IOError, wrapper.seek, 0, 1)
68
69 def test_data_stdin(self):
70 """Check that the same attributes are parsed when a file is piped through standard input."""
71 logfiles = [
72 "data/ADF/basicADF2007.01/dvb_gopt.adfout",
73 "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out",
74 ]
75 get_attributes = lambda data: [a for a in data._attrlist if hasattr(data, a)]
76 for lf in logfiles:
77 path = "%s/%s" % (__datadir__, lf)
78 expected_attributes = get_attributes(cclib.io.ccread(path))
79 with open(path) as handle:
80 contents = handle.read()
81 # This is fix strings not being unicode in Python2.
82 try:
83 stdin = io.StringIO(contents)
84 except TypeError:
85 stdin = io.StringIO(unicode(contents))
86 stdin.seek = sys.stdin.seek
87 data = cclib.io.ccread(stdin)
88 self.assertEqual(get_attributes(data), expected_attributes)
89
90
91 class LogfileTest(unittest.TestCase):
92 """Unit tests for the Logfile class."""
93
94 def test_float_basic(self):
95 """Are floats converted from strings correctly?"""
96 float = cclib.parser.logfileparser.Logfile('').float
97 self.assertEqual(float("0.0"), 0.0)
98 self.assertEqual(float("1.0"), 1.0)
99 self.assertEqual(float("-1.0"), -1.0)
100
101 def test_float_numeric_format(self):
102 """Does numeric formatting get converted correctly?"""
103 float = cclib.parser.logfileparser.Logfile('').float
104 self.assertEqual(float("1.2345E+02"), 123.45)
105 self.assertEqual(float("1.2345D+02"), 123.45)
106
107 def test_float_stars(self):
108 """Does the function return nan for stars?"""
109 float = cclib.parser.logfileparser.Logfile('').float
110 self.assertTrue(numpy.isnan(float("*")))
111 self.assertTrue(numpy.isnan(float("*****")))
112
113 def test_normalisesym_base_class_error(self):
114 """Does this method raise an error in the base class?"""
115 normalisesym = cclib.parser.logfileparser.Logfile('').normalisesym
116 self.assertRaises(NotImplementedError, normalisesym, 'Ag')
117
118 def test_parse_check_values(self):
119 """Are custom checks performed after parsing finishes?
120
121 The purpose of this test is not to comprehensively cover all the checks,
122 but rather to make sure the call and logging works. The unit tests
123 for the data class should have comprehensive coverage.
124 """
125 _, path = tempfile.mkstemp()
126 parser = cclib.parser.logfileparser.Logfile(path)
127 parser.extract = lambda self, inputfile, line: None
128 parser.logger = mock.Mock()
129
130 parser.etenergies = [1, -1]
131 parser.parse()
132 parser.logger.error.assert_called_once()
133
134
135 if __name__ == "__main__":
136 unittest.main()