<s> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import datetime <EOL> import dateutil . tz <EOL> try : <EOL> from unittest import mock <EOL> except ImportError : <EOL> import mock <EOL> import pytest <EOL> from tdclient import models <EOL> from tdclient . test . test_helper import * <EOL> def setup_function ( function ) : <EOL> unset_environ ( ) <EOL> def test_schema ( ) : <EOL> client = mock . MagicMock ( ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT:type>" , "<STR_LIT>" , status = "<STR_LIT:status>" , url = "<STR_LIT:url>" , debug = "<STR_LIT>" , start_at = "<STR_LIT>" , end_at = "<STR_LIT>" , cpu_time = "<STR_LIT>" , result_size = "<STR_LIT>" , result = "<STR_LIT:result>" , result_url = "<STR_LIT>" , hive_result_schema = [ [ "<STR_LIT>" , "<STR_LIT:string>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] ] , priority = "<STR_LIT>" , retry_limit = "<STR_LIT>" , org_name = "<STR_LIT>" , database = "<STR_LIT>" ) <EOL> assert job . id == "<STR_LIT>" <EOL> assert job . job_id == "<STR_LIT>" <EOL> assert job . type == "<STR_LIT:type>" <EOL> assert job . result_url == "<STR_LIT>" <EOL> assert job . priority == "<STR_LIT>" <EOL> assert job . retry_limit == "<STR_LIT>" <EOL> assert job . org_name == "<STR_LIT>" <EOL> assert job . database == "<STR_LIT>" <EOL> assert job . result_schema == [ [ "<STR_LIT>" , "<STR_LIT:string>" ] , [ "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> def test_job_priority ( ) : <EOL> client = mock . MagicMock ( ) <EOL> assert models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , priority = - <NUM_LIT:2> ) . priority == "<STR_LIT>" <EOL> assert models . Job ( client , "<STR_LIT:2>" , "<STR_LIT>" , "<STR_LIT>" , priority = - <NUM_LIT:1> ) . priority == "<STR_LIT>" <EOL> assert models . Job ( client , "<STR_LIT:3>" , "<STR_LIT>" , "<STR_LIT>" , priority = <NUM_LIT:0> ) . priority == "<STR_LIT>" <EOL> assert models . Job ( client , "<STR_LIT:4>" , "<STR_LIT>" , "<STR_LIT>" , priority = <NUM_LIT:1> ) . priority == "<STR_LIT>" <EOL> assert models . Job ( client , "<STR_LIT:5>" , "<STR_LIT>" , "<STR_LIT>" , priority = <NUM_LIT:2> ) . priority == "<STR_LIT>" <EOL> assert models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , priority = <NUM_LIT> ) . priority == "<STR_LIT>" <EOL> def test_job_wait_success ( ) : <EOL> client = mock . MagicMock ( ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . finished = mock . MagicMock ( side_effect = [ <EOL> False , <EOL> True , <EOL> ] ) <EOL> job . update = mock . MagicMock ( ) <EOL> with mock . patch ( "<STR_LIT>" ) as t_time : <EOL> t_time . side_effect = [ <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> ] <EOL> with mock . patch ( "<STR_LIT>" ) as t_sleep : <EOL> job . wait ( timeout = <NUM_LIT> ) <EOL> assert t_sleep . called <EOL> assert t_time . called <EOL> assert job . finished . called <EOL> assert job . update . called <EOL> def test_job_wait_failure ( ) : <EOL> client = mock . MagicMock ( ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . finished = mock . MagicMock ( return_value = False ) <EOL> job . update = mock . MagicMock ( ) <EOL> with mock . patch ( "<STR_LIT>" ) as t_time : <EOL> t_time . side_effect = [ <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> ] <EOL> with mock . patch ( "<STR_LIT>" ) as t_sleep : <EOL> with pytest . raises ( RuntimeError ) as error : <EOL> job . wait ( timeout = <NUM_LIT> ) <EOL> assert t_sleep . called <EOL> assert t_time . called <EOL> assert job . finished . called <EOL> assert not job . update . called <EOL> def test_job_kill ( ) : <EOL> client = mock . MagicMock ( ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . update = mock . MagicMock ( ) <EOL> job . kill ( ) <EOL> client . kill . assert_called_with ( "<STR_LIT>" ) <EOL> assert job . update . called <EOL> def test_job_result_generator ( ) : <EOL> client = mock . MagicMock ( ) <EOL> def job_result_each ( job_id ) : <EOL> assert job_id == "<STR_LIT>" <EOL> yield [ "<STR_LIT:foo>" , <NUM_LIT> ] <EOL> yield [ "<STR_LIT:bar>" , <NUM_LIT> ] <EOL> yield [ "<STR_LIT>" , <NUM_LIT> ] <EOL> client . job_result_each = job_result_each <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . success = mock . MagicMock ( return_value = True ) <EOL> job . update = mock . MagicMock ( ) <EOL> rows = [ ] <EOL> for row in job . result ( ) : <EOL> rows . append ( row ) <EOL> assert rows == [ [ "<STR_LIT:foo>" , <NUM_LIT> ] , [ "<STR_LIT:bar>" , <NUM_LIT> ] , [ "<STR_LIT>" , <NUM_LIT> ] ] <EOL> assert job . update . called <EOL> def test_job_result_list ( ) : <EOL> client = mock . MagicMock ( ) <EOL> result = [ <EOL> [ "<STR_LIT:foo>" , <NUM_LIT> ] , <EOL> [ "<STR_LIT:bar>" , <NUM_LIT> ] , <EOL> [ "<STR_LIT>" , <NUM_LIT> ] , <EOL> ] <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , result = result ) <EOL> job . success = mock . MagicMock ( return_value = True ) <EOL> job . update = mock . MagicMock ( ) <EOL> rows = [ ] <EOL> for row in job . result ( ) : <EOL> rows . append ( row ) <EOL> assert rows == [ [ "<STR_LIT:foo>" , <NUM_LIT> ] , [ "<STR_LIT:bar>" , <NUM_LIT> ] , [ "<STR_LIT>" , <NUM_LIT> ] ] <EOL> assert job . update . called <EOL> def test_job_result_failure ( ) : <EOL> client = mock . MagicMock ( ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . success = mock . MagicMock ( return_value = False ) <EOL> job . update = mock . MagicMock ( ) <EOL> with pytest . raises ( ValueError ) as error : <EOL> for row in job . result ( ) : <EOL> pass <EOL> assert not job . update . called <EOL> def test_job_result_format_generator ( ) : <EOL> client = mock . MagicMock ( ) <EOL> def job_result_format_each ( job_id , format ) : <EOL> assert job_id == "<STR_LIT>" <EOL> assert format == "<STR_LIT>" <EOL> yield [ "<STR_LIT:foo>" , <NUM_LIT> ] <EOL> yield [ "<STR_LIT:bar>" , <NUM_LIT> ] <EOL> yield [ "<STR_LIT>" , <NUM_LIT> ] <EOL> client . job_result_format_each = job_result_format_each <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . success = mock . MagicMock ( return_value = True ) <EOL> job . update = mock . MagicMock ( ) <EOL> rows = [ ] <EOL> for row in job . result_format ( "<STR_LIT>" ) : <EOL> rows . append ( row ) <EOL> assert rows == [ [ "<STR_LIT:foo>" , <NUM_LIT> ] , [ "<STR_LIT:bar>" , <NUM_LIT> ] , [ "<STR_LIT>" , <NUM_LIT> ] ] <EOL> assert job . update . called <EOL> def test_job_result_format_list ( ) : <EOL> client = mock . MagicMock ( ) <EOL> result = [ <EOL> [ "<STR_LIT:foo>" , <NUM_LIT> ] , <EOL> [ "<STR_LIT:bar>" , <NUM_LIT> ] , <EOL> [ "<STR_LIT>" , <NUM_LIT> ] , <EOL> ] <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , result = result ) <EOL> job . success = mock . MagicMock ( return_value = True ) <EOL> job . update = mock . MagicMock ( ) <EOL> rows = [ ] <EOL> for row in job . result_format ( "<STR_LIT>" ) : <EOL> rows . append ( row ) <EOL> assert rows == [ [ "<STR_LIT:foo>" , <NUM_LIT> ] , [ "<STR_LIT:bar>" , <NUM_LIT> ] , [ "<STR_LIT>" , <NUM_LIT> ] ] <EOL> assert job . update . called <EOL> def test_job_result_format_failure ( ) : <EOL> client = mock . MagicMock ( ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . success = mock . MagicMock ( return_value = False ) <EOL> job . update = mock . MagicMock ( ) <EOL> with pytest . raises ( ValueError ) as error : <EOL> for row in job . result_format ( "<STR_LIT>" ) : <EOL> pass <EOL> assert not job . update . called <EOL> def test_job_finished ( ) : <EOL> def job ( client , status ) : <EOL> stub = models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> stub . _update_progress = mock . MagicMock ( ) <EOL> return stub <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> assert not job ( client , "<STR_LIT>" ) . finished ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . finished ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . finished ( ) <EOL> assert job ( client , "<STR_LIT:success>" ) . finished ( ) <EOL> assert job ( client , "<STR_LIT:error>" ) . finished ( ) <EOL> assert job ( client , "<STR_LIT>" ) . finished ( ) <EOL> def test_job_success ( ) : <EOL> def job ( client , status ) : <EOL> stub = models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> stub . _update_progress = mock . MagicMock ( ) <EOL> return stub <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> assert not job ( client , "<STR_LIT>" ) . success ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . success ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . success ( ) <EOL> assert job ( client , "<STR_LIT:success>" ) . success ( ) <EOL> assert not job ( client , "<STR_LIT:error>" ) . success ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . success ( ) <EOL> def test_job_error ( ) : <EOL> def job ( client , status ) : <EOL> stub = models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> stub . _update_progress = mock . MagicMock ( ) <EOL> return stub <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> assert not job ( client , "<STR_LIT>" ) . error ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . error ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . error ( ) <EOL> assert not job ( client , "<STR_LIT:success>" ) . error ( ) <EOL> assert job ( client , "<STR_LIT:error>" ) . error ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . error ( ) <EOL> def test_job_killed ( ) : <EOL> def job ( client , status ) : <EOL> stub = models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> stub . _update_progress = mock . MagicMock ( ) <EOL> return stub <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> assert not job ( client , "<STR_LIT>" ) . killed ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . killed ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . killed ( ) <EOL> assert not job ( client , "<STR_LIT:success>" ) . killed ( ) <EOL> assert not job ( client , "<STR_LIT:error>" ) . killed ( ) <EOL> assert job ( client , "<STR_LIT>" ) . killed ( ) <EOL> def test_job_queued ( ) : <EOL> def job ( client , status ) : <EOL> stub = models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> stub . _update_progress = mock . MagicMock ( ) <EOL> return stub <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> assert job ( client , "<STR_LIT>" ) . queued ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . queued ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . queued ( ) <EOL> assert not job ( client , "<STR_LIT:success>" ) . queued ( ) <EOL> assert not job ( client , "<STR_LIT:error>" ) . queued ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . queued ( ) <EOL> def test_job_running ( ) : <EOL> def job ( client , status ) : <EOL> stub = models . Job ( client , "<STR_LIT:1>" , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> stub . _update_progress = mock . MagicMock ( ) <EOL> return stub <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> assert not job ( client , "<STR_LIT>" ) . running ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . running ( ) <EOL> assert job ( client , "<STR_LIT>" ) . running ( ) <EOL> assert not job ( client , "<STR_LIT:success>" ) . running ( ) <EOL> assert not job ( client , "<STR_LIT:error>" ) . running ( ) <EOL> assert not job ( client , "<STR_LIT>" ) . running ( ) <EOL> def test_job_update_progress ( ) : <EOL> def run ( client , job_id , status ) : <EOL> job = models . Job ( client , job_id , "<STR_LIT>" , "<STR_LIT>" , status = status ) <EOL> client . job_status . reset_mock ( ) <EOL> job . _update_progress ( ) <EOL> client = mock . MagicMock ( ) <EOL> client . job_status ( return_value = "<STR_LIT>" ) <EOL> run ( client , "<STR_LIT:1>" , "<STR_LIT>" ) <EOL> client . job_status . assert_called_with ( "<STR_LIT:1>" ) <EOL> run ( client , "<STR_LIT:2>" , "<STR_LIT>" ) <EOL> client . job_status . assert_called_with ( "<STR_LIT:2>" ) <EOL> run ( client , "<STR_LIT:3>" , "<STR_LIT>" ) <EOL> client . job_status . assert_called_with ( "<STR_LIT:3>" ) <EOL> run ( client , "<STR_LIT:4>" , "<STR_LIT:success>" ) <EOL> assert not client . job_status . called <EOL> run ( client , "<STR_LIT:5>" , "<STR_LIT:error>" ) <EOL> assert not client . job_status . called <EOL> run ( client , "<STR_LIT>" , "<STR_LIT>" ) <EOL> assert not client . job_status . called <EOL> def test_job_update_status ( ) : <EOL> client = mock . MagicMock ( ) <EOL> client . api . show_job = mock . MagicMock ( return_value = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : "<STR_LIT:success>" , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : datetime . datetime ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT> , tzinfo = dateutil . tz . tzutc ( ) ) , <EOL> "<STR_LIT>" : datetime . datetime ( <NUM_LIT> , <NUM_LIT:2> , <NUM_LIT:10> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT> , tzinfo = dateutil . tz . tzutc ( ) ) , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT:result>" : None , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : [ [ "<STR_LIT>" , "<STR_LIT>" ] ] , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT:0> , <EOL> "<STR_LIT>" : None , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> job = models . Job ( client , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> job . finished = mock . MagicMock ( return_value = False ) <EOL> assert job . status ( ) == "<STR_LIT:success>" <EOL> client . api . show_job . assert_called_with ( "<STR_LIT>" ) </s>
