Debian Codegraph @

test/parser/testlogfileparser.py

raw ◀ ▶
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 @unittest.skipIf(os.environ.get('CCLIB_SKIP_NETWORK_TESTS', False),
45 "Skipping network tests")
46 def test_url_seek(self):
47 """Can we seek only to the end of an url stream?"""
48
49 url = "https://raw.githubusercontent.com/cclib/cclib/master/data/ADF/basicADF2007.01/dvb_gopt.adfout"
50 stream = urlopen(url)
51 wrapper = cclib.parser.logfileparser.FileWrapper(stream)
52
53 # Unfortunately, the behavior of this wrapper differs between Python 2 and 3,
54 # so we need to diverge the assertions. We should try to keep the code as
55 # consistent as possible, but the Errors raised are actually different.
56 wrapper.seek(0, 2)
57 self.assertEqual(wrapper.pos, wrapper.size)
58 if sys.version_info[0] == 2:
59 self.assertRaises(AttributeError, wrapper.seek, 0, 0)
60 self.assertRaises(AttributeError, wrapper.seek, 0, 1)
61 else:
62 self.assertRaises(io.UnsupportedOperation, wrapper.seek, 0, 0)
63 self.assertRaises(io.UnsupportedOperation, wrapper.seek, 0, 1)
64
•65 @unittest.skip("Broken test")
66 def test_stdin_seek(self):
67 """We shouldn't be able to seek anywhere in standard input."""
68 wrapper = cclib.parser.logfileparser.FileWrapper(sys.stdin)
69 self.assertRaises(IOError, wrapper.seek, 0, 0)
70 self.assertRaises(IOError, wrapper.seek, 0, 1)
71
•72 @unittest.skip("Broken test")
73 def test_data_stdin(self):
74 """Check that the same attributes are parsed when a file is piped through standard input."""
75 logfiles = [
76 "data/ADF/basicADF2007.01/dvb_gopt.adfout",
77 "data/GAMESS/basicGAMESS-US2017/C_bigbasis.out",
78 ]
79 get_attributes = lambda data: [a for a in data._attrlist if hasattr(data, a)]
80 for lf in logfiles:
81 path = "%s/%s" % (__datadir__, lf)
82 expected_attributes = get_attributes(cclib.io.ccread(path))
83 with open(path) as handle:
84 contents = handle.read()
85 # This is fix strings not being unicode in Python2.
86 try:
87 stdin = io.StringIO(contents)
88 except TypeError:
89 stdin = io.StringIO(unicode(contents))
90 stdin.seek = sys.stdin.seek
91 data = cclib.io.ccread(stdin)
92 self.assertEqual(get_attributes(data), expected_attributes)
93
94
95 class LogfileTest(unittest.TestCase):
96 """Unit tests for the Logfile class."""
97
98 def test_float_basic(self):
99 """Are floats converted from strings correctly?"""
100 float = cclib.parser.logfileparser.Logfile('').float
101 self.assertEqual(float("0.0"), 0.0)
102 self.assertEqual(float("1.0"), 1.0)
103 self.assertEqual(float("-1.0"), -1.0)
104
105 def test_float_numeric_format(self):
106 """Does numeric formatting get converted correctly?"""
107 float = cclib.parser.logfileparser.Logfile('').float
108 self.assertEqual(float("1.2345E+02"), 123.45)
109 self.assertEqual(float("1.2345D+02"), 123.45)
110
111 def test_float_stars(self):
112 """Does the function return nan for stars?"""
113 float = cclib.parser.logfileparser.Logfile('').float
114 self.assertTrue(numpy.isnan(float("*")))
115 self.assertTrue(numpy.isnan(float("*****")))
116
117 def test_normalisesym_base_class_error(self):
118 """Does this method raise an error in the base class?"""
119 normalisesym = cclib.parser.logfileparser.Logfile('').normalisesym
120 self.assertRaises(NotImplementedError, normalisesym, 'Ag')
121
122 def test_parse_check_values(self):
123 """Are custom checks performed after parsing finishes?
124
125 The purpose of this test is not to comprehensively cover all the checks,
126 but rather to make sure the call and logging works. The unit tests
127 for the data class should have comprehensive coverage.
128 """
129 _, path = tempfile.mkstemp()
130 parser = cclib.parser.logfileparser.Logfile(path)
131 parser.extract = lambda self, inputfile, line: None
132 parser.logger = mock.Mock()
133
134 parser.etenergies = [1, -1]
135 parser.parse()
136 parser.logger.error.assert_called_once()
137
138
139 if __name__ == "__main__":
140 unittest.main()