Improve unit test coverage for cmd/db_manage.py

Add new unit tests for cmd/db_manage.py.
Increase the coverage for cmd/db_manage.py from 0 to 100%.

Move the subcommand 'command' of db_manage to out side
of method main for easier testing.

Change-Id: I686fbc25fd58aea91b5a862fc61c832f4e0d8684
Partial-Bug: #1511667
This commit is contained in:
Hieu LE 2016-08-12 15:50:29 +07:00
parent 99cfbcbc8b
commit 85d4c68365
2 changed files with 68 additions and 4 deletions

View File

@ -56,11 +56,13 @@ def add_command_parsers(subparsers):
parser.set_defaults(func=do_revision)
command_opt = cfg.SubCommandOpt('command',
title='Command',
help='Available commands',
handler=add_command_parsers)
def main():
command_opt = cfg.SubCommandOpt('command',
title='Command',
help='Available commands',
handler=add_command_parsers)
CONF.register_cli_opt(command_opt)
CONF(project='magnum')

View File

@ -0,0 +1,62 @@
# Copyright 2016 - Fujitsu, Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import mock
import six
from magnum.cmd import db_manage
from magnum.tests import base
class TestMagnumDbManage(base.TestCase):
def setUp(self):
super(TestMagnumDbManage, self).setUp()
def clear_conf():
db_manage.CONF.reset()
db_manage.CONF.unregister_opt(db_manage.command_opt)
clear_conf()
self.addCleanup(clear_conf)
@mock.patch('magnum.db.migration.version')
@mock.patch('sys.argv', ['magnum-db-manage', 'version'])
def test_db_manage_version(self, mock_version):
with mock.patch('sys.stdout', new=six.StringIO()) as fakeOutput:
mock_version.return_value = '123456'
db_manage.main()
self.assertEqual('Current DB revision is 123456\n',
fakeOutput.getvalue())
mock_version.assert_called_once_with()
@mock.patch('magnum.db.migration.upgrade')
@mock.patch('sys.argv', ['magnum-db-manage', 'upgrade'])
def test_db_manage_upgrade(self, mock_upgrade):
db_manage.main()
mock_upgrade.assert_called_once_with(base.CONF.command.revision)
@mock.patch('magnum.db.migration.stamp')
@mock.patch('sys.argv', ['magnum-db-manage', 'stamp', 'foo bar'])
def test_db_manage_stamp(self, mock_stamp):
db_manage.main()
mock_stamp.assert_called_once_with('foo bar')
@mock.patch('magnum.db.migration.revision')
@mock.patch('sys.argv', ['magnum-db-manage', 'revision', '-m', 'foo bar'])
def test_db_manage_revision(self, mock_revision):
db_manage.main()
mock_revision.assert_called_once_with(
message='foo bar',
autogenerate=base.CONF.command.autogenerate)