12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- import time
16
-
17
15
import pytest
18
- try :
19
- import IPython
20
- from IPython .testing import tools
21
- from IPython .terminal import interactiveshell
22
- except ImportError : # pragma: NO COVER
23
- IPython = None
16
+ import IPython
17
+ from IPython .testing import tools
18
+ from IPython .terminal import interactiveshell
19
+ import time
24
20
25
21
26
22
@pytest .fixture (scope = 'session' )
@@ -56,12 +52,6 @@ def _strip_region_tags(sample_text):
56
52
return '\n ' .join (magic_lines )
57
53
58
54
59
- def _run_magic_sample (sample , ip ):
60
- result = ip .run_cell (_strip_region_tags (sample ))
61
- result .raise_error () # Throws an exception if the cell failed.
62
-
63
-
64
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
65
55
def test_datalab_query_magic (ipython ):
66
56
ip = _set_up_ipython ('google.datalab.kernel' )
67
57
@@ -74,10 +64,9 @@ def test_datalab_query_magic(ipython):
74
64
ORDER BY count ASC
75
65
# [END bigquery_migration_datalab_query_magic]
76
66
"""
77
- _run_magic_sample ( sample , ip )
67
+ ip . run_cell ( _strip_region_tags ( sample ) )
78
68
79
69
80
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
81
70
def test_client_library_query_magic (ipython ):
82
71
ip = _set_up_ipython ('google.cloud.bigquery' )
83
72
@@ -90,94 +79,132 @@ def test_client_library_query_magic(ipython):
90
79
ORDER BY count ASC
91
80
# [END bigquery_migration_client_library_query_magic]
92
81
"""
93
- _run_magic_sample ( sample , ip )
82
+ ip . run_cell ( _strip_region_tags ( sample ) )
94
83
95
84
96
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
97
85
def test_datalab_query_magic_results_variable (ipython ):
98
86
ip = _set_up_ipython ('google.datalab.kernel' )
99
87
100
88
sample = """
101
- # [START bigquery_migration_datalab_query_magic_results_variable]
102
- %%bq --name my_variable
103
- SELECT word, SUM(word_count) as count
104
- FROM `bigquery-public-data.samples.shakespeare`
105
- GROUP BY word
106
- ORDER BY count ASC
107
- # [END bigquery_migration_datalab_query_magic_results_variable]
89
+ # [START bigquery_migration_datalab_query_magic_define_query]
90
+ %%bq query -n my_query
91
+ SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current`
92
+ WHERE state = "TX"
93
+ LIMIT 100
94
+ # [END bigquery_migration_datalab_query_magic_define_query]
108
95
"""
109
- _run_magic_sample (sample , ip )
96
+ ip .run_cell (_strip_region_tags (sample ))
97
+
98
+ sample = """
99
+ # [START bigquery_migration_datalab_execute_query]
100
+ import google.datalab.bigquery as bq
101
+
102
+ my_variable = my_query.execute().result().to_dataframe()
103
+ # [END bigquery_migration_datalab_execute_query]
104
+ """
105
+ ip .run_cell (_strip_region_tags (sample ))
106
+
107
+ variable_name = "my_variable"
108
+ assert variable_name in ip .user_ns # verify that variable exists
109
+ my_variable = ip .user_ns [variable_name ]
110
+ assert len (my_variable ) == 100
111
+ ip .user_ns .pop (variable_name ) # clean up variable
110
112
111
113
112
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
113
114
def test_client_library_query_magic_results_variable (ipython ):
114
115
ip = _set_up_ipython ('google.cloud.bigquery' )
115
116
116
117
sample = """
117
118
# [START bigquery_migration_client_library_query_magic_results_variable]
118
119
%%bigquery my_variable
119
- SELECT word, SUM(word_count) as count
120
- FROM `bigquery-public-data.samples.shakespeare`
121
- GROUP BY word
122
- ORDER BY count ASC
120
+ SELECT name FROM `bigquery-public-data.usa_names.usa_1910_current`
121
+ WHERE state = "TX"
122
+ LIMIT 100
123
123
# [END bigquery_migration_client_library_query_magic_results_variable]
124
124
"""
125
- _run_magic_sample (sample , ip )
125
+ ip .run_cell (_strip_region_tags (sample ))
126
+
127
+ variable_name = "my_variable"
128
+ assert variable_name in ip .user_ns # verify that variable exists
129
+ my_variable = ip .user_ns [variable_name ]
130
+ assert len (my_variable ) == 100
131
+ ip .user_ns .pop (variable_name ) # clean up variable
126
132
127
133
128
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
129
134
def test_datalab_magic_parameterized_query (ipython ):
135
+ import pandas
136
+
130
137
ip = _set_up_ipython ('google.datalab.kernel' )
131
138
132
139
sample = """
133
140
# [START bigquery_migration_datalab_magic_define_parameterized_query]
134
- %%bq query -n my_variable
141
+ %%bq query -n my_query
135
142
SELECT word, SUM(word_count) as count
136
143
FROM `bigquery-public-data.samples.shakespeare`
137
144
WHERE corpus = @corpus_name
138
145
GROUP BY word
139
146
ORDER BY count ASC
147
+ LIMIT @limit
140
148
# [END bigquery_migration_datalab_magic_define_parameterized_query]
141
149
"""
142
- _run_magic_sample (sample , ip )
150
+ ip .run_cell (_strip_region_tags (sample ))
151
+
152
+ sample = """
153
+ # [START bigquery_migration_datalab_magic_query_params]
154
+ corpus_name = "hamlet"
155
+ limit = 10
156
+ # [END bigquery_migration_datalab_magic_query_params]
157
+ """
158
+ ip .run_cell (_strip_region_tags (sample ))
143
159
144
160
sample = """
145
161
# [START bigquery_migration_datalab_magic_execute_parameterized_query]
146
- %%bq execute -q endpoint_stats
162
+ %%bq execute -q my_query --to-dataframe
147
163
parameters:
148
164
- name: corpus_name
149
165
type: STRING
150
- value: hamlet
166
+ value: $corpus_name
167
+ - name: limit
168
+ type: INTEGER
169
+ value: $limit
151
170
# [END bigquery_migration_datalab_magic_execute_parameterized_query]
152
171
"""
153
- _run_magic_sample (sample , ip )
172
+ ip .run_cell (_strip_region_tags (sample ))
173
+ df = ip .user_ns ["_" ] # Retrieves last returned object in notebook session
174
+ assert isinstance (df , pandas .DataFrame )
175
+ assert len (df ) == 10
154
176
155
177
156
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
157
- def test_query_magic_parameterized_query (ipython ):
178
+ def test_client_library_magic_parameterized_query (ipython ):
179
+ import pandas
180
+
158
181
ip = _set_up_ipython ('google.cloud.bigquery' )
159
182
160
183
sample = """
161
184
# [START bigquery_migration_client_library_magic_query_params]
162
- params = {"corpus_name": "hamlet"}
185
+ params = {"corpus_name": "hamlet", "limit": 10 }
163
186
# [END bigquery_migration_client_library_magic_query_params]
164
187
"""
165
- _run_magic_sample ( sample , ip )
188
+ ip . run_cell ( _strip_region_tags ( sample ) )
166
189
167
190
sample = """
168
191
# [START bigquery_migration_client_library_magic_parameterized_query]
169
- %%bigquery my_variable --params $params
192
+ %%bigquery --params $params
170
193
SELECT word, SUM(word_count) as count
171
194
FROM `bigquery-public-data.samples.shakespeare`
172
195
WHERE corpus = @corpus_name
173
196
GROUP BY word
174
197
ORDER BY count ASC
198
+ LIMIT @limit
175
199
# [END bigquery_migration_client_library_magic_parameterized_query]
176
200
"""
177
- _run_magic_sample (sample , ip )
201
+ ip .run_cell (_strip_region_tags (sample ))
202
+
203
+ df = ip .user_ns ["_" ] # Retrieves last returned object in notebook session
204
+ assert isinstance (df , pandas .DataFrame )
205
+ assert len (df ) == 10
178
206
179
207
180
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
181
208
def test_datalab_list_tables_magic (ipython ):
182
209
ip = _set_up_ipython ('google.datalab.kernel' )
183
210
@@ -186,26 +213,11 @@ def test_datalab_list_tables_magic(ipython):
186
213
%bq tables list --dataset bigquery-public-data.samples
187
214
# [END bigquery_migration_datalab_list_tables_magic]
188
215
"""
189
- _run_magic_sample (sample , ip )
190
-
216
+ ip .run_cell (_strip_region_tags (sample ))
191
217
192
- @pytest .mark .skipif (IPython is None , reason = "Requires `ipython`" )
193
- def test_command_line_interface (ipython ):
194
- ip = IPython .get_ipython ()
195
-
196
- sample = """
197
- # [START bigquery_migration_command_line_list_tables]
198
- !bq ls bigquery-public-data:samples
199
- # [END bigquery_migration_command_line_list_tables]
200
- """
201
- _run_magic_sample (sample , ip )
202
-
203
- sample = """
204
- # [START bigquery_migration_command_line_help]
205
- !bq help
206
- # [END bigquery_migration_command_line_help]
207
- """
208
- _run_magic_sample (sample , ip )
218
+ # Retrieves last returned object in notebook session
219
+ html_element = ip .user_ns ["_" ]
220
+ assert "shakespeare" in html_element .data
209
221
210
222
211
223
def test_datalab_query ():
0 commit comments