Debian Codegraph @

test/io/testscripts.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch 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 """Unit tests for main scripts (ccget, ccwrite)."""
8
9 from __future__ import print_function
10
11 import os
12 import sys
13 import unittest
14 import six
15 from six import add_move, MovedModule
16 add_move(MovedModule('mock', 'mock', 'unittest.mock'))
17 from six.moves import mock
18
19 import cclib
20
21
22 __filedir__ = os.path.dirname(__file__)
23 __filepath__ = os.path.realpath(__filedir__)
24 __datadir__ = os.path.join(__filepath__, "..", "..", "data")
25
26
27 INPUT_FILE = os.path.join(
28 __datadir__,
29 'ADF/basicADF2007.01/dvb_gopt.adfout'
30 )
31 CJSON_OUTPUT_FILENAME = 'dvb_gopt.cjson'
32
33
34 @mock.patch("cclib.scripts.ccget.ccread")
35 class ccgetTest(unittest.TestCase):
36
37 def setUp(self):
38 try:
39 from cclib.scripts import ccget
40 except ImportError:
41 self.fail("ccget cannot be imported")
42
43 self.main = ccget.ccget
44
45 @mock.patch("cclib.scripts.ccget.sys.argv", ["ccget"])
46 def test_empty_argv(self, mock_ccread):
47 """Does the script fail as expected if called without parameters?"""
48 with self.assertRaises(SystemExit):
49 self.main()
50
51 @mock.patch(
52 "cclib.scripts.ccget.sys.argv",
53 ["ccget", "atomcoords", INPUT_FILE]
54 )
55 def test_ccread_invocation(self, mock_ccread):
56 self.main()
57
58 self.assertEqual(mock_ccread.call_count, 1)
59 ccread_call_args, ccread_call_kwargs = mock_ccread.call_args
60 self.assertEqual(ccread_call_args[0], INPUT_FILE)
61
62
63 @mock.patch("cclib.scripts.ccwrite.ccwrite")
64 class ccwriteTest(unittest.TestCase):
65
66 def setUp(self):
67 try:
68 from cclib.scripts import ccwrite
69 except ImportError:
70 self.fail("ccwrite cannot be imported")
71
72 self.main = ccwrite.main
73
74 @mock.patch('cclib.scripts.ccwrite.sys.argv', ['ccwrite'])
75 def test_empty_argv(self, mock_ccwrite):
76 """Does the script fail as expected if called without parameters?"""
77 with self.assertRaises(SystemExit):
78 self.main()
79
80 @mock.patch(
81 "cclib.scripts.ccwrite.sys.argv",
82 ["ccwrite", "cjson", INPUT_FILE]
83 )
84 def test_ccwrite_call(self, mock_ccwrite):
85 """is ccwrite called with the given parameters?"""
86 self.main()
87
88 self.assertEqual(mock_ccwrite.call_count, 1)
89 ccwrite_call_args, ccwrite_call_kwargs = mock_ccwrite.call_args
90 self.assertEqual(ccwrite_call_args[1], 'cjson')
91 self.assertEqual(ccwrite_call_args[2], CJSON_OUTPUT_FILENAME)
92
93
94 class ccframeTest(unittest.TestCase):
95
96 def setUp(self):
97 # It would be best to test with Pandas and not a mock!
98 if not hasattr(cclib.io.ccio, "pd"):
99 cclib.io.ccio.pd = mock.MagicMock()
100
101 def test_main_empty_argv(self):
102 """Does main() fail as expected if called without arguments?"""
103 with self.assertRaises(SystemExit):
104 cclib.scripts.ccframe.main()
105
106 @mock.patch(
107 "cclib.scripts.ccframe.sys.argv",
108 ["ccframe", INPUT_FILE]
109 )
110 @mock.patch("cclib.io.ccio._has_pandas", False)
111 def test_main_without_pandas(self):
112 """Does ccframe fail if Pandas can't be imported?"""
113 with six.assertRaisesRegex(
114 self, ImportError, "You must install `pandas` to use this function"
115 ):
116 cclib.scripts.ccframe.main()
117
118 @mock.patch(
119 "cclib.scripts.ccframe.sys.argv",
120 ["ccframe", INPUT_FILE]
121 )
122 @mock.patch("cclib.io.ccio._has_pandas", True)
123 def test_main(self):
124 """Is ccframe called with the given parameters?"""
125 with mock.patch('sys.stdout') as mock_stdout:
126 cclib.scripts.ccframe.main()
127 self.assertEqual(mock_stdout.write.call_count, 2)
128 df, newline = mock_stdout.write.call_args_list
129 if isinstance(df[0][0], mock.MagicMock):
130 self.assertEqual(df[0][0].name, 'mock.DataFrame()')
131 else:
132 # TODO: this is what we really should be testing
133 pass
134 self.assertEqual(newline[0][0], '\n')
135
136
137 if __name__ == "__main__":
138 unittest.main()