Skip to content

Commit 8fba904

Browse files
lbristol88tswast
authored andcommitted
Created new route for results and added redirect for main. (#2069)
* Set a timeout when waiting for query job to finish. * Transferred results from main function into its own route. Added redirect from main.
1 parent 53e3d0c commit 8fba904

File tree

2 files changed

+64
-16
lines changed

2 files changed

+64
-16
lines changed

appengine/standard_python37/bigquery/main.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
# [START gae_python37_bigquery]
1616
import concurrent.futures
1717

18-
from flask import Flask, render_template
18+
import flask
1919
from google.cloud import bigquery
2020

2121

22-
app = Flask(__name__)
22+
app = flask.Flask(__name__)
2323
bigquery_client = bigquery.Client()
2424

2525

26-
@app.route('/')
26+
@app.route("/")
2727
def main():
28-
query_job = bigquery_client.query("""
28+
query_job = bigquery_client.query(
29+
"""
2930
SELECT
3031
CONCAT(
3132
'https://stackoverflow.com/questions/',
@@ -35,20 +36,43 @@ def main():
3536
WHERE tags like '%google-bigquery%'
3637
ORDER BY view_count DESC
3738
LIMIT 10
38-
""")
39+
"""
40+
)
41+
42+
return flask.redirect(
43+
flask.url_for(
44+
"results",
45+
project_id=query_job.project,
46+
job_id=query_job.job_id,
47+
location=query_job.location,
48+
)
49+
)
50+
51+
52+
@app.route("/results")
53+
def results():
54+
project_id = flask.request.args.get("project_id")
55+
job_id = flask.request.args.get("job_id")
56+
location = flask.request.args.get("location")
57+
58+
query_job = bigquery_client.get_job(
59+
job_id,
60+
project=project_id,
61+
location=location,
62+
)
3963

4064
try:
4165
# Set a timeout because queries could take longer than one minute.
4266
results = query_job.result(timeout=30)
4367
except concurrent.futures.TimeoutError:
44-
return render_template('timeout.html', job_id=query_job.job_id)
68+
return flask.render_template("timeout.html", job_id=query_job.job_id)
4569

46-
return render_template('query_result.html', results=results)
70+
return flask.render_template("query_result.html", results=results)
4771

4872

49-
if __name__ == '__main__':
73+
if __name__ == "__main__":
5074
# This is used when running locally only. When deploying to Google App
5175
# Engine, a webserver process such as Gunicorn will serve the app. This
5276
# can be configured by adding an `entrypoint` to app.yaml.
53-
app.run(host='127.0.0.1', port=8080, debug=True)
77+
app.run(host="127.0.0.1", port=8080, debug=True)
5478
# [END gae_python37_bigquery]

appengine/standard_python37/bigquery/main_test.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,46 @@ def flask_client():
2828

2929

3030
def test_main(flask_client):
31-
r = flask_client.get('/')
31+
r = flask_client.get("/")
32+
assert r.status_code == 302
33+
assert "/results" in r.headers.get("location", "")
34+
35+
36+
def test_results(flask_client, monkeypatch):
37+
import main
38+
39+
fake_job = mock.create_autospec(bigquery.QueryJob)
40+
fake_rows = [("example1.com", "42"), ("example2.com", "38")]
41+
fake_job.result.return_value = fake_rows
42+
43+
def fake_get_job(self, job_id, **kwargs):
44+
return fake_job
45+
46+
monkeypatch.setattr(main.bigquery.Client, "get_job", fake_get_job)
47+
48+
r = flask_client.get(
49+
"/results?project_id=123&job_id=456&location=my_location"
50+
)
51+
response_body = r.data.decode("utf-8")
52+
3253
assert r.status_code == 200
33-
assert 'Query Result' in r.data.decode('utf-8')
54+
assert "Query Result" in response_body # verifies header
55+
assert "example2.com" in response_body
56+
assert "42" in response_body
3457

3558

36-
def test_main_timeout(flask_client, monkeypatch):
59+
def test_results_timeout(flask_client, monkeypatch):
3760
import main
3861

3962
fake_job = mock.create_autospec(bigquery.QueryJob)
4063
fake_job.result.side_effect = concurrent.futures.TimeoutError()
4164

42-
def fake_query(query):
65+
def fake_get_job(self, job_id, **kwargs):
4366
return fake_job
4467

45-
monkeypatch.setattr(main.bigquery_client, 'query', fake_query)
68+
monkeypatch.setattr(main.bigquery.Client, "get_job", fake_get_job)
69+
70+
r = flask_client.get("/results", follow_redirects=True)
4671

47-
r = flask_client.get('/')
4872
assert r.status_code == 200
49-
assert 'Query Timeout' in r.data.decode('utf-8')
73+
assert "Query Timeout" in r.data.decode("utf-8")

0 commit comments

Comments
 (0)