<s> import unittest <EOL> import mock <EOL> import six <EOL> from bigquery import client <EOL> from bigquery . errors import ( <EOL> JobInsertException , JobExecutingException , <EOL> BigQueryTimeoutException <EOL> ) <EOL> from googleapiclient . errors import HttpError <EOL> from nose . tools import raises <EOL> class HttpResponse ( object ) : <EOL> def __init__ ( self , status , reason = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . status = status <EOL> self . reason = reason <EOL> class TestGetClient ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_job_collection = mock . Mock ( ) <EOL> self . mock_bq_service . jobs . return_value = self . mock_job_collection <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , '<STR_LIT>' ) <EOL> def test_no_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( AssertionError , client . get_client , '<STR_LIT:foo>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_initialize_readonly ( self , mock_build , mock_return_cred ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BIGQUERY_SCOPE_READ_ONLY <EOL> mock_cred = mock . Mock ( ) <EOL> mock_http = mock . Mock ( ) <EOL> mock_service_url = mock . Mock ( ) <EOL> mock_cred . from_p12_keyfile_buffer . return_value . authorize . return_value = mock_http <EOL> mock_bq = mock . Mock ( ) <EOL> mock_build . return_value = mock_bq <EOL> key = '<STR_LIT:key>' <EOL> service_account = '<STR_LIT>' <EOL> project_id = '<STR_LIT>' <EOL> mock_return_cred . return_value = mock_cred <EOL> bq_client = client . get_client ( <EOL> project_id , service_url = mock_service_url , <EOL> service_account = service_account , private_key = key , <EOL> readonly = True ) <EOL> mock_return_cred . assert_called_once_with ( ) <EOL> mock_cred . from_p12_keyfile_buffer . assert_called_once_with ( <EOL> service_account , mock . ANY , <EOL> scopes = BIGQUERY_SCOPE_READ_ONLY ) <EOL> self . assertTrue ( <EOL> mock_cred . from_p12_keyfile_buffer . return_value . authorize . called ) <EOL> mock_build . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , http = mock_http , <EOL> discoveryServiceUrl = mock_service_url ) <EOL> self . assertEquals ( mock_bq , bq_client . bigquery ) <EOL> self . assertEquals ( project_id , bq_client . project_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_initialize_read_write ( self , mock_build , mock_return_cred ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BIGQUERY_SCOPE <EOL> mock_cred = mock . Mock ( ) <EOL> mock_http = mock . Mock ( ) <EOL> mock_service_url = mock . Mock ( ) <EOL> mock_cred . from_p12_keyfile_buffer . return_value . authorize . return_value = mock_http <EOL> mock_bq = mock . Mock ( ) <EOL> mock_build . return_value = mock_bq <EOL> key = '<STR_LIT:key>' <EOL> service_account = '<STR_LIT>' <EOL> project_id = '<STR_LIT>' <EOL> mock_return_cred . return_value = mock_cred <EOL> bq_client = client . get_client ( <EOL> project_id , service_url = mock_service_url , <EOL> service_account = service_account , private_key = key , <EOL> readonly = False ) <EOL> mock_return_cred . assert_called_once_with ( ) <EOL> mock_cred . from_p12_keyfile_buffer . assert_called_once_with ( <EOL> service_account , mock . ANY , scopes = BIGQUERY_SCOPE ) <EOL> self . assertTrue ( <EOL> mock_cred . from_p12_keyfile_buffer . return_value . authorize . called ) <EOL> mock_build . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , http = mock_http , <EOL> discoveryServiceUrl = mock_service_url ) <EOL> self . assertEquals ( mock_bq , bq_client . bigquery ) <EOL> self . assertEquals ( project_id , bq_client . project_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_initialize_key_file ( self , mock_build , mock_return_cred ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BIGQUERY_SCOPE <EOL> mock_cred = mock . Mock ( ) <EOL> mock_http = mock . Mock ( ) <EOL> mock_service_url = mock . Mock ( ) <EOL> mock_cred . from_p12_keyfile . return_value . authorize . return_value = mock_http <EOL> mock_bq = mock . Mock ( ) <EOL> mock_build . return_value = mock_bq <EOL> key_file = '<STR_LIT>' <EOL> service_account = '<STR_LIT>' <EOL> project_id = '<STR_LIT>' <EOL> mock_return_cred . return_value = mock_cred <EOL> bq_client = client . get_client ( <EOL> project_id , service_url = mock_service_url , <EOL> service_account = service_account , <EOL> private_key_file = key_file , readonly = False ) <EOL> mock_return_cred . assert_called_once_with ( ) <EOL> mock_cred . from_p12_keyfile . assert_called_once_with ( service_account , <EOL> key_file , <EOL> scopes = BIGQUERY_SCOPE ) <EOL> self . assertTrue ( <EOL> mock_cred . from_p12_keyfile . return_value . authorize . called ) <EOL> mock_build . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , http = mock_http , <EOL> discoveryServiceUrl = mock_service_url ) <EOL> self . assertEquals ( mock_bq , bq_client . bigquery ) <EOL> self . assertEquals ( project_id , bq_client . project_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' if six . PY2 else '<STR_LIT>' ) <EOL> def test_initialize_json_key_file ( self , mock_open , mock_build , mock_return_cred ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BIGQUERY_SCOPE <EOL> import json <EOL> mock_cred = mock . Mock ( ) <EOL> mock_http = mock . Mock ( ) <EOL> mock_service_url = mock . Mock ( ) <EOL> mock_cred . from_json_keyfile_dict . return_value . authorize . return_value = mock_http <EOL> mock_bq = mock . Mock ( ) <EOL> mock_build . return_value = mock_bq <EOL> json_key_file = '<STR_LIT>' <EOL> json_key = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_open . return_value . __enter__ . return_value . read . return_value = json . dumps ( json_key ) <EOL> project_id = '<STR_LIT>' <EOL> mock_return_cred . return_value = mock_cred <EOL> bq_client = client . get_client ( <EOL> project_id , service_url = mock_service_url , <EOL> json_key_file = json_key_file , readonly = False ) <EOL> mock_return_cred . assert_called_once_with ( ) <EOL> mock_cred . from_json_keyfile_dict . assert_called_once_with ( json_key , <EOL> scopes = BIGQUERY_SCOPE ) <EOL> self . assertTrue ( <EOL> mock_cred . from_json_keyfile_dict . return_value . authorize . called ) <EOL> mock_build . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , http = mock_http , <EOL> discoveryServiceUrl = mock_service_url ) <EOL> self . assertEquals ( mock_bq , bq_client . bigquery ) <EOL> self . assertEquals ( project_id , bq_client . project_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' if six . PY2 else '<STR_LIT>' ) <EOL> def test_initialize_json_key_file_without_project_id ( self , mock_open , mock_build , <EOL> mock_return_cred ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BIGQUERY_SCOPE <EOL> import json <EOL> mock_cred = mock . Mock ( ) <EOL> mock_http = mock . Mock ( ) <EOL> mock_service_url = mock . Mock ( ) <EOL> mock_cred . from_json_keyfile_dict . return_value . authorize . return_value = mock_http <EOL> mock_bq = mock . Mock ( ) <EOL> mock_build . return_value = mock_bq <EOL> json_key_file = '<STR_LIT>' <EOL> json_key = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> mock_open . return_value . __enter__ . return_value . read . return_value = json . dumps ( json_key ) <EOL> mock_return_cred . return_value = mock_cred <EOL> bq_client = client . get_client ( <EOL> service_url = mock_service_url , json_key_file = json_key_file , readonly = False ) <EOL> mock_open . assert_called_once_with ( json_key_file , '<STR_LIT:r>' ) <EOL> mock_return_cred . assert_called_once_with ( ) <EOL> mock_cred . from_json_keyfile_dict . assert_called_once_with ( json_key , <EOL> scopes = BIGQUERY_SCOPE ) <EOL> self . assertTrue ( <EOL> mock_cred . from_json_keyfile_dict . return_value . authorize . called ) <EOL> mock_build . assert_called_once_with ( '<STR_LIT>' , '<STR_LIT>' , http = mock_http , <EOL> discoveryServiceUrl = mock_service_url ) <EOL> self . assertEquals ( mock_bq , bq_client . bigquery ) <EOL> self . assertEquals ( json_key [ '<STR_LIT>' ] , bq_client . project_id ) <EOL> class TestQuery ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_job_collection = mock . Mock ( ) <EOL> self . mock_bq_service . jobs . return_value = self . mock_job_collection <EOL> self . query = '<STR_LIT:foo>' <EOL> self . project_id = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , <EOL> self . project_id ) <EOL> def test_query ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> expected_job_id = '<STR_LIT>' <EOL> expected_job_ref = { '<STR_LIT>' : expected_job_id } <EOL> mock_query_job . execute . return_value = { <EOL> '<STR_LIT>' : expected_job_ref , <EOL> '<STR_LIT>' : True <EOL> } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> job_id , results = self . client . query ( self . query ) <EOL> self . mock_job_collection . query . assert_called_once_with ( <EOL> projectId = self . project_id , <EOL> body = { '<STR_LIT>' : self . query , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None } <EOL> ) <EOL> self . assertEquals ( job_id , '<STR_LIT>' ) <EOL> self . assertEquals ( results , [ ] ) <EOL> def test_query_max_results_set ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> expected_job_id = '<STR_LIT>' <EOL> expected_job_ref = { '<STR_LIT>' : expected_job_id } <EOL> mock_query_job . execute . return_value = { <EOL> '<STR_LIT>' : expected_job_ref , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> max_results = <NUM_LIT:10> <EOL> job_id , results = self . client . query ( self . query , <EOL> max_results = max_results ) <EOL> self . mock_job_collection . query . assert_called_once_with ( <EOL> projectId = self . project_id , <EOL> body = { '<STR_LIT>' : self . query , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : max_results , '<STR_LIT>' : False } <EOL> ) <EOL> self . assertEquals ( job_id , '<STR_LIT>' ) <EOL> self . assertEquals ( results , [ ] ) <EOL> def test_query_timeout_set ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> expected_job_id = '<STR_LIT>' <EOL> expected_job_ref = { '<STR_LIT>' : expected_job_id } <EOL> mock_query_job . execute . return_value = { <EOL> '<STR_LIT>' : expected_job_ref , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> timeout = <NUM_LIT:5> <EOL> job_id , results = self . client . query ( self . query , timeout = timeout ) <EOL> self . mock_job_collection . query . assert_called_once_with ( <EOL> projectId = self . project_id , <EOL> body = { '<STR_LIT>' : self . query , '<STR_LIT>' : timeout * <NUM_LIT:1000> , <EOL> '<STR_LIT>' : False , '<STR_LIT>' : None } <EOL> ) <EOL> self . assertEquals ( job_id , '<STR_LIT>' ) <EOL> self . assertEquals ( results , [ ] ) <EOL> def test_sync_query_timeout ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> expected_job_id = '<STR_LIT>' <EOL> expected_job_ref = { '<STR_LIT>' : expected_job_id } <EOL> mock_query_job . execute . return_value = { <EOL> '<STR_LIT>' : expected_job_ref , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> timeout = <NUM_LIT:5> <EOL> self . assertRaises ( BigQueryTimeoutException , self . client . query , <EOL> self . query , None , timeout ) <EOL> def test_async_query_timeout ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> expected_job_id = '<STR_LIT>' <EOL> expected_job_ref = { '<STR_LIT>' : expected_job_id } <EOL> mock_query_job . execute . return_value = { <EOL> '<STR_LIT>' : expected_job_ref , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> job_id , results = self . client . query ( self . query ) <EOL> self . assertEquals ( job_id , '<STR_LIT>' ) <EOL> self . assertEquals ( results , [ ] ) <EOL> def test_query_dry_run_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> mock_query_job . execute . return_value = { '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : True } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> job_id , results = self . client . query ( self . query , dry_run = True ) <EOL> self . mock_job_collection . query . assert_called_once_with ( <EOL> projectId = self . project_id , <EOL> body = { '<STR_LIT>' : self . query , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : None , <EOL> '<STR_LIT>' : True } <EOL> ) <EOL> self . assertIsNone ( job_id ) <EOL> self . assertEqual ( [ ] , results ) <EOL> def test_query_dry_run_invalid ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> mock_query_job . execute . side_effect = HttpError ( <EOL> '<STR_LIT>' , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> job_id , results = self . client . query ( '<STR_LIT>' % self . query , <EOL> dry_run = True ) <EOL> self . mock_job_collection . query . assert_called_once_with ( <EOL> projectId = self . project_id , <EOL> body = { '<STR_LIT>' : '<STR_LIT>' % self . query , '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : True } <EOL> ) <EOL> self . assertIsNone ( job_id ) <EOL> self . assertEqual ( { '<STR_LIT:message>' : '<STR_LIT>' } , results ) <EOL> def test_query_with_results ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_query_job = mock . Mock ( ) <EOL> expected_job_id = '<STR_LIT>' <EOL> expected_job_ref = { '<STR_LIT>' : expected_job_id } <EOL> mock_query_job . execute . return_value = { <EOL> '<STR_LIT>' : expected_job_ref , <EOL> '<STR_LIT>' : { '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } ] } , <EOL> '<STR_LIT>' : [ { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : <NUM_LIT:10> } ] } ] , <EOL> '<STR_LIT>' : True , <EOL> } <EOL> self . mock_job_collection . query . return_value = mock_query_job <EOL> job_id , results = self . client . query ( self . query ) <EOL> self . mock_job_collection . query . assert_called_once_with ( <EOL> projectId = self . project_id , <EOL> body = { '<STR_LIT>' : self . query , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : False , <EOL> '<STR_LIT>' : None } <EOL> ) <EOL> self . assertEquals ( job_id , '<STR_LIT>' ) <EOL> self . assertEquals ( results , [ { '<STR_LIT:foo>' : <NUM_LIT:10> } ] ) <EOL> class TestGetQueryResults ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_job_collection = mock . Mock ( ) <EOL> self . mock_bq_service . jobs . return_value = self . mock_job_collection <EOL> self . project_id = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , <EOL> self . project_id ) <EOL> def test_get_response ( self ) : <EOL> """<STR_LIT>""" <EOL> job_id = '<STR_LIT:bar>' <EOL> mock_query_job = mock . Mock ( ) <EOL> mock_query_reply = mock . Mock ( ) <EOL> mock_query_job . execute . return_value = mock_query_reply <EOL> self . mock_job_collection . getQueryResults . return_value = mock_query_job <EOL> offset = <NUM_LIT:5> <EOL> limit = <NUM_LIT:10> <EOL> page_token = "<STR_LIT>" <EOL> timeout = <NUM_LIT:1> <EOL> actual = self . client . get_query_results ( job_id , offset , limit , <EOL> page_token , timeout ) <EOL> self . mock_job_collection . getQueryResults . assert_called_once_with ( <EOL> projectId = self . project_id , jobId = job_id , startIndex = offset , <EOL> maxResults = limit , pageToken = page_token , timeoutMs = <NUM_LIT:1000> ) <EOL> mock_query_job . execute . assert_called_once_with ( ) <EOL> self . assertEquals ( actual , mock_query_reply ) <EOL> class TestTransformRow ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_job_collection = mock . Mock ( ) <EOL> self . mock_bq_service . jobs . return_value = self . mock_job_collection <EOL> self . project_id = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , <EOL> self . project_id ) <EOL> def test_transform_row ( self ) : <EOL> """<STR_LIT>""" <EOL> schema = [ { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } ] <EOL> row = { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : None } , { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : '<STR_LIT:True>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } <EOL> expected = { '<STR_LIT:foo>' : <NUM_LIT> , '<STR_LIT:bar>' : None , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT> } <EOL> actual = self . client . _transform_row ( row , schema ) <EOL> self . assertEquals ( actual , expected ) <EOL> def test_transform_row_with_nested ( self ) : <EOL> """<STR_LIT>""" <EOL> schema = [ { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } ] } ] <EOL> row = { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } } ] } <EOL> expected = { '<STR_LIT:foo>' : <NUM_LIT> , '<STR_LIT:bar>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' } } <EOL> actual = self . client . _transform_row ( row , schema ) <EOL> self . assertEquals ( actual , expected ) <EOL> def test_transform_row_with_nested_repeated ( self ) : <EOL> """<STR_LIT>""" <EOL> schema = [ { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } ] } ] <EOL> row = { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : [ { '<STR_LIT:v>' : { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } } , <EOL> { '<STR_LIT:v>' : { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } } ] } ] } <EOL> expected = { '<STR_LIT:foo>' : <NUM_LIT> , '<STR_LIT:bar>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : '<STR_LIT>' } ] } <EOL> actual = self . client . _transform_row ( row , schema ) <EOL> self . assertEquals ( actual , expected ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestCheckJob ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . project_id = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( mock . Mock ( ) , self . project_id ) <EOL> def test_job_incomplete ( self , mock_exec ) : <EOL> """<STR_LIT>""" <EOL> mock_exec . return_value = { '<STR_LIT>' : False } <EOL> is_completed , total_rows = self . client . check_job ( <NUM_LIT:1> ) <EOL> self . assertFalse ( is_completed ) <EOL> self . assertEquals ( total_rows , <NUM_LIT:0> ) <EOL> def test_query_complete ( self , mock_exec ) : <EOL> """<STR_LIT>""" <EOL> mock_exec . return_value = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT:bar>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } , <EOL> { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT:abc>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } <EOL> ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } <EOL> ] <EOL> } , <EOL> '<STR_LIT>' : '<STR_LIT:2>' <EOL> } <EOL> is_completed , total_rows = self . client . check_job ( <NUM_LIT:1> ) <EOL> self . assertTrue ( is_completed ) <EOL> self . assertEquals ( total_rows , <NUM_LIT:2> ) <EOL> class TestWaitForJob ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . project_id = '<STR_LIT>' <EOL> self . api_mock = mock . Mock ( ) <EOL> self . client = client . BigQueryClient ( self . api_mock , self . project_id ) <EOL> def test_completed_jobs ( self ) : <EOL> """<STR_LIT>""" <EOL> return_values = [ { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } , <EOL> { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } ] <EOL> def side_effect ( * args , ** kwargs ) : <EOL> return return_values . pop ( <NUM_LIT:0> ) <EOL> self . api_mock . jobs ( ) . get ( ) . execute . side_effect = side_effect <EOL> job_resource = self . client . wait_for_job ( <EOL> { '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } , <EOL> '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } } , <EOL> interval = <NUM_LIT> , <EOL> timeout = <NUM_LIT> ) <EOL> self . assertEqual ( self . api_mock . jobs ( ) . get ( ) . execute . call_count , <NUM_LIT:2> ) <EOL> self . assertIsInstance ( job_resource , dict ) <EOL> def test_timeout_error ( self ) : <EOL> """<STR_LIT>""" <EOL> incomplete_job = { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } <EOL> self . api_mock . jobs ( ) . get ( ) . execute . return_value = incomplete_job <EOL> self . assertRaises ( BigQueryTimeoutException , self . client . wait_for_job , <EOL> incomplete_job , <NUM_LIT> , <NUM_LIT> ) <EOL> def test_wait_job_http_error ( self ) : <EOL> """<STR_LIT>""" <EOL> job = { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } <EOL> expected_result = { <EOL> "<STR_LIT:error>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } ] , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> self . api_mock . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobExecutingException , <EOL> self . client . wait_for_job , <EOL> job , <EOL> interval = <NUM_LIT> , <EOL> timeout = <NUM_LIT> ) <EOL> def test_wait_job_error_result ( self ) : <EOL> """<STR_LIT>""" <EOL> job = { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } <EOL> expected_result = { <EOL> "<STR_LIT:status>" : { <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:location>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } , <EOL> } , <EOL> } <EOL> self . api_mock . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobExecutingException , <EOL> self . client . wait_for_job , <EOL> job , <EOL> interval = <NUM_LIT> , <EOL> timeout = <NUM_LIT> ) <EOL> def test_accepts_job_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return_values = [ { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } , <EOL> { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } ] <EOL> def side_effect ( * args , ** kwargs ) : <EOL> return return_values . pop ( <NUM_LIT:0> ) <EOL> self . api_mock . jobs ( ) . get ( ) . execute . side_effect = side_effect <EOL> job_resource = self . client . wait_for_job ( "<STR_LIT>" , <EOL> interval = <NUM_LIT> , <EOL> timeout = <NUM_LIT:5> ) <EOL> self . assertEqual ( self . api_mock . jobs ( ) . get ( ) . execute . call_count , <NUM_LIT:2> ) <EOL> self . assertIsInstance ( job_resource , dict ) <EOL> def test_accepts_integer_job_id ( self ) : <EOL> return_values = [ { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } , <EOL> { '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : "<STR_LIT>" } } ] <EOL> def side_effect ( * args , ** kwargs ) : <EOL> return return_values . pop ( <NUM_LIT:0> ) <EOL> self . api_mock . jobs ( ) . get ( ) . execute . side_effect = side_effect <EOL> job_resource = self . client . wait_for_job ( <NUM_LIT> , <EOL> interval = <NUM_LIT> , <EOL> timeout = <NUM_LIT> ) <EOL> self . assertEqual ( self . api_mock . jobs ( ) . get ( ) . execute . call_count , <NUM_LIT:2> ) <EOL> self . assertIsInstance ( job_resource , dict ) <EOL> class TestImportDataFromURIs ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_api = mock . Mock ( ) <EOL> self . query = '<STR_LIT:foo>' <EOL> self . project_id = '<STR_LIT>' <EOL> self . dataset_id = '<STR_LIT>' <EOL> self . table_id = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_api , <EOL> self . project_id ) <EOL> def test_csv_job_body_constructed_correctly ( self ) : <EOL> expected_result = { <EOL> '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> } <EOL> body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : { "<STR_LIT>" : [ "<STR_LIT>" ] } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : self . dataset_id , <EOL> "<STR_LIT>" : self . table_id <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT:a>" , <EOL> "<STR_LIT>" : "<STR_LIT:b>" , <EOL> "<STR_LIT>" : "<STR_LIT:c>" , <EOL> "<STR_LIT>" : "<STR_LIT:d>" , <EOL> "<STR_LIT>" : "<STR_LIT:e>" , <EOL> "<STR_LIT>" : "<STR_LIT:f>" , <EOL> "<STR_LIT>" : "<STR_LIT:g>" , <EOL> "<STR_LIT>" : "<STR_LIT:h>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:k>" <EOL> } <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> result = self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" , <EOL> create_disposition = "<STR_LIT:a>" , <EOL> write_disposition = "<STR_LIT:b>" , <EOL> field_delimiter = "<STR_LIT:c>" , <EOL> skip_leading_rows = "<STR_LIT:d>" , <EOL> encoding = "<STR_LIT:e>" , <EOL> quote = "<STR_LIT:f>" , <EOL> max_bad_records = "<STR_LIT:g>" , <EOL> allow_quoted_newlines = "<STR_LIT:h>" , <EOL> source_format = "<STR_LIT>" , <EOL> allow_jagged_rows = "<STR_LIT>" , <EOL> ignore_unknown_values = "<STR_LIT:k>" ) <EOL> self . mock_api . jobs ( ) . insert . assert_called_with ( <EOL> projectId = self . project_id , <EOL> body = body <EOL> ) <EOL> self . assertEqual ( result , expected_result ) <EOL> def test_json_job_body_constructed_correctly ( self ) : <EOL> expected_result = { <EOL> '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> } <EOL> body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : { "<STR_LIT>" : [ "<STR_LIT>" ] } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : self . dataset_id , <EOL> "<STR_LIT>" : self . table_id <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> result = self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" , <EOL> source_format = "<STR_LIT>" ) <EOL> self . mock_api . jobs ( ) . insert . assert_called_with ( <EOL> projectId = self . project_id , <EOL> body = body <EOL> ) <EOL> self . assertEqual ( result , expected_result ) <EOL> @ raises ( Exception ) <EOL> def test_field_delimiter_exception_if_not_csv ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" , <EOL> source_format = "<STR_LIT>" , <EOL> field_delimiter = "<STR_LIT:U+002C>" ) <EOL> @ raises ( Exception ) <EOL> def test_allow_jagged_rows_exception_if_not_csv ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" , <EOL> source_format = "<STR_LIT>" , <EOL> allow_jagged_rows = True ) <EOL> @ raises ( Exception ) <EOL> def test_allow_quoted_newlines_exception_if_not_csv ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" , <EOL> source_format = "<STR_LIT>" , <EOL> allow_quoted_newlines = True ) <EOL> @ raises ( Exception ) <EOL> def test_quote_exception_if_not_csv ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" , <EOL> source_format = "<STR_LIT>" , <EOL> quote = "<STR_LIT:'>" ) <EOL> @ raises ( Exception ) <EOL> def test_skip_leading_rows_exception_if_not_csv ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client . import_data_from_uris ( [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" , <EOL> source_format = "<STR_LIT>" , <EOL> skip_leading_rows = <NUM_LIT:10> ) <EOL> def test_accepts_single_source_uri ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> } <EOL> body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : { "<STR_LIT>" : [ "<STR_LIT>" ] } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : self . dataset_id , <EOL> "<STR_LIT>" : self . table_id <EOL> } <EOL> } <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> result = self . client . import_data_from_uris ( "<STR_LIT>" , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> schema = [ "<STR_LIT>" ] , <EOL> job = "<STR_LIT>" ) <EOL> self . mock_api . jobs ( ) . insert . assert_called_with ( <EOL> projectId = self . project_id , <EOL> body = body <EOL> ) <EOL> self . assertEqual ( result , expected_result ) <EOL> def test_import_http_error ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> "<STR_LIT:error>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } ] , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobInsertException , <EOL> self . client . import_data_from_uris , <EOL> [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id ) <EOL> def test_import_error_result ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> "<STR_LIT:status>" : { <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:location>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } , <EOL> } , <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobInsertException , <EOL> self . client . import_data_from_uris , <EOL> [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id ) <EOL> class TestExportDataToURIs ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_api = mock . Mock ( ) <EOL> self . project_id = '<STR_LIT>' <EOL> self . dataset_id = '<STR_LIT>' <EOL> self . table_id = '<STR_LIT>' <EOL> self . destination_format = "<STR_LIT>" <EOL> self . print_header = False <EOL> self . client = client . BigQueryClient ( self . mock_api , <EOL> self . project_id ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_export ( self , mock_generate_hex ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> } <EOL> body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( self . dataset_id , <EOL> self . table_id ) <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : self . dataset_id , <EOL> "<STR_LIT>" : self . table_id <EOL> } , <EOL> "<STR_LIT>" : self . destination_format , <EOL> "<STR_LIT>" : self . print_header , <EOL> } <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> mock_generate_hex . return_value = "<STR_LIT>" <EOL> result = self . client . export_data_to_uris ( <EOL> [ "<STR_LIT>" ] , self . dataset_id , self . table_id , <EOL> destination_format = self . destination_format , <EOL> print_header = self . print_header <EOL> ) <EOL> self . mock_api . jobs ( ) . insert . assert_called_with ( <EOL> projectId = self . project_id , <EOL> body = body <EOL> ) <EOL> self . assertEqual ( result , expected_result ) <EOL> def test_export_http_error ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> "<STR_LIT:error>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } ] , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobInsertException , <EOL> self . client . export_data_to_uris , <EOL> [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id ) <EOL> def test_export_error_result ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> "<STR_LIT:status>" : { <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:location>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } , <EOL> } , <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobInsertException , <EOL> self . client . export_data_to_uris , <EOL> [ "<STR_LIT>" ] , <EOL> self . dataset_id , <EOL> self . table_id ) <EOL> class TestWriteToTable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> client . _bq_client = None <EOL> self . mock_api = mock . Mock ( ) <EOL> self . query = '<STR_LIT:foo>' <EOL> self . project_id = '<STR_LIT>' <EOL> self . dataset_id = '<STR_LIT>' <EOL> self . table_id = '<STR_LIT>' <EOL> self . external_udf_uris = [ '<STR_LIT>' ] <EOL> self . use_query_cache = False <EOL> self . priority = "<STR_LIT>" <EOL> self . client = client . BigQueryClient ( self . mock_api , <EOL> self . project_id ) <EOL> def test_write ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> '<STR_LIT:status>' : { '<STR_LIT:state>' : u'<STR_LIT>' } , <EOL> } <EOL> body = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : self . project_id , <EOL> "<STR_LIT>" : self . dataset_id , <EOL> "<STR_LIT>" : self . table_id <EOL> } , <EOL> "<STR_LIT>" : self . query , <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : self . external_udf_uris [ <NUM_LIT:0> ] <EOL> } ] , <EOL> "<STR_LIT>" : self . use_query_cache , <EOL> "<STR_LIT>" : self . priority , <EOL> } <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> result = self . client . write_to_table ( self . query , <EOL> self . dataset_id , <EOL> self . table_id , <EOL> external_udf_uris = self . external_udf_uris , <EOL> use_query_cache = False , <EOL> priority = self . priority ) <EOL> self . mock_api . jobs ( ) . insert . assert_called_with ( <EOL> projectId = self . project_id , <EOL> body = body <EOL> ) <EOL> self . assertEqual ( result , expected_result ) <EOL> def test_write_http_error ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> "<STR_LIT:error>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } ] , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobInsertException , self . client . write_to_table , <EOL> self . query ) <EOL> def test_write_error_result ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = { <EOL> "<STR_LIT:status>" : { <EOL> "<STR_LIT:state>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:location>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } , <EOL> } , <EOL> } <EOL> self . mock_api . jobs ( ) . insert ( ) . execute . return_value = expected_result <EOL> self . assertRaises ( JobInsertException , self . client . write_to_table , <EOL> self . query ) <EOL> class TestFilterTablesByTime ( unittest . TestCase ) : <EOL> def test_empty_tables ( self ) : <EOL> """<STR_LIT>""" <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _filter_tables_by_time ( { } , <NUM_LIT> , <NUM_LIT:0> ) <EOL> self . assertEqual ( [ ] , tables ) <EOL> def test_multi_inside_range ( self ) : <EOL> """<STR_LIT>""" <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _filter_tables_by_time ( { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT:0> <EOL> } , <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> sorted ( <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) , <EOL> sorted ( tables ) <EOL> ) <EOL> def test_not_inside_range ( self ) : <EOL> """<STR_LIT>""" <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _filter_tables_by_time ( { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : - <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> } , <NUM_LIT> , <NUM_LIT> ) <EOL> self . assertEqual ( [ ] , tables ) <EOL> NEXT_TABLE_LIST_RESPONSE = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> ] , <EOL> "<STR_LIT>" : <NUM_LIT:3> <EOL> } <EOL> FULL_TABLE_LIST_RESPONSE = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" <EOL> } <EOL> ] , <EOL> "<STR_LIT>" : <NUM_LIT:8> <EOL> } <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestGetQuerySchema ( unittest . TestCase ) : <EOL> def test_query_complete ( self , get_query_mock ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BigQueryClient <EOL> bq = BigQueryClient ( mock . Mock ( ) , '<STR_LIT>' ) <EOL> get_query_mock . return_value = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> result_schema = bq . get_query_schema ( job_id = <NUM_LIT> ) <EOL> self . assertEquals ( result_schema , '<STR_LIT>' ) <EOL> def test_query_incomplete ( self , get_query_mock ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BigQueryClient <EOL> bq = BigQueryClient ( mock . Mock ( ) , '<STR_LIT>' ) <EOL> get_query_mock . return_value = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> self . assertRaises ( client . UnfinishedQueryException , bq . get_query_schema , <EOL> job_id = <NUM_LIT> ) <EOL> class TestGetTableSchema ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> def test_table_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> expected = [ <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] <EOL> self . mock_tables . get . return_value . execute . return_value = { '<STR_LIT>' : { '<STR_LIT>' : expected } } <EOL> self . assertEqual ( <EOL> expected , self . client . get_table_schema ( self . dataset , self . table ) ) <EOL> self . mock_tables . get . assert_called_once_with ( <EOL> projectId = self . project , tableId = self . table , datasetId = self . dataset ) <EOL> self . mock_tables . get . return_value . execute . assert_called_once_with ( ) <EOL> def test_table_does_not_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . get . return_value . execute . side_effect = HttpError ( { '<STR_LIT:status>' : "<STR_LIT>" } , '<STR_LIT:{}>' . encode ( '<STR_LIT:utf8>' ) ) <EOL> self . assertIsNone ( <EOL> self . client . get_table_schema ( self . dataset , self . table ) ) <EOL> self . mock_tables . get . assert_called_once_with ( <EOL> projectId = self . project , tableId = self . table , datasetId = self . dataset ) <EOL> self . mock_tables . get . return_value . execute . assert_called_once_with ( ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> class TestGetQueryRows ( unittest . TestCase ) : <EOL> def test_query_complete ( self , get_query_mock ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BigQueryClient <EOL> bq = BigQueryClient ( mock . Mock ( ) , '<STR_LIT>' ) <EOL> get_query_mock . return_value = { <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT:bar>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } , <EOL> { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT:abc>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } <EOL> ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } <EOL> ] <EOL> } , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } <EOL> result_rows = bq . get_query_rows ( job_id = <NUM_LIT> , offset = <NUM_LIT:0> , limit = <NUM_LIT:0> ) <EOL> expected_rows = [ { '<STR_LIT:foo>' : '<STR_LIT:bar>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:foo>' : '<STR_LIT:abc>' , '<STR_LIT>' : '<STR_LIT>' } ] <EOL> self . assertEquals ( result_rows , expected_rows ) <EOL> def test_query_complete_with_page_token ( self , get_query_mock ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BigQueryClient <EOL> page_one_resp = { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> } , { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> } ] <EOL> } , <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:f>" : [ { <EOL> "<STR_LIT:v>" : "<STR_LIT:foo>" , <EOL> } , { <EOL> "<STR_LIT:v>" : "<STR_LIT:bar>" <EOL> } ] <EOL> } , { <EOL> "<STR_LIT:f>" : [ { <EOL> "<STR_LIT:v>" : "<STR_LIT:abc>" , <EOL> } , { <EOL> "<STR_LIT:v>" : "<STR_LIT>" <EOL> } ] <EOL> } ] , <EOL> "<STR_LIT>" : "<STR_LIT:4>" <EOL> } <EOL> page_two_resp = { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> } , { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> } ] <EOL> } , <EOL> "<STR_LIT>" : [ { <EOL> "<STR_LIT:f>" : [ { <EOL> "<STR_LIT:v>" : "<STR_LIT>" , <EOL> } , { <EOL> "<STR_LIT:v>" : "<STR_LIT>" <EOL> } ] <EOL> } , { <EOL> "<STR_LIT:f>" : [ { <EOL> "<STR_LIT:v>" : "<STR_LIT>" , <EOL> } , { <EOL> "<STR_LIT:v>" : "<STR_LIT>" <EOL> } ] <EOL> } ] , <EOL> "<STR_LIT>" : "<STR_LIT:4>" <EOL> } <EOL> bq = BigQueryClient ( mock . Mock ( ) , '<STR_LIT>' ) <EOL> get_query_mock . side_effect = [ page_one_resp , page_two_resp ] <EOL> result_rows = bq . get_query_rows ( job_id = <NUM_LIT> , offset = <NUM_LIT:0> , limit = <NUM_LIT:0> ) <EOL> expected_rows = [ { '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : '<STR_LIT:bar>' } , <EOL> { '<STR_LIT>' : '<STR_LIT:abc>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } ] <EOL> self . assertEquals ( result_rows , expected_rows ) <EOL> def test_query_incomplete ( self , get_query_mock ) : <EOL> """<STR_LIT>""" <EOL> from bigquery . client import BigQueryClient <EOL> bq = BigQueryClient ( mock . Mock ( ) , '<STR_LIT>' ) <EOL> get_query_mock . return_value = { <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT:bar>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } , <EOL> { '<STR_LIT:f>' : [ { '<STR_LIT:v>' : '<STR_LIT:abc>' } , { '<STR_LIT:v>' : '<STR_LIT>' } ] } <EOL> ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' } <EOL> ] <EOL> } , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } <EOL> self . assertRaises ( client . UnfinishedQueryException , bq . get_query_rows , <EOL> job_id = <NUM_LIT> , offset = <NUM_LIT:0> , limit = <NUM_LIT:0> ) <EOL> class TestCheckTable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> def test_table_does_not_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . get . return_value . execute . side_effect = ( <EOL> HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) ) <EOL> actual = self . client . check_table ( self . dataset , self . table ) <EOL> self . assertFalse ( actual ) <EOL> self . mock_tables . get . assert_called_once_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table ) <EOL> self . mock_tables . get . return_value . execute . assert_called_once_with ( ) <EOL> def test_table_does_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . get . return_value . execute . side_effect = { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } <EOL> actual = self . client . check_table ( self . dataset , self . table ) <EOL> self . assertTrue ( actual ) <EOL> self . mock_tables . get . assert_called_once_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table ) <EOL> self . mock_tables . get . return_value . execute . assert_called_once_with ( ) <EOL> class TestCreateTable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . schema = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> ] <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . body = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : self . schema } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . table , '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . dataset } <EOL> } <EOL> self . expiration_time = <NUM_LIT> <EOL> def test_table_create_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . insert . return_value . execute . side_effect = ( <EOL> HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) ) <EOL> actual = self . client . create_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . create_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . insert . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . insert . return_value . execute . assert_called_with ( ) <EOL> def test_table_create_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . insert . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . create_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . create_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . insert . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . insert . return_value . execute . assert_called_with ( ) <EOL> def test_table_create_body_with_expiration_time ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . insert . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> self . client . create_table ( self . dataset , self . table , <EOL> self . schema , self . expiration_time ) <EOL> body = self . body . copy ( ) <EOL> body . update ( { <EOL> '<STR_LIT>' : self . expiration_time <EOL> } ) <EOL> self . mock_tables . insert . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = body ) <EOL> self . mock_tables . insert . return_value . execute . assert_called_with ( ) <EOL> class TestUpdateTable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . schema = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> ] <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . body = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : self . schema } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . table , '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . dataset } <EOL> } <EOL> self . expiration_time = <NUM_LIT> <EOL> def test_table_update_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . update . return_value . execute . side_effect = ( <EOL> HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) ) <EOL> actual = self . client . update_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . update_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . update . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . update . return_value . execute . assert_called_with ( ) <EOL> def test_table_update_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . update . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . update_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . update_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . update . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . update . return_value . execute . assert_called_with ( ) <EOL> class TestPatchTable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . schema = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT:foo>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT:bar>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> ] <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . body = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : self . schema } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . table , '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . dataset } <EOL> } <EOL> self . expiration_time = <NUM_LIT> <EOL> def test_table_patch_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . patch . return_value . execute . side_effect = ( <EOL> HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) ) <EOL> actual = self . client . patch_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . patch_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . patch . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . patch . return_value . execute . assert_called_with ( ) <EOL> def test_table_patch_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . patch . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . patch_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . patch_table ( self . dataset , self . table , <EOL> self . schema ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . patch . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . patch . return_value . execute . assert_called_with ( ) <EOL> class TestCreateView ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . query = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . body = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : self . query } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . table , '<STR_LIT>' : self . project , <EOL> '<STR_LIT>' : self . dataset } <EOL> } <EOL> def test_view_create_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . insert . return_value . execute . side_effect = ( <EOL> HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) ) <EOL> actual = self . client . create_view ( self . dataset , self . table , <EOL> self . query ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . create_view ( self . dataset , self . table , <EOL> self . query ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . insert . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . insert . return_value . execute . assert_called_with ( ) <EOL> def test_view_create_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . insert . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . create_view ( self . dataset , self . table , <EOL> self . query ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . create_view ( self . dataset , self . table , <EOL> self . query ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . insert . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_tables . insert . return_value . execute . assert_called_with ( ) <EOL> class TestDeleteTable ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_tables = mock . Mock ( ) <EOL> self . mock_bq_service . tables . return_value = self . mock_tables <EOL> self . table = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> def test_delete_table_fail ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . delete . return_value . execute . side_effect = ( <EOL> HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) ) <EOL> actual = self . client . delete_table ( self . dataset , self . table ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . delete_table ( self . dataset , self . table ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . delete . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table ) <EOL> self . mock_tables . delete . return_value . execute . assert_called_with ( ) <EOL> def test_delete_table_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_tables . delete . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . delete_table ( self . dataset , self . table ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . delete_table ( self . dataset , self . table ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_tables . delete . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table ) <EOL> self . mock_tables . delete . return_value . execute . assert_called_with ( ) <EOL> class TestParseTableListReponse ( unittest . TestCase ) : <EOL> def test_full_parse ( self ) : <EOL> """<STR_LIT>""" <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _parse_table_list_response ( FULL_TABLE_LIST_RESPONSE ) <EOL> expected_result = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } <EOL> } <EOL> self . assertEquals ( expected_result , tables ) <EOL> def test_empty_parse ( self ) : <EOL> """<STR_LIT>""" <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _parse_table_list_response ( { } ) <EOL> self . assertEquals ( tables , { } ) <EOL> def test_error ( self ) : <EOL> """<STR_LIT>""" <EOL> error_response = { <EOL> "<STR_LIT:error>" : { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:location>" : "<STR_LIT>" <EOL> } <EOL> ] , <EOL> "<STR_LIT:code>" : <NUM_LIT> , <EOL> "<STR_LIT:message>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _parse_table_list_response ( error_response ) <EOL> self . assertEquals ( tables , { } ) <EOL> def test_incorrect_table_formats ( self ) : <EOL> """<STR_LIT>""" <EOL> list_response = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> ] , <EOL> } <EOL> bq = client . BigQueryClient ( None , '<STR_LIT>' ) <EOL> tables = bq . _parse_table_list_response ( list_response ) <EOL> self . assertEquals ( tables , { } ) <EOL> class TestPushRows ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_table_data = mock . Mock ( ) <EOL> self . mock_bq_service . tabledata . return_value = self . mock_table_data <EOL> self . table = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . rows = [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } ] <EOL> self . data = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT>' : "<STR_LIT>" , '<STR_LIT>' : <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } ] <EOL> } <EOL> def test_push_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_table_data . insertAll . return_value . execute . return_value = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> actual = self . client . push_rows ( self . dataset , self . table , self . rows , <EOL> '<STR_LIT>' ) <EOL> self . assertFalse ( actual ) <EOL> self . mock_bq_service . tabledata . assert_called_once_with ( ) <EOL> self . mock_table_data . insertAll . assert_called_once_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table , <EOL> body = self . data ) <EOL> execute_calls = [ mock . call ( ) ] <EOL> self . mock_table_data . insertAll . return_value . execute . assert_has_calls ( <EOL> execute_calls ) <EOL> def test_push_failed_swallow_results_false ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_table_data . insertAll . return_value . execute . return_value = { <EOL> '<STR_LIT>' : '<STR_LIT:foo>' } <EOL> self . client . swallow_results = False <EOL> actual = self . client . push_rows ( self . dataset , self . table , self . rows , <EOL> '<STR_LIT>' ) <EOL> self . client . swallow_results = True <EOL> self . assertEqual ( <EOL> actual , <EOL> self . mock_table_data . insertAll . return_value . execute . return_value ) <EOL> def test_push_exception ( self ) : <EOL> """<STR_LIT>""" <EOL> e = HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) <EOL> self . mock_table_data . insertAll . return_value . execute . side_effect = e <EOL> actual = self . client . push_rows ( self . dataset , self . table , self . rows , <EOL> '<STR_LIT>' ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . push_rows ( self . dataset , self . table , self . rows , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( actual , { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:message>' : e <EOL> } ] <EOL> } ] } ) <EOL> self . client . swallow_results = True <EOL> self . mock_bq_service . tabledata . assert_called_with ( ) <EOL> self . mock_table_data . insertAll . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table , <EOL> body = self . data ) <EOL> execute_calls = [ mock . call ( ) ] <EOL> self . mock_table_data . insertAll . return_value . execute . assert_has_calls ( <EOL> execute_calls ) <EOL> def test_push_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_table_data . insertAll . return_value . execute . return_value = { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } <EOL> actual = self . client . push_rows ( self . dataset , self . table , self . rows , <EOL> '<STR_LIT>' ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . push_rows ( self . dataset , self . table , self . rows , <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:foo>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_bq_service . tabledata . assert_called_with ( ) <EOL> self . mock_table_data . insertAll . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , tableId = self . table , <EOL> body = self . data ) <EOL> execute_calls = [ mock . call ( ) ] <EOL> self . mock_table_data . insertAll . return_value . execute . assert_has_calls ( <EOL> execute_calls ) <EOL> def test_request_data_with_options ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_body = self . data . copy ( ) <EOL> self . client . push_rows ( <EOL> self . dataset , self . table , self . rows , <EOL> insert_id_key = '<STR_LIT>' ) <EOL> self . mock_table_data . insertAll . assert_called_with ( <EOL> projectId = self . project , <EOL> datasetId = self . dataset , <EOL> tableId = self . table , <EOL> body = expected_body ) <EOL> self . client . push_rows ( <EOL> self . dataset , self . table , self . rows , <EOL> insert_id_key = '<STR_LIT>' , <EOL> ignore_unknown_values = False , <EOL> skip_invalid_rows = False ) <EOL> expected_body [ '<STR_LIT>' ] = False <EOL> expected_body [ '<STR_LIT>' ] = False <EOL> self . mock_table_data . insertAll . assert_called_with ( <EOL> projectId = self . project , <EOL> datasetId = self . dataset , <EOL> tableId = self . table , <EOL> body = expected_body ) <EOL> self . client . push_rows ( <EOL> self . dataset , self . table , self . rows , <EOL> insert_id_key = '<STR_LIT>' , <EOL> ignore_unknown_values = True , <EOL> skip_invalid_rows = True , <EOL> template_suffix = '<STR_LIT>' <EOL> ) <EOL> expected_body [ '<STR_LIT>' ] = True <EOL> expected_body [ '<STR_LIT>' ] = True <EOL> expected_body [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . mock_table_data . insertAll . assert_called_with ( <EOL> projectId = self . project , <EOL> datasetId = self . dataset , <EOL> tableId = self . table , <EOL> body = expected_body ) <EOL> class TestGetAllTables ( unittest . TestCase ) : <EOL> def test_get_tables ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . return_value = FULL_TABLE_LIST_RESPONSE <EOL> mock_tables = mock . Mock ( ) <EOL> mock_tables . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . tables . return_value = mock_tables <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> expected_result = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } <EOL> } <EOL> tables = bq . _get_all_tables ( '<STR_LIT>' , cache = False ) <EOL> self . assertEquals ( expected_result , tables ) <EOL> def test_get_all_tables_with_page_token ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . side_effect = [ NEXT_TABLE_LIST_RESPONSE , <EOL> FULL_TABLE_LIST_RESPONSE ] <EOL> mock_tables = mock . Mock ( ) <EOL> mock_tables . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . tables . return_value = mock_tables <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> expected_result = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> } <EOL> tables = bq . _get_all_tables ( '<STR_LIT>' , cache = False ) <EOL> self . assertEquals ( expected_result , tables ) <EOL> def test_get_all_tables_with_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . return_value = FULL_TABLE_LIST_RESPONSE <EOL> mock_tables = mock . Mock ( ) <EOL> mock_tables . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . tables . return_value = mock_tables <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> expected_result = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } <EOL> } <EOL> tables = bq . _get_all_tables ( '<STR_LIT>' , cache = True ) <EOL> self . assertEquals ( expected_result , tables ) <EOL> mock_execute . execute . side_effect = [ NEXT_TABLE_LIST_RESPONSE , <EOL> FULL_TABLE_LIST_RESPONSE ] <EOL> tables = bq . _get_all_tables ( '<STR_LIT>' , cache = True ) <EOL> self . assertEquals ( expected_result , tables ) <EOL> expected_result = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } , <EOL> } <EOL> tables = bq . _get_all_tables ( '<STR_LIT>' , cache = False ) <EOL> self . assertEquals ( expected_result , tables ) <EOL> class TestGetTables ( unittest . TestCase ) : <EOL> def test_get_tables ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . return_value = FULL_TABLE_LIST_RESPONSE <EOL> mock_tables = mock . Mock ( ) <EOL> mock_tables . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . tables . return_value = mock_tables <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> tables = bq . get_tables ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT> ) <EOL> six . assertCountEqual ( self , tables , [ '<STR_LIT>' ] ) <EOL> def test_get_tables_from_datetimes ( self ) : <EOL> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . return_value = FULL_TABLE_LIST_RESPONSE <EOL> mock_tables = mock . Mock ( ) <EOL> mock_tables . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . tables . return_value = mock_tables <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> start = datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:10> ) <EOL> end = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:10> ) <EOL> tables = bq . get_tables ( '<STR_LIT>' , '<STR_LIT>' , start , end ) <EOL> six . assertCountEqual ( self , tables , [ '<STR_LIT>' ] ) <EOL> class TestCreateDataset ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_datasets = mock . Mock ( ) <EOL> self . mock_bq_service . datasets . return_value = self . mock_datasets <EOL> self . dataset = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . friendly_name = "<STR_LIT>" <EOL> self . description = "<STR_LIT:description>" <EOL> self . access = [ { '<STR_LIT>' : "<STR_LIT>" } ] <EOL> self . body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . dataset , <EOL> '<STR_LIT>' : self . project } , <EOL> '<STR_LIT>' : self . friendly_name , <EOL> '<STR_LIT:description>' : self . description , <EOL> '<STR_LIT>' : self . access <EOL> } <EOL> def test_dataset_create_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . insert . return_value . execute . side_effect = HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) <EOL> actual = self . client . create_dataset ( self . dataset , <EOL> friendly_name = self . friendly_name , <EOL> description = self . description , <EOL> access = self . access ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . create_dataset ( self . dataset , <EOL> friendly_name = self . friendly_name , <EOL> description = self . description , <EOL> access = self . access ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . insert . assert_called_with ( <EOL> projectId = self . project , body = self . body ) <EOL> self . mock_datasets . insert . return_value . execute . assert_called_with ( ) <EOL> def test_dataset_create_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . insert . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . create_dataset ( self . dataset , <EOL> self . friendly_name , <EOL> self . description , <EOL> self . access ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . create_dataset ( self . dataset , <EOL> self . friendly_name , <EOL> self . description , <EOL> self . access ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . insert . assert_called_with ( <EOL> projectId = self . project , body = self . body ) <EOL> self . mock_datasets . insert . return_value . execute . assert_called_with ( ) <EOL> class TestDeleteDataset ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_datasets = mock . Mock ( ) <EOL> self . mock_bq_service . datasets . return_value = self . mock_datasets <EOL> self . project = '<STR_LIT>' <EOL> self . dataset = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> def test_delete_datasets_fail ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . delete . return_value . execute . side_effect = HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) <EOL> actual = self . client . delete_dataset ( self . dataset ) <EOL> self . assertFalse ( actual ) <EOL> self . mock_datasets . delete . assert_called_once_with ( <EOL> projectId = self . project , datasetId = self . dataset , <EOL> deleteContents = False ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . delete_dataset ( self . dataset ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . delete . return_value . execute . assert_called_with ( ) <EOL> def test_delete_datasets_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . delete . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . delete_dataset ( self . dataset ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . delete_dataset ( self . dataset ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . delete . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , <EOL> deleteContents = False ) <EOL> self . mock_datasets . delete . return_value . execute . assert_called_with ( ) <EOL> def test_delete_datasets_delete_contents_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . delete . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . delete_dataset ( self . dataset , True ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . delete_dataset ( self . dataset , True ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . delete . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , <EOL> deleteContents = True ) <EOL> self . mock_datasets . delete . return_value . execute . assert_called_with ( ) <EOL> FULL_DATASET_LIST_RESPONSE = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } , <EOL> { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:id>" : "<STR_LIT>" <EOL> } <EOL> ] , <EOL> "<STR_LIT>" : <NUM_LIT:8> <EOL> } <EOL> class TestGetDatasets ( unittest . TestCase ) : <EOL> def test_get_datasets ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . return_value = FULL_DATASET_LIST_RESPONSE <EOL> mock_datasets = mock . Mock ( ) <EOL> mock_datasets . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . datasets . return_value = mock_datasets <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> datasets = bq . get_datasets ( ) <EOL> six . assertCountEqual ( self , datasets , <EOL> FULL_DATASET_LIST_RESPONSE [ '<STR_LIT>' ] ) <EOL> def test_get_datasets_returns_no_list ( self ) : <EOL> """<STR_LIT>""" <EOL> mock_execute = mock . Mock ( ) <EOL> mock_execute . execute . return_value = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> mock_datasets = mock . Mock ( ) <EOL> mock_datasets . list . return_value = mock_execute <EOL> mock_bq_service = mock . Mock ( ) <EOL> mock_bq_service . datasets . return_value = mock_datasets <EOL> bq = client . BigQueryClient ( mock_bq_service , '<STR_LIT>' ) <EOL> datasets = bq . get_datasets ( ) <EOL> six . assertCountEqual ( self , datasets , [ ] ) <EOL> class TestUpdateDataset ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . mock_bq_service = mock . Mock ( ) <EOL> self . mock_datasets = mock . Mock ( ) <EOL> self . mock_bq_service . datasets . return_value = self . mock_datasets <EOL> self . dataset = '<STR_LIT>' <EOL> self . project = '<STR_LIT>' <EOL> self . client = client . BigQueryClient ( self . mock_bq_service , self . project ) <EOL> self . friendly_name = "<STR_LIT>" <EOL> self . description = "<STR_LIT:description>" <EOL> self . access = [ { '<STR_LIT>' : "<STR_LIT>" } ] <EOL> self . body = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . dataset , <EOL> '<STR_LIT>' : self . project } , <EOL> '<STR_LIT>' : self . friendly_name , <EOL> '<STR_LIT:description>' : self . description , <EOL> '<STR_LIT>' : self . access <EOL> } <EOL> def test_dataset_update_failed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . update . return_value . execute . side_effect = HttpError ( HttpResponse ( <NUM_LIT> ) , '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) ) <EOL> actual = self . client . update_dataset ( self . dataset , <EOL> friendly_name = self . friendly_name , <EOL> description = self . description , <EOL> access = self . access ) <EOL> self . assertFalse ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . update_dataset ( self . dataset , <EOL> friendly_name = self . friendly_name , <EOL> description = self . description , <EOL> access = self . access ) <EOL> self . assertEqual ( actual , { } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . update . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_datasets . update . return_value . execute . assert_called_with ( ) <EOL> def test_dataset_update_success ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mock_datasets . update . return_value . execute . side_effect = [ { <EOL> '<STR_LIT:status>' : '<STR_LIT:foo>' } , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ] <EOL> actual = self . client . update_dataset ( self . dataset , <EOL> self . friendly_name , <EOL> self . description , <EOL> self . access ) <EOL> self . assertTrue ( actual ) <EOL> self . client . swallow_results = False <EOL> actual = self . client . update_dataset ( self . dataset , <EOL> self . friendly_name , <EOL> self . description , <EOL> self . access ) <EOL> self . assertEqual ( actual , { '<STR_LIT:status>' : '<STR_LIT:bar>' } ) <EOL> self . client . swallow_results = True <EOL> self . mock_datasets . update . assert_called_with ( <EOL> projectId = self . project , datasetId = self . dataset , body = self . body ) <EOL> self . mock_datasets . update . return_value . execute . assert_called_with ( ) </s>
