<s> import unittest2 <EOL> class TestQueryResults ( unittest2 . TestCase ) : <EOL> PROJECT = '<STR_LIT>' <EOL> JOB_NAME = '<STR_LIT>' <EOL> JOB_NAME = '<STR_LIT>' <EOL> JOB_TYPE = '<STR_LIT>' <EOL> QUERY = '<STR_LIT>' <EOL> TOKEN = '<STR_LIT>' <EOL> def _getTargetClass ( self ) : <EOL> from gcloud . bigquery . query import QueryResults <EOL> return QueryResults <EOL> def _makeOne ( self , * args , ** kw ) : <EOL> return self . _getTargetClass ( ) ( * args , ** kw ) <EOL> def _makeResource ( self , complete = False ) : <EOL> resource = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : self . JOB_NAME , <EOL> } , <EOL> '<STR_LIT>' : complete , <EOL> '<STR_LIT>' : [ ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] , <EOL> } , <EOL> } <EOL> if complete : <EOL> resource [ '<STR_LIT>' ] = <NUM_LIT:1000> <EOL> resource [ '<STR_LIT>' ] = [ <EOL> { '<STR_LIT:f>' : [ <EOL> { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : <NUM_LIT:32> } , <EOL> ] } , <EOL> { '<STR_LIT:f>' : [ <EOL> { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : <NUM_LIT> } , <EOL> ] } , <EOL> { '<STR_LIT:f>' : [ <EOL> { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : <NUM_LIT> } , <EOL> ] } , <EOL> { '<STR_LIT:f>' : [ <EOL> { '<STR_LIT:v>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:v>' : <NUM_LIT> } , <EOL> ] } , <EOL> ] <EOL> resource [ '<STR_LIT>' ] = self . TOKEN <EOL> resource [ '<STR_LIT>' ] = <NUM_LIT> <EOL> resource [ '<STR_LIT>' ] = False <EOL> return resource <EOL> def _verifySchema ( self , query , resource ) : <EOL> from gcloud . bigquery . table import SchemaField <EOL> if '<STR_LIT>' in resource : <EOL> fields = resource [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . assertEqual ( len ( query . schema ) , len ( fields ) ) <EOL> for found , expected in zip ( query . schema , fields ) : <EOL> self . assertTrue ( isinstance ( found , SchemaField ) ) <EOL> self . assertEqual ( found . name , expected [ '<STR_LIT:name>' ] ) <EOL> self . assertEqual ( found . field_type , expected [ '<STR_LIT:type>' ] ) <EOL> self . assertEqual ( found . mode , expected [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( found . description , <EOL> expected . get ( '<STR_LIT:description>' ) ) <EOL> self . assertEqual ( found . fields , expected . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> self . assertTrue ( query . schema is None ) <EOL> def _verifyRows ( self , query , resource ) : <EOL> expected = resource . get ( '<STR_LIT>' ) <EOL> if expected is None : <EOL> self . assertEqual ( query . rows , [ ] ) <EOL> else : <EOL> found = query . rows <EOL> self . assertEqual ( len ( found ) , len ( expected ) ) <EOL> for f_row , e_row in zip ( found , expected ) : <EOL> self . assertEqual ( f_row , <EOL> tuple ( [ cell [ '<STR_LIT:v>' ] for cell in e_row [ '<STR_LIT:f>' ] ] ) ) <EOL> def _verifyResourceProperties ( self , query , resource ) : <EOL> self . assertEqual ( query . cache_hit , resource . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( query . complete , resource . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( query . errors , resource . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( query . page_token , resource . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( query . total_rows , resource . get ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( query . total_bytes_processed , <EOL> resource . get ( '<STR_LIT>' ) ) <EOL> if '<STR_LIT>' in resource : <EOL> self . assertEqual ( query . name , resource [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . assertTrue ( query . name is None ) <EOL> self . _verifySchema ( query , resource ) <EOL> self . _verifyRows ( query , resource ) <EOL> def test_ctor ( self ) : <EOL> client = _Client ( self . PROJECT ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> self . assertEqual ( query . query , self . QUERY ) <EOL> self . assertTrue ( query . _client is client ) <EOL> self . assertTrue ( query . cache_hit is None ) <EOL> self . assertTrue ( query . complete is None ) <EOL> self . assertTrue ( query . errors is None ) <EOL> self . assertTrue ( query . name is None ) <EOL> self . assertTrue ( query . page_token is None ) <EOL> self . assertEqual ( query . rows , [ ] ) <EOL> self . assertTrue ( query . schema is None ) <EOL> self . assertTrue ( query . total_rows is None ) <EOL> self . assertTrue ( query . total_bytes_processed is None ) <EOL> self . assertTrue ( query . default_dataset is None ) <EOL> self . assertTrue ( query . max_results is None ) <EOL> self . assertTrue ( query . preserve_nulls is None ) <EOL> self . assertTrue ( query . use_query_cache is None ) <EOL> def test_job_wo_jobid ( self ) : <EOL> client = _Client ( self . PROJECT ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> self . assertTrue ( query . job is None ) <EOL> def test_job_w_jobid ( self ) : <EOL> from gcloud . bigquery . job import QueryJob <EOL> SERVER_GENERATED = '<STR_LIT>' <EOL> client = _Client ( self . PROJECT ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> query . _properties [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : SERVER_GENERATED , <EOL> } <EOL> job = query . job <EOL> self . assertTrue ( isinstance ( job , QueryJob ) ) <EOL> self . assertEqual ( job . query , self . QUERY ) <EOL> self . assertTrue ( job . _client is client ) <EOL> self . assertEqual ( job . name , SERVER_GENERATED ) <EOL> fetched_later = query . job <EOL> self . assertTrue ( fetched_later is job ) <EOL> def test_schema ( self ) : <EOL> client = _Client ( self . PROJECT ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> self . _verifyResourceProperties ( query , { } ) <EOL> resource = { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } , <EOL> ] , <EOL> } , <EOL> } <EOL> query . _set_properties ( resource ) <EOL> self . _verifyResourceProperties ( query , resource ) <EOL> def test_run_w_bound_client ( self ) : <EOL> PATH = '<STR_LIT>' % self . PROJECT <EOL> RESOURCE = self . _makeResource ( complete = False ) <EOL> conn = _Connection ( RESOURCE ) <EOL> client = _Client ( project = self . PROJECT , connection = conn ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> query . run ( ) <EOL> self . assertEqual ( len ( conn . _requested ) , <NUM_LIT:1> ) <EOL> req = conn . _requested [ <NUM_LIT:0> ] <EOL> self . assertEqual ( req [ '<STR_LIT>' ] , '<STR_LIT:POST>' ) <EOL> self . assertEqual ( req [ '<STR_LIT:path>' ] , '<STR_LIT>' % PATH ) <EOL> SENT = { '<STR_LIT>' : self . QUERY } <EOL> self . assertEqual ( req [ '<STR_LIT:data>' ] , SENT ) <EOL> self . _verifyResourceProperties ( query , RESOURCE ) <EOL> def test_run_w_alternate_client ( self ) : <EOL> PATH = '<STR_LIT>' % self . PROJECT <EOL> RESOURCE = self . _makeResource ( complete = True ) <EOL> DATASET = '<STR_LIT>' <EOL> conn1 = _Connection ( ) <EOL> client1 = _Client ( project = self . PROJECT , connection = conn1 ) <EOL> conn2 = _Connection ( RESOURCE ) <EOL> client2 = _Client ( project = self . PROJECT , connection = conn2 ) <EOL> query = self . _makeOne ( self . QUERY , client1 ) <EOL> query . default_dataset = client2 . dataset ( DATASET ) <EOL> query . max_results = <NUM_LIT:100> <EOL> query . preserve_nulls = True <EOL> query . timeout_ms = <NUM_LIT> <EOL> query . use_query_cache = False <EOL> query . run ( client = client2 ) <EOL> self . assertEqual ( len ( conn1 . _requested ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( conn2 . _requested ) , <NUM_LIT:1> ) <EOL> req = conn2 . _requested [ <NUM_LIT:0> ] <EOL> self . assertEqual ( req [ '<STR_LIT>' ] , '<STR_LIT:POST>' ) <EOL> self . assertEqual ( req [ '<STR_LIT:path>' ] , '<STR_LIT>' % PATH ) <EOL> SENT = { <EOL> '<STR_LIT>' : self . QUERY , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . PROJECT , <EOL> '<STR_LIT>' : DATASET , <EOL> } , <EOL> '<STR_LIT>' : <NUM_LIT:100> , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : False , <EOL> } <EOL> self . assertEqual ( req [ '<STR_LIT:data>' ] , SENT ) <EOL> self . _verifyResourceProperties ( query , RESOURCE ) <EOL> def test_fetch_data_query_not_yet_run ( self ) : <EOL> conn = _Connection ( ) <EOL> client = _Client ( project = self . PROJECT , connection = conn ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> self . assertRaises ( ValueError , query . fetch_data ) <EOL> def test_fetch_data_w_bound_client ( self ) : <EOL> PATH = '<STR_LIT>' % ( self . PROJECT , self . JOB_NAME ) <EOL> BEFORE = self . _makeResource ( complete = False ) <EOL> AFTER = self . _makeResource ( complete = True ) <EOL> conn = _Connection ( AFTER ) <EOL> client = _Client ( project = self . PROJECT , connection = conn ) <EOL> query = self . _makeOne ( self . QUERY , client ) <EOL> query . _set_properties ( BEFORE ) <EOL> self . assertFalse ( query . complete ) <EOL> rows , total_rows , page_token = query . fetch_data ( ) <EOL> self . assertTrue ( query . complete ) <EOL> self . assertEqual ( len ( rows ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( rows [ <NUM_LIT:0> ] , ( '<STR_LIT>' , <NUM_LIT:32> ) ) <EOL> self . assertEqual ( rows [ <NUM_LIT:1> ] , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertEqual ( rows [ <NUM_LIT:2> ] , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertEqual ( rows [ <NUM_LIT:3> ] , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertEqual ( total_rows , AFTER [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( page_token , AFTER [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( conn . _requested ) , <NUM_LIT:1> ) <EOL> req = conn . _requested [ <NUM_LIT:0> ] <EOL> self . assertEqual ( req [ '<STR_LIT>' ] , '<STR_LIT:GET>' ) <EOL> self . assertEqual ( req [ '<STR_LIT:path>' ] , '<STR_LIT>' % PATH ) <EOL> def test_fetch_data_w_alternate_client ( self ) : <EOL> PATH = '<STR_LIT>' % ( self . PROJECT , self . JOB_NAME ) <EOL> MAX = <NUM_LIT:10> <EOL> TOKEN = '<STR_LIT>' <EOL> START = <NUM_LIT> <EOL> TIMEOUT = <NUM_LIT> <EOL> BEFORE = self . _makeResource ( complete = False ) <EOL> AFTER = self . _makeResource ( complete = True ) <EOL> conn1 = _Connection ( ) <EOL> client1 = _Client ( project = self . PROJECT , connection = conn1 ) <EOL> conn2 = _Connection ( AFTER ) <EOL> client2 = _Client ( project = self . PROJECT , connection = conn2 ) <EOL> query = self . _makeOne ( self . QUERY , client1 ) <EOL> query . _set_properties ( BEFORE ) <EOL> self . assertFalse ( query . complete ) <EOL> rows , total_rows , page_token = query . fetch_data ( <EOL> client = client2 , max_results = MAX , page_token = TOKEN , <EOL> start_index = START , timeout_ms = TIMEOUT ) <EOL> self . assertTrue ( query . complete ) <EOL> self . assertEqual ( len ( rows ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( rows [ <NUM_LIT:0> ] , ( '<STR_LIT>' , <NUM_LIT:32> ) ) <EOL> self . assertEqual ( rows [ <NUM_LIT:1> ] , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertEqual ( rows [ <NUM_LIT:2> ] , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertEqual ( rows [ <NUM_LIT:3> ] , ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> self . assertEqual ( total_rows , AFTER [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( page_token , AFTER [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( conn1 . _requested ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( len ( conn2 . _requested ) , <NUM_LIT:1> ) <EOL> req = conn2 . _requested [ <NUM_LIT:0> ] <EOL> self . assertEqual ( req [ '<STR_LIT>' ] , '<STR_LIT:GET>' ) <EOL> self . assertEqual ( req [ '<STR_LIT:path>' ] , '<STR_LIT>' % PATH ) <EOL> self . assertEqual ( req [ '<STR_LIT>' ] , <EOL> { '<STR_LIT>' : MAX , <EOL> '<STR_LIT>' : TOKEN , <EOL> '<STR_LIT>' : START , <EOL> '<STR_LIT>' : TIMEOUT } ) <EOL> class _Client ( object ) : <EOL> def __init__ ( self , project = '<STR_LIT>' , connection = None ) : <EOL> self . project = project <EOL> self . connection = connection <EOL> def dataset ( self , name ) : <EOL> from gcloud . bigquery . dataset import Dataset <EOL> return Dataset ( name , client = self ) <EOL> class _Connection ( object ) : <EOL> def __init__ ( self , * responses ) : <EOL> self . _responses = responses <EOL> self . _requested = [ ] <EOL> def api_request ( self , ** kw ) : <EOL> from gcloud . exceptions import NotFound <EOL> self . _requested . append ( kw ) <EOL> response , self . _responses = self . _responses [ <NUM_LIT:0> ] , self . _responses [ <NUM_LIT:1> : ] <EOL> return response </s>
