<s> """<STR_LIT>""" <EOL> import json <EOL> import unittest <EOL> from mock import Mock , patch <EOL> from freezer . apiclient import exceptions <EOL> from freezer . apiclient import jobs <EOL> class TestJobManager ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_client = Mock ( ) <EOL> self . mock_response = Mock ( ) <EOL> self . mock_client . endpoint = '<STR_LIT>' <EOL> self . mock_client . auth_token = '<STR_LIT>' <EOL> self . headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> self . mock_client . client_id = '<STR_LIT>' <EOL> self . job_manager = jobs . JobManager ( self . mock_client ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create ( self , mock_requests ) : <EOL> self . assertEqual ( '<STR_LIT>' , self . job_manager . endpoint ) <EOL> self . assertEqual ( { '<STR_LIT>' : '<STR_LIT>' } , self . job_manager . headers ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_ok ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> retval = self . job_manager . create ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( '<STR_LIT>' , retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_adds_client_id_if_not_provided ( self , mock_requests , mock_json ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_json . dumps . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> retval = self . job_manager . create ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> mock_json . dumps . assert_called_with ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( '<STR_LIT>' , retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_leaves_provided_client_id ( self , mock_requests , mock_json ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_json . dumps . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> retval = self . job_manager . create ( { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> mock_json . dumps . assert_called_with ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( '<STR_LIT>' , retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_create_fail_when_api_return_error_code ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> mock_requests . post . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . create , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_delete_ok ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> mock_requests . delete . return_value = self . mock_response <EOL> retval = self . job_manager . delete ( '<STR_LIT>' ) <EOL> self . assertIsNone ( retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_delete_fail ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> mock_requests . delete . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . delete , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_ok ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT:200> <EOL> self . mock_response . json . return_value = { '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_requests . get . return_value = self . mock_response <EOL> retval = self . job_manager . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT>' : '<STR_LIT>' } , retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_fails_on_error_different_from_404 ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> mock_requests . get . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . get , '<STR_LIT>' ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_get_none ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> mock_requests . get . return_value = self . mock_response <EOL> retval = self . job_manager . get ( '<STR_LIT>' ) <EOL> self . assertIsNone ( retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_list_ok ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT:200> <EOL> job_list = [ { '<STR_LIT>' : '<STR_LIT>' } , { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> self . mock_response . json . return_value = { '<STR_LIT>' : job_list } <EOL> mock_requests . get . return_value = self . mock_response <EOL> retval = self . job_manager . list ( ) <EOL> self . assertEqual ( job_list , retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_list_error ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> job_list = [ { '<STR_LIT>' : '<STR_LIT>' } , { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> self . mock_response . json . return_value = { '<STR_LIT>' : job_list } <EOL> mock_requests . get . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . list ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_update_ok ( self , mock_requests ) : <EOL> self . mock_response . status_code = <NUM_LIT:200> <EOL> self . mock_response . json . return_value = { <EOL> "<STR_LIT>" : { "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> "<STR_LIT:version>" : <NUM_LIT:12> , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> mock_requests . patch . return_value = self . mock_response <EOL> retval = self . job_manager . update ( '<STR_LIT>' , { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( <NUM_LIT:12> , retval ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_update_raise_MetadataUpdateFailure_when_api_return_error_code ( self , mock_requests ) : <EOL> self . mock_response . json . return_value = { <EOL> "<STR_LIT>" : { "<STR_LIT:status>" : "<STR_LIT>" } , <EOL> "<STR_LIT:version>" : <NUM_LIT:12> , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . text = '<STR_LIT>' <EOL> mock_requests . patch . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . update , <EOL> '<STR_LIT>' , { '<STR_LIT:status>' : '<STR_LIT>' } ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_start_job_posts_proper_data ( self , mock_requests ) : <EOL> job_id = '<STR_LIT>' <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT:result>' : '<STR_LIT:success>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> endpoint = '<STR_LIT>' . format ( self . mock_client . endpoint , job_id ) <EOL> data = { "<STR_LIT:start>" : None } <EOL> retval = self . job_manager . start_job ( job_id ) <EOL> self . assertEqual ( { '<STR_LIT:result>' : '<STR_LIT:success>' } , retval ) <EOL> args = mock_requests . post . call_args [ <NUM_LIT:0> ] <EOL> kwargs = mock_requests . post . call_args [ <NUM_LIT:1> ] <EOL> self . assertEquals ( endpoint , args [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( data , json . loads ( kwargs [ '<STR_LIT:data>' ] ) ) <EOL> self . assertEquals ( self . headers , kwargs [ '<STR_LIT>' ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_start_job_raise_ApiClientException_when_api_return_error_code ( self , mock_requests ) : <EOL> job_id = '<STR_LIT>' <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT:result>' : '<STR_LIT:success>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . start_job , job_id ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_stop_job_posts_proper_data ( self , mock_requests ) : <EOL> job_id = '<STR_LIT>' <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT:result>' : '<STR_LIT:success>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> endpoint = '<STR_LIT>' . format ( self . mock_client . endpoint , job_id ) <EOL> data = { "<STR_LIT>" : None } <EOL> retval = self . job_manager . stop_job ( job_id ) <EOL> self . assertEqual ( { '<STR_LIT:result>' : '<STR_LIT:success>' } , retval ) <EOL> args = mock_requests . post . call_args [ <NUM_LIT:0> ] <EOL> kwargs = mock_requests . post . call_args [ <NUM_LIT:1> ] <EOL> self . assertEquals ( endpoint , args [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( data , json . loads ( kwargs [ '<STR_LIT:data>' ] ) ) <EOL> self . assertEquals ( self . headers , kwargs [ '<STR_LIT>' ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_stop_job_raise_ApiClientException_when_api_return_error_code ( self , mock_requests ) : <EOL> job_id = '<STR_LIT>' <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT:result>' : '<STR_LIT:success>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . start_job , job_id ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_abort_job_posts_proper_data ( self , mock_requests ) : <EOL> job_id = '<STR_LIT>' <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT:result>' : '<STR_LIT:success>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> endpoint = '<STR_LIT>' . format ( self . mock_client . endpoint , job_id ) <EOL> data = { "<STR_LIT>" : None } <EOL> retval = self . job_manager . abort_job ( job_id ) <EOL> self . assertEqual ( { '<STR_LIT:result>' : '<STR_LIT:success>' } , retval ) <EOL> args = mock_requests . post . call_args [ <NUM_LIT:0> ] <EOL> kwargs = mock_requests . post . call_args [ <NUM_LIT:1> ] <EOL> self . assertEquals ( endpoint , args [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( data , json . loads ( kwargs [ '<STR_LIT:data>' ] ) ) <EOL> self . assertEquals ( self . headers , kwargs [ '<STR_LIT>' ] ) <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_abort_job_raise_ApiClientException_when_api_return_error_code ( self , mock_requests ) : <EOL> job_id = '<STR_LIT>' <EOL> self . mock_response . status_code = <NUM_LIT> <EOL> self . mock_response . json . return_value = { '<STR_LIT:result>' : '<STR_LIT:success>' } <EOL> mock_requests . post . return_value = self . mock_response <EOL> self . assertRaises ( exceptions . ApiClientException , self . job_manager . abort_job , job_id ) </s>
