Browse Source

Add unit tests for shell.py

This commit try to add unit tests for shell.py. Also find a bug
in parse_args if not set the config file.

Change-Id: I73a80b238ec95073fc64698655c045fc88ba352a
Dong Ma 2 years ago
parent
commit
a4ff5c214f
2 changed files with 54 additions and 3 deletions
  1. 3
    3
      coverage2sql/shell.py
  2. 51
    0
      coverage2sql/tests/test_shell.py

+ 3
- 3
coverage2sql/shell.py View File

@@ -57,9 +57,9 @@ def list_opts():
57 57
     return [('DEFAULT', copy.deepcopy(SHELL_OPTS))]
58 58
 
59 59
 
60
-def parse_args(argv):
61
-    CONF(argv[1:], project='coverage2sql', version=_version_)
62
-    CONF(default_config_files=[CONF.config_file])
60
+def parse_args(argv, default_config_files=None):
61
+    CONF(argv[1:], project='coverage2sql', version=_version_,
62
+         default_config_files=default_config_files)
63 63
 
64 64
 
65 65
 def process_results(project_name=".", coverage_rate=0.0):

+ 51
- 0
coverage2sql/tests/test_shell.py View File

@@ -0,0 +1,51 @@
1
+# Copyright (c) 2016 Hewlett Packard Enterprise Development L.P.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+#    http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS,
11
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12
+# implied.
13
+# See the License for the specific language governing permissions and
14
+# limitations under the License.
15
+
16
+import fixtures
17
+import mock
18
+from oslo_config import cfg
19
+import tempfile
20
+
21
+from coverage2sql.migrations import cli as migration_cli
22
+from coverage2sql import shell
23
+from coverage2sql.tests import base
24
+
25
+
26
+class TestMain(base.TestCase):
27
+    def setUp(self):
28
+        super(TestMain, self).setUp()
29
+        cfg.CONF.reset()
30
+        cfg.CONF.unregister_opt(migration_cli.command_opt)
31
+        self.fake_args = ['coverage2sql', 'test']
32
+        self.useFixture(fixtures.MonkeyPatch('sys.argv', self.fake_args))
33
+
34
+    @mock.patch('coverage2sql.read_coverage.ReadCoverage')
35
+    @mock.patch('coverage2sql.shell.process_results')
36
+    def test_main(self, process_results_mock, read_coverage_mock):
37
+        tfile = tempfile.NamedTemporaryFile()
38
+        tfile.write(b'test me later')
39
+        tfile.flush()
40
+        self.fake_args.extend([tfile.name])
41
+        fake_read_coverage = mock.MagicMock('ReadCoverage')
42
+        fake_get_coverage_rate = 'fake coverage rate'
43
+        fake_read_coverage.get_coverage_rate = mock.MagicMock(
44
+            'get_coverage_rate')
45
+        fake_read_coverage.get_coverage_rate.return_value = (
46
+            fake_get_coverage_rate)
47
+        read_coverage_mock.return_value = fake_read_coverage
48
+        shell.main()
49
+        read_coverage_mock.assert_called_with(mock.ANY)
50
+        self.assertEqual(fake_get_coverage_rate,
51
+                         process_results_mock.call_args_list[0][0][1])

Loading…
Cancel
Save