@@ -115,8 +115,8 @@ def test_query(self):
115
115
116
116
self .mock_job_collection .query .assert_called_once_with (
117
117
projectId = self .project_id ,
118
- body = {'query' : self .query , 'timeoutMs' : 10000 , 'maxResults ' : None ,
119
- 'dryRun ' : False }
118
+ body = {'query' : self .query , 'timeoutMs' : 10000 , 'dryRun ' : False ,
119
+ 'maxResults ' : None }
120
120
)
121
121
self .assertEquals (job_id , 'spiderman' )
122
122
self .assertEquals (results , [])
@@ -168,8 +168,8 @@ def test_query_timeout(self):
168
168
169
169
self .mock_job_collection .query .assert_called_once_with (
170
170
projectId = self .project_id ,
171
- body = {'query' : self .query , 'timeoutMs' : timeout * 1000 ,
172
- 'maxResults' : None , 'dryRun ' : False }
171
+ body = {'query' : self .query , 'timeoutMs' : timeout * 1000 , 'dryRun' :
172
+ False , 'maxResults ' : None }
173
173
)
174
174
self .assertEquals (job_id , 'spiderman' )
175
175
self .assertEquals (results , [])
@@ -241,8 +241,8 @@ def test_query_with_results(self):
241
241
242
242
self .mock_job_collection .query .assert_called_once_with (
243
243
projectId = self .project_id ,
244
- body = {'query' : self .query , 'timeoutMs' : 10000 , 'maxResults ' : None ,
245
- 'dryRun ' : False }
244
+ body = {'query' : self .query , 'timeoutMs' : 10000 , 'dryRun ' : False ,
245
+ 'maxResults ' : None }
246
246
)
247
247
self .assertEquals (job_id , 'spiderman' )
248
248
self .assertEquals (results , [{'foo' : 10 }])
@@ -564,6 +564,49 @@ def test_query_incomplete(self, get_query_mock):
564
564
self .assertEquals (result_schema , [])
565
565
566
566
567
+ class TestGetTableSchema (unittest .TestCase ):
568
+
569
+ def setUp (self ):
570
+ self .mock_bq_service = mock .Mock ()
571
+ self .mock_tables = mock .Mock ()
572
+ self .mock_bq_service .tables .return_value = self .mock_tables
573
+ self .table = 'table'
574
+ self .project = 'project'
575
+ self .dataset = 'dataset'
576
+ self .client = client .BigQueryClient (self .mock_bq_service , self .project )
577
+
578
+ def test_table_exists (self ):
579
+ """Ensure that the table schema is returned if the table exists."""
580
+
581
+ expected = [
582
+ {'type' : 'FLOAT' , 'name' : 'foo' , 'mode' : 'NULLABLE' },
583
+ {'type' : 'INTEGER' , 'name' : 'bar' , 'mode' : 'NULLABLE' },
584
+ {'type' : 'INTEGER' , 'name' : 'baz' , 'mode' : 'NULLABLE' },
585
+ ]
586
+
587
+ self .mock_tables .get .return_value .execute .return_value = \
588
+ {'schema' : {'fields' : expected }}
589
+
590
+ self .assertEqual (
591
+ expected , self .client .get_table_schema (self .dataset , self .table ))
592
+ self .mock_tables .get .assert_called_once_with (
593
+ projectId = self .project , tableId = self .table , datasetId = self .dataset )
594
+ self .mock_tables .get .return_value .execute .assert_called_once_with ()
595
+
596
+ def test_table_does_not_exist (self ):
597
+ """Ensure that None is returned if the table doesn't exist."""
598
+ from apiclient .errors import HttpError
599
+
600
+ self .mock_tables .get .return_value .execute .side_effect = \
601
+ HttpError ({'status' : "404" }, '{}' )
602
+
603
+ self .assertIsNone (
604
+ self .client .get_table_schema (self .dataset , self .table ))
605
+ self .mock_tables .get .assert_called_once_with (
606
+ projectId = self .project , tableId = self .table , datasetId = self .dataset )
607
+ self .mock_tables .get .return_value .execute .assert_called_once_with ()
608
+
609
+
567
610
@mock .patch ('bigquery.client.BigQueryClient._get_query_results' )
568
611
class TestGetQueryRows (unittest .TestCase ):
569
612
0 commit comments