Skip to content

Commit 045e6f8

Browse files
author
Bill Prin
committed
Use context manager to mock raw_input instead of patch
1 parent 357567a commit 045e6f8

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

bigquery/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ 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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
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
23-
2422

2523
# [START async_query]
2624
def async_query(service, project_id, query, batch=False, num_retries=5):
@@ -71,14 +69,14 @@ def run(project_id, query_string, batch, num_retries, interval):
7169

7270
# [START main]
7371
def main():
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 (
72+
project_id = raw_input("Enter the project ID: ")
73+
query_string = raw_input("Enter the Bigquery SQL Query: ")
74+
batch = raw_input("Run query as batch (y/n)?: ") in (
7775
'True', 'true', 'y', 'Y', 'yes', 'Yes')
7876

79-
num_retries = get_input(
77+
num_retries = raw_input(
8078
"Enter number of times to retry in case of 500 error: ")
81-
interval = get_input(
79+
interval = raw_input(
8280
"Enter how often to poll the query for completion (seconds): ")
8381
for result in run(project_id, query_string, batch, num_retries, interval):
8482
print(result)

bigquery/samples/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,3 @@ def paging(service, request_func, num_retries=5, **kwargs):
4949
yield response
5050
# [END paging]
5151

52-
def get_input(text):
53-
return input(text)

bigquery/tests/test_async_query.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
import unittest
1717

1818
from bigquery.samples.async_query import run, main
19-
from tests import CloudBaseTest, BUCKET_NAME_ENV, PROJECT_ID_ENV
20-
from mock import patch
21-
19+
from tests import CloudBaseTest, mock_raw_input, BUCKET_NAME_ENV, PROJECT_ID_ENV
2220

2321
class TestAsyncQuery(CloudBaseTest):
2422

@@ -31,23 +29,23 @@ def test_async_query(self):
3129
self.assertIsNotNone(json.loads(result))
3230

3331

34-
class TestAsyncRunner(CloudBaseTest):
32+
def mock_get_input(input):
33+
test_bucket_name = os.environ.get(BUCKET_NAME_ENV)
34+
test_project_id = os.environ.get(PROJECT_ID_ENV)
35+
answers = [test_bucket_name, test_project_id, 'n',
36+
'1', '1']
37+
ret = answers[TestAsyncRunner.i]
38+
TestAsyncRunner.i += 1
39+
return ret
3540

36-
i = 0
3741

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
42+
class TestAsyncRunner(CloudBaseTest):
4643

44+
i = 0
4745

48-
@patch('bigquery.samples.async_query.get_input', new=mock_get_input)
4946
def test_async_query_runner(self):
50-
main()
47+
with mock_raw_input(mock_get_input):
48+
main()
5149

5250

5351
if __name__ == '__main__':

tests/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@
1818
import json
1919
import os
2020
import unittest
21-
21+
from contextlib import contextmanager
22+
import __builtin__
2223

2324
BUCKET_NAME_ENV = 'TEST_BUCKET_NAME'
2425
PROJECT_ID_ENV = 'TEST_PROJECT_ID'
2526
RESOURCE_PATH = os.path.join(
2627
os.path.abspath(os.path.dirname(__file__)), 'resources')
2728

2829

30+
@contextmanager
31+
def mock_raw_input(mock):
32+
original_raw_input = __builtin__.raw_input
33+
__builtin__.raw_input = mock
34+
yield
35+
__builtin__.raw_input = original_raw_input
36+
37+
2938
class CloudBaseTest(unittest.TestCase):
3039

3140
def setUp(self):

0 commit comments

Comments
 (0)