Skip to content

Commit a2cd34b

Browse files
committed
Merge pull request #32 from GoogleCloudPlatform/regression
Adds Regression Test By Mocking Input
2 parents 78079cd + 09961a7 commit a2cd34b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

bigquery/tests/test_async_query.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +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
19-
18+
from bigquery.samples.async_query import run, main
19+
from tests import CloudBaseTest, mock_raw_input, BUCKET_NAME_ENV, \
20+
PROJECT_ID_ENV
2021

2122
class TestAsyncQuery(CloudBaseTest):
2223

@@ -29,5 +30,16 @@ def test_async_query(self):
2930
self.assertIsNotNone(json.loads(result))
3031

3132

33+
class TestAsyncRunner(CloudBaseTest):
34+
35+
def test_async_query_runner(self):
36+
test_bucket_name = os.environ.get(BUCKET_NAME_ENV)
37+
test_project_id = os.environ.get(PROJECT_ID_ENV)
38+
answers = [test_bucket_name, test_project_id, 'n',
39+
'1', '1']
40+
with mock_raw_input(answers):
41+
main()
42+
43+
3244
if __name__ == '__main__':
3345
unittest.main()

tests/__init__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,33 @@
1818
import json
1919
import os
2020
import unittest
21-
21+
import __builtin__
2222

2323
BUCKET_NAME_ENV = 'TEST_BUCKET_NAME'
2424
PROJECT_ID_ENV = 'TEST_PROJECT_ID'
2525
RESOURCE_PATH = os.path.join(
2626
os.path.abspath(os.path.dirname(__file__)), 'resources')
2727

2828

29+
class mock_raw_input(object):
30+
31+
def __init__(self, list_):
32+
self.i = 0
33+
self.list_ = list_
34+
35+
def get_next_value(self, question):
36+
ret = self.list_[self.i]
37+
self.i += 1
38+
return ret
39+
40+
def __enter__(self):
41+
self.raw_input_cache = __builtin__.raw_input
42+
__builtin__.raw_input = self.get_next_value
43+
44+
def __exit__(self, exc_type, exc_value, traceback):
45+
__builtin__.raw_input = self.raw_input_cache
46+
47+
2948
class CloudBaseTest(unittest.TestCase):
3049

3150
def setUp(self):

0 commit comments

Comments
 (0)