Merge "Add unit tests for subunit2sql.shell.main"
This commit is contained in:
commit
8af81fea4a
@ -14,10 +14,15 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
|
import fixtures
|
||||||
import mock
|
import mock
|
||||||
|
from oslo_config import cfg
|
||||||
|
|
||||||
from subunit2sql import exceptions
|
from subunit2sql import exceptions
|
||||||
|
from subunit2sql.migrations import cli as migration_cli
|
||||||
from subunit2sql import shell
|
from subunit2sql import shell
|
||||||
from subunit2sql.tests import base
|
from subunit2sql.tests import base
|
||||||
|
|
||||||
@ -130,3 +135,62 @@ class TestShell(base.TestCase):
|
|||||||
}
|
}
|
||||||
self.assertRaises(exceptions.UnknownStatus,
|
self.assertRaises(exceptions.UnknownStatus,
|
||||||
shell.increment_counts, fake_test, fake_result)
|
shell.increment_counts, fake_test, fake_result)
|
||||||
|
|
||||||
|
|
||||||
|
class TestMain(base.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
super(TestMain, self).setUp()
|
||||||
|
cfg.CONF.reset()
|
||||||
|
cfg.CONF.unregister_opt(migration_cli.command_opt)
|
||||||
|
self.fake_args = ['subunit2sql']
|
||||||
|
self.useFixture(fixtures.MonkeyPatch('sys.argv', self.fake_args))
|
||||||
|
|
||||||
|
@mock.patch('subunit2sql.read_subunit.ReadSubunit')
|
||||||
|
@mock.patch('subunit2sql.shell.process_results')
|
||||||
|
def test_main(self, process_results_mock, read_subunit_mock):
|
||||||
|
fake_read_subunit = mock.MagicMock('ReadSubunit')
|
||||||
|
fake_get_results = 'fake results'
|
||||||
|
fake_read_subunit.get_results = mock.MagicMock('get_results')
|
||||||
|
fake_read_subunit.get_results.return_value = fake_get_results
|
||||||
|
read_subunit_mock.return_value = fake_read_subunit
|
||||||
|
shell.main()
|
||||||
|
read_subunit_mock.assert_called_once_with(sys.stdin,
|
||||||
|
attachments=False,
|
||||||
|
attr_regex='\[(.*)\]')
|
||||||
|
process_results_mock.assert_called_once_with(fake_get_results)
|
||||||
|
|
||||||
|
@mock.patch('subunit2sql.read_subunit.ReadSubunit')
|
||||||
|
@mock.patch('subunit2sql.shell.process_results')
|
||||||
|
def test_main_multiple_files(self, process_results_mock,
|
||||||
|
read_subunit_mock):
|
||||||
|
tfile1 = tempfile.NamedTemporaryFile()
|
||||||
|
tfile2 = tempfile.NamedTemporaryFile()
|
||||||
|
tfile1.write('test me later 1')
|
||||||
|
tfile2.write('test me later 2')
|
||||||
|
tfile1.flush()
|
||||||
|
tfile2.flush()
|
||||||
|
self.fake_args.extend([tfile1.name, tfile2.name])
|
||||||
|
fake_read_subunit = mock.MagicMock('ReadSubunit')
|
||||||
|
fake_get_results_1 = 'fake results 1'
|
||||||
|
fake_get_results_2 = 'fake results 2'
|
||||||
|
fake_read_subunit.get_results = mock.MagicMock('get_results')
|
||||||
|
fake_read_subunit.get_results.side_effect = [fake_get_results_1,
|
||||||
|
fake_get_results_2]
|
||||||
|
read_subunit_mock.return_value = fake_read_subunit
|
||||||
|
shell.main()
|
||||||
|
read_subunit_mock.assert_called_with(mock.ANY,
|
||||||
|
attachments=False,
|
||||||
|
attr_regex='\[(.*)\]')
|
||||||
|
self.assertEqual(2, len(read_subunit_mock.call_args_list))
|
||||||
|
file_1 = read_subunit_mock.call_args_list[0][0][0]
|
||||||
|
self.assertIsInstance(file_1, file)
|
||||||
|
file_1.seek(0)
|
||||||
|
self.assertEqual('test me later 1', file_1.read())
|
||||||
|
file_2 = read_subunit_mock.call_args_list[1][0][0]
|
||||||
|
self.assertIsInstance(file_2, file)
|
||||||
|
file_2.seek(0)
|
||||||
|
self.assertEqual('test me later 2', file_2.read())
|
||||||
|
self.assertEqual(fake_get_results_1,
|
||||||
|
process_results_mock.call_args_list[0][0][0])
|
||||||
|
self.assertEqual(fake_get_results_2,
|
||||||
|
process_results_mock.call_args_list[1][0][0])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user