12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import os
16
15
import time
17
16
18
- from google .cloud import bigquery
19
17
import pytest
20
18
try :
21
19
import IPython
25
23
IPython = None
26
24
27
25
28
- @pytest .fixture
29
- def temp_dataset ():
30
- client = bigquery .Client ()
31
- dataset_id = "temp_dataset_{}" .format (int (time .time () * 1000 ))
32
- dataset_ref = bigquery .DatasetReference (client .project , dataset_id )
33
- dataset = client .create_dataset (bigquery .Dataset (dataset_ref ))
34
- yield dataset
35
- client .delete_dataset (dataset , delete_contents = True )
36
-
37
-
38
26
@pytest .fixture (scope = 'session' )
39
27
def ipython ():
40
28
config = tools .default_config ()
@@ -43,16 +31,6 @@ def ipython():
43
31
return shell
44
32
45
33
46
- @pytest .fixture ()
47
- def ipython_interactive (request , ipython ):
48
- """Activate IPython's builtin hooks
49
-
50
- for the duration of the test scope.
51
- """
52
- with ipython .builtin_trap :
53
- yield ipython
54
-
55
-
56
34
@pytest .fixture
57
35
def to_delete ():
58
36
from google .cloud import bigquery
@@ -65,9 +43,9 @@ def to_delete():
65
43
client .delete_dataset (dataset , delete_contents = True )
66
44
67
45
68
- def _set_up_ipython ():
46
+ def _set_up_ipython (extension ):
69
47
ip = IPython .get_ipython ()
70
- ip .extension_manager .load_extension ('google.cloud.bigquery' )
48
+ ip .extension_manager .load_extension (extension )
71
49
return ip
72
50
73
51
@@ -84,11 +62,10 @@ def _run_magic_sample(sample, ip):
84
62
85
63
86
64
@pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
87
- def test_query_magic (ipython ):
88
- ip = _set_up_ipython ()
65
+ def test_datalab_query_magic (ipython ):
66
+ ip = _set_up_ipython ('google.datalab.kernel' )
89
67
90
- # Datalab sample
91
- """
68
+ sample = """
92
69
# [START bigquery_migration_datalab_query_magic]
93
70
%%bq
94
71
SELECT word, SUM(word_count) as count
@@ -97,6 +74,12 @@ def test_query_magic(ipython):
97
74
ORDER BY count ASC
98
75
# [END bigquery_migration_datalab_query_magic]
99
76
"""
77
+ _run_magic_sample (sample , ip )
78
+
79
+
80
+ @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
81
+ def test_client_library_query_magic (ipython ):
82
+ ip = _set_up_ipython ('google.cloud.bigquery' )
100
83
101
84
sample = """
102
85
# [START bigquery_migration_client_library_query_magic]
@@ -111,11 +94,10 @@ def test_query_magic(ipython):
111
94
112
95
113
96
@pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
114
- def test_query_magic_results_variable (ipython ):
115
- ip = _set_up_ipython ()
97
+ def test_datalab_query_magic_results_variable (ipython ):
98
+ ip = _set_up_ipython ('google.datalab.kernel' )
116
99
117
- # Datalab sample
118
- """
100
+ sample = """
119
101
# [START bigquery_migration_datalab_query_magic_results_variable]
120
102
%%bq --name my_variable
121
103
SELECT word, SUM(word_count) as count
@@ -124,6 +106,12 @@ def test_query_magic_results_variable(ipython):
124
106
ORDER BY count ASC
125
107
# [END bigquery_migration_datalab_query_magic_results_variable]
126
108
"""
109
+ _run_magic_sample (sample , ip )
110
+
111
+
112
+ @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
113
+ def test_client_library_query_magic_results_variable (ipython ):
114
+ ip = _set_up_ipython ('google.cloud.bigquery' )
127
115
128
116
sample = """
129
117
# [START bigquery_migration_client_library_query_magic_results_variable]
@@ -138,33 +126,41 @@ def test_query_magic_results_variable(ipython):
138
126
139
127
140
128
@pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
141
- def test_query_magic_parameterized_query (ipython ):
142
- ip = _set_up_ipython ()
129
+ def test_datalab_magic_parameterized_query (ipython ):
130
+ ip = _set_up_ipython ('google.datalab.kernel' )
143
131
144
- # Datalab samples
145
- """
146
- # [START bigquery_migration_datalab_magic_parameterized_query_define]
132
+ sample = """
133
+ # [START bigquery_migration_datalab_magic_define_parameterized_query]
147
134
%%bq query -n my_variable
148
135
SELECT word, SUM(word_count) as count
149
136
FROM `bigquery-public-data.samples.shakespeare`
150
137
WHERE corpus = @corpus_name
151
138
GROUP BY word
152
139
ORDER BY count ASC
153
- # [END bigquery_migration_datalab_magic_parameterized_query_define]
140
+ # [END bigquery_migration_datalab_magic_define_parameterized_query]
141
+ """
142
+ _run_magic_sample (sample , ip )
154
143
155
- # [START bigquery_migration_datalab_magic_parameterized_query_execute]
144
+ sample = """
145
+ # [START bigquery_migration_datalab_magic_execute_parameterized_query]
156
146
%%bq execute -q endpoint_stats
157
147
parameters:
158
148
- name: corpus_name
159
149
type: STRING
160
150
value: hamlet
161
- # [END bigquery_migration_datalab_magic_parameterized_query_execute ]
151
+ # [END bigquery_migration_datalab_magic_execute_parameterized_query ]
162
152
"""
153
+ _run_magic_sample (sample , ip )
154
+
155
+
156
+ @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
157
+ def test_query_magic_parameterized_query (ipython ):
158
+ ip = _set_up_ipython ('google.cloud.bigquery' )
163
159
164
160
sample = """
165
- # [START bigquery_migration_client_library_magic_parameterized_query_define_parameter ]
161
+ # [START bigquery_migration_client_library_magic_query_params ]
166
162
params = {"corpus_name": "hamlet"}
167
- # [END bigquery_migration_client_library_magic_parameterized_query_define_parameter ]
163
+ # [END bigquery_migration_client_library_magic_query_params ]
168
164
"""
169
165
_run_magic_sample (sample , ip )
170
166
@@ -182,20 +178,25 @@ def test_query_magic_parameterized_query(ipython):
182
178
183
179
184
180
@pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
185
- def test_command_line_interface (ipython ):
186
- ip = IPython . get_ipython ( )
181
+ def test_datalab_list_tables_magic (ipython ):
182
+ ip = _set_up_ipython ( 'google.datalab.kernel' )
187
183
188
- # Datalab sample
189
- """
184
+ sample = """
190
185
# [START bigquery_migration_datalab_list_tables_magic]
191
186
%bq tables list --dataset bigquery-public-data.samples
192
187
# [END bigquery_migration_datalab_list_tables_magic]
193
188
"""
189
+ _run_magic_sample (sample , ip )
190
+
191
+
192
+ @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
193
+ def test_command_line_interface (ipython ):
194
+ ip = IPython .get_ipython ()
194
195
195
196
sample = """
196
- # [START bigquery_migration_datalab_list_tables_magic ]
197
+ # [START bigquery_migration_command_line_list_tables ]
197
198
!bq ls bigquery-public-data:samples
198
- # [END bigquery_migration_datalab_list_tables_magic ]
199
+ # [END bigquery_migration_command_line_list_tables ]
199
200
"""
200
201
_run_magic_sample (sample , ip )
201
202
@@ -260,7 +261,7 @@ def test_datalab_load_table_from_gcs_csv(to_delete):
260
261
'gs://cloud-samples-data/bigquery/us-states/us-states.csv' ,
261
262
mode = 'append' ,
262
263
source_format = 'csv' ,
263
- csv_options = bq .CSVOptions (skip_leading_rows = 1 )
264
+ csv_options = bq .CSVOptions (skip_leading_rows = 1 )
264
265
) # Waits for the job to complete
265
266
# [END bigquery_migration_datalab_load_table_from_gcs_csv]
266
267
@@ -285,13 +286,13 @@ def test_client_library_load_table_from_gcs_csv(to_delete):
285
286
286
287
# Create the table
287
288
job_config = bigquery .LoadJobConfig (
288
- schema = [
289
+ schema = [
289
290
bigquery .SchemaField ('name' , 'STRING' ),
290
291
bigquery .SchemaField ('post_abbr' , 'STRING' )
291
292
],
292
- skip_leading_rows = 1 ,
293
+ skip_leading_rows = 1 ,
293
294
# The source format defaults to CSV, so the line below is optional.
294
- source_format = bigquery .SourceFormat .CSV
295
+ source_format = bigquery .SourceFormat .CSV
295
296
)
296
297
load_job = client .load_table_from_uri (
297
298
'gs://cloud-samples-data/bigquery/us-states/us-states.csv' ,
@@ -330,15 +331,16 @@ def test_datalab_load_table_from_dataframe(to_delete):
330
331
},
331
332
])
332
333
schema = bq .Schema .from_data (dataframe )
333
- table = bq .Table ('{}.monty_python' .format (dataset_id )).create (schema = schema )
334
+ table = bq .Table (
335
+ '{}.monty_python' .format (dataset_id )).create (schema = schema )
334
336
table .insert (dataframe ) # Starts steaming insert of data
335
337
# [END bigquery_migration_datalab_load_table_from_dataframe]
336
338
# The Datalab library uses tabledata().insertAll() to load data from
337
339
# pandas DataFrames to tables. Because it can take a long time for the rows
338
340
# to be available in the table, this test does not assert on the number of
339
341
# rows in the destination table after the job is run. If errors are
340
342
# encountered during the insertion, this test will fail.
341
- # See https://cloud.google.com/bigquery/streaming-data-into-bigquery#dataavailability
343
+ # See https://cloud.google.com/bigquery/streaming-data-into-bigquery
342
344
343
345
344
346
def test_client_library_load_table_from_dataframe (to_delete ):
@@ -375,4 +377,3 @@ def test_client_library_load_table_from_dataframe(to_delete):
375
377
376
378
table = client .get_table (dataset .table ('monty_python' ))
377
379
assert table .num_rows == 4
378
-
0 commit comments