Skip to content

Commit 357567a

Browse files
author
Bill Prin
committed
Adds Regression Test By Mocking Input
1 parent c58e810 commit 357567a

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

bigquery/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ tox==1.9.0
1212
uritemplate==0.6
1313
virtualenv==12.0.7
1414
wsgiref==0.1.2
15+
mock==1.0.1

bigquery/samples/async_query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from bigquery.samples.utils import get_service
2020
from bigquery.samples.utils import paging
2121
from bigquery.samples.utils import poll_job
22+
from bigquery.samples.utils import get_input
2223

2324

2425
# [START async_query]
@@ -70,16 +71,15 @@ def run(project_id, query_string, batch, num_retries, interval):
7071

7172
# [START main]
7273
def main():
73-
project_id = raw_input("Enter the project ID: ")
74-
query_string = raw_input("Enter the Bigquery SQL Query: ")
75-
batch = raw_input("Run query as batch (y/n)?: ") in (
74+
project_id = get_input("Enter the project ID: ")
75+
query_string = get_input("Enter the Bigquery SQL Query: ")
76+
batch = get_input("Run query as batch (y/n)?: ") in (
7677
'True', 'true', 'y', 'Y', 'yes', 'Yes')
7778

78-
num_retries = raw_input(
79+
num_retries = get_input(
7980
"Enter number of times to retry in case of 500 error: ")
80-
interval = raw_input(
81+
interval = get_input(
8182
"Enter how often to poll the query for completion (seconds): ")
82-
8383
for result in run(project_id, query_string, batch, num_retries, interval):
8484
print(result)
8585
# [END main]

bigquery/samples/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ def paging(service, request_func, num_retries=5, **kwargs):
4848
has_next = False
4949
yield response
5050
# [END paging]
51+
52+
def get_input(text):
53+
return input(text)

bigquery/tests/test_async_query.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
# limitations under the License.
1313
#
1414
import json
15+
import os
1516
import unittest
1617

17-
from bigquery.samples.async_query import run
18-
from tests import CloudBaseTest
18+
from bigquery.samples.async_query import run, main
19+
from tests import CloudBaseTest, BUCKET_NAME_ENV, PROJECT_ID_ENV
20+
from mock import patch
1921

2022

2123
class TestAsyncQuery(CloudBaseTest):
@@ -29,5 +31,24 @@ def test_async_query(self):
2931
self.assertIsNotNone(json.loads(result))
3032

3133

34+
class TestAsyncRunner(CloudBaseTest):
35+
36+
i = 0
37+
38+
def mock_get_input(input):
39+
test_bucket_name = os.environ.get(BUCKET_NAME_ENV)
40+
test_project_id = os.environ.get(PROJECT_ID_ENV)
41+
answers = [test_bucket_name, test_project_id, 'n',
42+
'1', '1']
43+
ret = answers[TestAsyncRunner.i]
44+
TestAsyncRunner.i += 1
45+
return ret
46+
47+
48+
@patch('bigquery.samples.async_query.get_input', new=mock_get_input)
49+
def test_async_query_runner(self):
50+
main()
51+
52+
3253
if __name__ == '__main__':
3354
unittest.main()

0 commit comments

Comments
 (0)