#NEWLINE# def test_load_table_from_storage_then_dump_table( self ) : #NEWLINE# #INDENT# import csv #NEWLINE# import tempfile #NEWLINE# from gcloud.storage import Client as StorageClient #NEWLINE# local_id = unique_resource_id ( ) #NEWLINE# BUCKET_NAME = ( 'bq_load_test' + local_id ) #NEWLINE# BLOB_NAME = 'person_ages.csv' #NEWLINE# GS_URL = ( 'gs://%s/%s' % ( BUCKET_NAME , BLOB_NAME ) ) #NEWLINE# ROWS = [ ( 'Phred Phlyntstone' , 32 ) , ( 'Bharney Rhubble' , 33 ) , ( 'Wylma Phlyntstone' , 29 ) , ( 'Bhettye Rhubble' , 27 ) ] #NEWLINE# TABLE_NAME = 'test_table' #NEWLINE# s_client = StorageClient ( ) #NEWLINE# bucket = s_client . create_bucket ( BUCKET_NAME ) #NEWLINE# self . to_delete . append ( bucket ) #NEWLINE# blob = bucket . blob ( BLOB_NAME ) #NEWLINE# with tempfile . TemporaryFile ( mode = 'w+' ) as csv_file : #NEWLINE# #INDENT# writer = csv . writer ( csv_file ) #NEWLINE# writer . writerow ( ( 'Full Name' , 'Age' ) ) #NEWLINE# writer . writerows ( ROWS ) #NEWLINE# blob . upload_from_file ( csv_file , rewind = True , content_type = 'text/csv' ) #NEWLINE# #UNINDENT# self . to_delete . insert ( 0 , blob ) #NEWLINE# dataset = Config . CLIENT . dataset ( DATASET_NAME ) #NEWLINE# dataset . create ( ) #NEWLINE# self . to_delete . append ( dataset ) #NEWLINE# full_name = bigquery . SchemaField ( 'full_name' , 'STRING' , mode = 'REQUIRED' ) #NEWLINE# age = bigquery . SchemaField ( 'age' , 'INTEGER' , mode = 'REQUIRED' ) #NEWLINE# table = dataset . table ( TABLE_NAME , schema = [ full_name , age ] ) #NEWLINE# table . create ( ) #NEWLINE# self . to_delete . insert ( 0 , table ) #NEWLINE# job = Config . CLIENT . load_table_from_storage ( ( 'bq_load_storage_test_' + local_id ) , table , GS_URL ) #NEWLINE# job . create_disposition = 'CREATE_NEVER' #NEWLINE# job . skip_leading_rows = 1 #NEWLINE# job . source_format = 'CSV' #NEWLINE# job . write_disposition = 'WRITE_EMPTY' #NEWLINE# job . begin ( ) #NEWLINE# counter = 9 #NEWLINE# while ( ( job . state not in ( 'DONE' , 'done' ) ) and ( counter > 0 ) ) : #NEWLINE# #INDENT# counter -= 1 #NEWLINE# job . reload ( ) #NEWLINE# if ( job . state not in ( 'DONE' , 'done' ) ) : #NEWLINE# #INDENT# time . sleep ( 10 ) #NEWLINE# #UNINDENT# #UNINDENT# self . assertTrue ( ( job . state in ( 'DONE' , 'done' ) ) ) #NEWLINE# ( rows , _ , _ ) = table . fetch_data ( ) #NEWLINE# by_age = operator . itemgetter ( 1 ) #NEWLINE# self . assertEqual ( sorted ( rows , key = by_age ) , sorted ( _ , key = by_age ) )