<s> import datetime <EOL> import sys <EOL> import tempfile <EOL> import fixtures <EOL> import mock <EOL> from oslo_config import cfg <EOL> from subunit2sql import exceptions <EOL> from subunit2sql . migrations import cli as migration_cli <EOL> from subunit2sql import shell <EOL> from subunit2sql . tests import base <EOL> class TestShell ( base . TestCase ) : <EOL> def test_run_totals ( self ) : <EOL> fake_results = { } <EOL> for num in range ( <NUM_LIT:100> ) : <EOL> test_name = '<STR_LIT>' + str ( num ) <EOL> fake_results [ test_name ] = { '<STR_LIT:status>' : '<STR_LIT:success>' } <EOL> for num in range ( <NUM_LIT:50> ) : <EOL> test_name = '<STR_LIT>' + str ( num ) <EOL> fake_results [ test_name ] = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> for num in range ( <NUM_LIT:16> ) : <EOL> test_name = '<STR_LIT>' + str ( num ) <EOL> fake_results [ test_name ] = { '<STR_LIT:status>' : '<STR_LIT>' } <EOL> totals = shell . get_run_totals ( fake_results ) <EOL> self . assertEqual ( totals [ '<STR_LIT:success>' ] , <NUM_LIT:100> ) <EOL> self . assertEqual ( totals [ '<STR_LIT>' ] , <NUM_LIT:16> ) <EOL> self . assertEqual ( totals [ '<STR_LIT>' ] , <NUM_LIT:50> ) <EOL> def test_running_avg ( self ) : <EOL> fake_test = mock . MagicMock ( ) <EOL> fake_test . success = <NUM_LIT> <EOL> fake_test . run_time = <NUM_LIT> <EOL> fake_result = { <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ) , <EOL> } <EOL> fake_values = { } <EOL> result = shell . running_avg ( fake_test , fake_values , fake_result ) <EOL> expected_avg = ( ( <NUM_LIT> * <NUM_LIT> ) + <NUM_LIT:50> ) / <NUM_LIT> <EOL> self . assertEqual ( expected_avg , result [ '<STR_LIT>' ] ) <EOL> def test_running_avg_no_prev ( self ) : <EOL> fake_test = mock . MagicMock ( ) <EOL> fake_test . success = <NUM_LIT:1> <EOL> fake_test . run_time = None <EOL> fake_result = { <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ) , <EOL> } <EOL> fake_values = { } <EOL> result = shell . running_avg ( fake_test , fake_values , fake_result ) <EOL> expected_avg = <NUM_LIT:50> <EOL> self . assertEqual ( expected_avg , result [ '<STR_LIT>' ] ) <EOL> def test_increment_counts_success ( self ) : <EOL> fake_test = mock . MagicMock ( ) <EOL> fake_test . run_count = <NUM_LIT:15> <EOL> fake_test . success = <NUM_LIT:5> <EOL> fake_test . run_time = <NUM_LIT> <EOL> fake_result = { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ) , <EOL> } <EOL> values = shell . increment_counts ( fake_test , fake_result ) <EOL> self . assertEqual ( values [ '<STR_LIT>' ] , <NUM_LIT:16> ) <EOL> self . assertEqual ( values [ '<STR_LIT:success>' ] , <NUM_LIT:6> ) <EOL> expected_avg = ( ( <NUM_LIT:5> * <NUM_LIT> ) + <NUM_LIT:50> ) / <NUM_LIT:6> <EOL> self . assertEqual ( values [ '<STR_LIT>' ] , expected_avg ) <EOL> def test_increment_counts_failure ( self ) : <EOL> fake_test = mock . MagicMock ( ) <EOL> fake_test . run_count = <NUM_LIT:15> <EOL> fake_test . success = <NUM_LIT:5> <EOL> fake_test . failure = <NUM_LIT:10> <EOL> fake_test . run_time = <NUM_LIT> <EOL> fake_result = { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:50> ) , <EOL> } <EOL> values = shell . increment_counts ( fake_test , fake_result ) <EOL> self . assertEqual ( values [ '<STR_LIT>' ] , <NUM_LIT:16> ) <EOL> self . assertEqual ( values [ '<STR_LIT>' ] , <NUM_LIT:11> ) <EOL> self . assertNotIn ( '<STR_LIT>' , values ) <EOL> def test_increment_counts_skip ( self ) : <EOL> fake_test = mock . MagicMock ( ) <EOL> fake_test . run_count = <NUM_LIT:15> <EOL> fake_test . success = <NUM_LIT:5> <EOL> fake_test . failure = <NUM_LIT:10> <EOL> fake_test . run_time = <NUM_LIT> <EOL> fake_result = { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:2> ) , <EOL> } <EOL> values = shell . increment_counts ( fake_test , fake_result ) <EOL> self . assertEqual ( values , { } ) <EOL> def test_increment_counts_unknown_status ( self ) : <EOL> fake_test = mock . MagicMock ( ) <EOL> fake_result = { <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:0> ) , <EOL> '<STR_LIT>' : datetime . datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:2> ) , <EOL> } <EOL> self . assertRaises ( exceptions . UnknownStatus , <EOL> shell . increment_counts , fake_test , fake_result ) <EOL> class TestMain ( base . TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( TestMain , self ) . setUp ( ) <EOL> cfg . CONF . reset ( ) <EOL> cfg . CONF . unregister_opt ( migration_cli . command_opt ) <EOL> self . fake_args = [ '<STR_LIT>' ] <EOL> self . useFixture ( fixtures . MonkeyPatch ( '<STR_LIT>' , self . fake_args ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_main ( self , process_results_mock , read_subunit_mock ) : <EOL> fake_read_subunit = mock . MagicMock ( '<STR_LIT>' ) <EOL> fake_get_results = '<STR_LIT>' <EOL> fake_read_subunit . get_results = mock . MagicMock ( '<STR_LIT>' ) <EOL> fake_read_subunit . get_results . return_value = fake_get_results <EOL> read_subunit_mock . return_value = fake_read_subunit <EOL> shell . main ( ) <EOL> read_subunit_mock . assert_called_once_with ( sys . stdin , <EOL> attachments = False , <EOL> attr_regex = '<STR_LIT>' , <EOL> targets = [ ] ) <EOL> process_results_mock . assert_called_once_with ( fake_get_results ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_main_multiple_files ( self , process_results_mock , <EOL> read_subunit_mock ) : <EOL> tfile1 = tempfile . NamedTemporaryFile ( ) <EOL> tfile2 = tempfile . NamedTemporaryFile ( ) <EOL> tfile1 . write ( b'<STR_LIT>' ) <EOL> tfile2 . write ( b'<STR_LIT>' ) <EOL> tfile1 . flush ( ) <EOL> tfile2 . flush ( ) <EOL> self . fake_args . extend ( [ tfile1 . name , tfile2 . name ] ) <EOL> fake_read_subunit = mock . MagicMock ( '<STR_LIT>' ) <EOL> fake_get_results_1 = '<STR_LIT>' <EOL> fake_get_results_2 = '<STR_LIT>' <EOL> fake_read_subunit . get_results = mock . MagicMock ( '<STR_LIT>' ) <EOL> fake_read_subunit . get_results . side_effect = [ fake_get_results_1 , <EOL> fake_get_results_2 ] <EOL> read_subunit_mock . return_value = fake_read_subunit <EOL> shell . main ( ) <EOL> read_subunit_mock . assert_called_with ( mock . ANY , <EOL> attachments = False , <EOL> attr_regex = '<STR_LIT>' , <EOL> targets = [ ] ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( read_subunit_mock . call_args_list ) ) <EOL> file_1 = read_subunit_mock . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> file_1 . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( '<STR_LIT>' , file_1 . read ( ) ) <EOL> file_2 = read_subunit_mock . call_args_list [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> file_2 . seek ( <NUM_LIT:0> ) <EOL> self . assertEqual ( '<STR_LIT>' , file_2 . read ( ) ) <EOL> self . assertEqual ( fake_get_results_1 , <EOL> process_results_mock . call_args_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( fake_get_results_2 , <EOL> process_results_mock . call_args_list [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_main_with_targets ( self , process_results_mock , read_subunit_mock , <EOL> ext_mock ) : <EOL> exts = mock . MagicMock ( '<STR_LIT>' ) <EOL> ext_mock . return_value = exts <EOL> exts . map = mock . MagicMock ( '<STR_LIT>' ) <EOL> exts . map . return_value = [ mock . sentinel . extension ] <EOL> fake_read_subunit = mock . MagicMock ( '<STR_LIT>' ) <EOL> fake_get_results = '<STR_LIT>' <EOL> fake_read_subunit . get_results = mock . MagicMock ( '<STR_LIT>' ) <EOL> fake_read_subunit . get_results . return_value = fake_get_results <EOL> read_subunit_mock . return_value = fake_read_subunit <EOL> shell . main ( ) <EOL> read_subunit_mock . assert_called_once_with ( <EOL> sys . stdin , attachments = False , attr_regex = '<STR_LIT>' , <EOL> targets = [ mock . sentinel . extension ] ) <EOL> process_results_mock . assert_called_once_with ( fake_get_results ) </s>
