8000 asserts for all tests · realjordanna/python-docs-samples@1ccfc4a · GitHub
[go: up one dir, main page]

65E0
Skip to content

Commit 1ccfc4a

Browse files
committed
asserts for all tests
1 parent 7870a4a commit 1ccfc4a

File tree

2 files changed

+78
-65
lines changed

2 files changed

+78
-65
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
google-cloud-bigquery[pandas,pyarrow]==1.7.0
22
datalab==1.1.4
3-
ipython==7.2.0
3+
ipython==7.2.0; python_version > '2.7'
4+
ipython==5.5; python_version == '2.7'
45
google-cloud-monitoring==0.28.1

bigquery/datalab-migration/samples_test.py

Lines changed: 76 additions & 64 deletions
+
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import time
16-
1715
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
2420

2521

2622
@pytest.fixture(scope='session')
@@ -56,12 +52,6 @@ def _strip_region_tags(sample_text):
5652
return '\n'.join(magic_lines)
5753

5854

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`")
6555
def test_datalab_query_magic(ipython):
6656
ip = _set_up_ipython('google.datalab.kernel')
6757

@@ -74,10 +64,9 @@ def test_datalab_query_magic(ipython):
7464
ORDER BY count ASC
7565
# [END bigquery_migration_datalab_query_magic]
7666
"""
77-
_run_magic_sample(sample, ip)
67+
ip.run_cell(_strip_region_tags(sample))
7868

7969

80-
@pytest.mark.skipif(IPython is None, reason="Requires `ipython`")
8170
def test_client_library_query_magic(ipython):
8271
ip = _set_up_ipython('google.cloud.bigquery')
8372

@@ -90,94 +79,132 @@ def test_client_library_query_magic(ipython):
9079
ORDER BY count ASC
9180
# [END bigquery_migration_client_library_query_magic]
9281
"""
93-
_run_magic_sample(sample, ip)
82+
ip.run_cell(_strip_region_tags(sample))
9483

9584

96-
@pytest.mark.skipif(IPython is None, reason="Requires `ipython`")
9785
def test_datalab_query_magic_results_variable(ipython):
9886
ip = _set_up_ipython('google.datalab.kernel')
9987

10088
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]
10895
"""
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
110112

111113

112-
@pytest.mark.skipif(IPython is None, reason="Requires `ipython`")
113114
def test_client_library_query_magic_results_variable(ipython):
114115
ip = _set_up_ipython('google.cloud.bigquery')
115116

116117
sample = """
117118
# [START bigquery_migration_client_library_query_magic_results_variable]
118119
%%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
123123
# [END bigquery_migration_client_library_query_magic_results_variable]
124124
"""
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
126132

127133

128-
@pytest.mark.skipif(IPython is None, reason="Requires `ipython`")
129134
def test_datalab_magic_parameterized_query(ipython):
135+
import pandas
136+
130137
ip = _set_up_ipython('google.datalab.kernel')
131138

132139
sample = """
133140
# [START bigquery_migration_datalab_magic_define_parameterized_query]
134-
%%bq query -n my_variable
141+
%%bq query -n my_query
135142
SELECT word, SUM(word_count) as count
136143
FROM `bigquery-public-data.samples.shakespeare`
137144
WHERE corpus = @corpus_name
138145
GROUP BY word
139146
ORDER BY count ASC
147+
LIMIT @limit
140148
# [END bigquery_migration_datalab_magic_define_parameterized_query]
141149
"""
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))
143159

144160
sample = """
145161
# [START bigquery_migration_datalab_magic_execute_parameterized_query]
146-
%%bq execute -q endpoint_stats
162+
%%bq execute -q my_query --to-dataframe
147163
parameters:
148164
- name: corpus_name
149165
type: STRING
150-
value: hamlet
166+
value: $corpus_name
167+
- name: limit
168+
type: INTEGER
169+
value: $limit
151170
# [END bigquery_migration_datalab_magic_execute_parameterized_query]
152171
"""
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
154176

155177

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
158181
ip = _set_up_ipython('google.cloud.bigquery')
159182

160183
sample = """
161184
# [START bigquery_migration_client_library_magic_query_params]
162-
params = {"corpus_name": "hamlet"}
185+
params = {"corpus_name": "hamlet", "limit": 10}
163186
# [END bigquery_migration_client_library_magic_query_params]
164187
"""
165-
_run_magic_sample(sample, ip)
188+
ip.run_cell(_strip_region_tags(sample))
166189

167190
sample = """
168191
# [START bigquery_migration_client_library_magic_parameterized_query]
169-
%%bigquery my_variable --params $params
192+
%%bigquery --params $params
170193
SELECT word, SUM(word_count) as count
171194
FROM `bigquery-public-data.samples.shakespeare`
172195
WHERE corpus = @corpus_name
173196
GROUP BY word
174197
ORDER BY count ASC
198+
LIMIT @limit
175199
# [END bigquery_migration_client_library_magic_parameterized_query]
176200
"""
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
178206

179207

180-
@pytest.mark.skipif(IPython is None, reason="Requires `ipython`")
181208
def test_datalab_list_tables_magic(ipython):
182209
ip = _set_up_ipython('google.datalab.kernel')
183210

@@ -186,26 +213,11 @@ def test_datalab_list_tables_magic(ipython):
186213
%bq tables list --dataset bigquery-public-data.samples
187214
# [END bigquery_migration_datalab_list_tables_magic]
188215
"""
189-
_run_magic_sample(sample, ip)
190-
216+
ip.run_cell(_strip_region_tags(sample))
191217

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
209221

210222

211223
def test_datalab_query():

0 commit comments

Comments
 (0)
0