Debian Codegraph @

test/io/testscripts.py

raw ◀ ▶
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 @unittest.skip("Broken test")
102 def test_main_empty_argv(self):
103 """Does main() fail as expected if called without arguments?"""
104 with self.assertRaises(SystemExit):
105 cclib.scripts.ccframe.main()
106
107 @mock.patch(
108 "cclib.scripts.ccframe.sys.argv",
109 ["ccframe", INPUT_FILE]
110 )
111 @mock.patch("cclib.io.ccio._has_pandas", False)
112 def test_main_without_pandas(self):
113 """Does ccframe fail if Pandas can't be imported?"""
114 with six.assertRaisesRegex(
115 self, ImportError, "You must install `pandas` to use this function"
116 ):
117 cclib.scripts.ccframe.main()
118
119 @mock.patch(
120 "cclib.scripts.ccframe.sys.argv",
121 ["ccframe", INPUT_FILE]
122 )
123 @mock.patch("cclib.io.ccio._has_pandas", True)
124 def test_main(self):
125 """Is ccframe called with the given parameters?"""
126 with mock.patch('sys.stdout') as mock_stdout:
127 cclib.scripts.ccframe.main()
128 self.assertEqual(mock_stdout.write.call_count, 2)
129 df, newline = mock_stdout.write.call_args_list
130 if isinstance(df[0][0], mock.MagicMock):
131 self.assertEqual(df[0][0].name, 'mock.DataFrame()')
132 else:
133 # TODO: this is what we really should be testing
134 pass
135 self.assertEqual(newline[0][0], '\n')
136
137
138 if __name__ == "__main__":
139 unittest.main()