diff --git a/.travis.yml b/.travis.yml index e5e04c212d0..02d8ae6ca1c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ env: - GOOGLE_APPLICATION_CREDENTIALS=${TRAVIS_BUILD_DIR}/testing/resources/service-account.json - GOOGLE_CLIENT_SECRETS=${TRAVIS_BUILD_DIR}/testing/resources/client-secrets.json - GAE_ROOT=${HOME}/.cache/ - - secure: TQ0e6XKeFwVkoqgOJH9f/afyRouUSC6s7LC32C4JS+O2X4sXyXTPXACmzu5wCW0BXPc6HvITMLvkf7g6XXyGlCPkjM8Uw5Vg5F9+cwN1HMlI+gK6bMGTUfrwN5ruFT+KmEnD4F93NY3xkDbZd0fw23d/mVloTc6V0gUsxEUkuhM= + - secure: f3aU0nf8ZBV2QfZ03oeqvR0f/JM69P/7IH3IGoBcRUWVIXXhQ6Esh9SmCUILPtis1ZKu11I9c+NDebZio7PFgTqfvLbKzAkrg0ucx+Bsyx6379/S1trbLeKunERSGA3GqK6+OCoR5q/9sKxNvlm/c/e9h7xZmPfP5W0qwVR/K0M= addons: apt: sources: diff --git a/bigquery/cloud-client/quickstart_test.py b/bigquery/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..f5842960ce6 --- /dev/null +++ b/bigquery/cloud-client/quickstart_test.py @@ -0,0 +1,45 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from google.cloud import bigquery +import pytest + +import quickstart + + +# Must match the dataset listed in quickstart.py (there's no easy way to +# extract this). +DATASET_ID = 'my_new_dataset' + + +@pytest.fixture +def temporary_dataset(): + """Fixture that ensures the test dataset does not exist before or + after a test.""" + bigquery_client = bigquery.Client() + dataset = bigquery_client.dataset(DATASET_ID) + + if dataset.exists(): + dataset.delete() + + yield + + if dataset.exists(): + dataset.delete() + + +def test_quickstart(capsys, temporary_dataset): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert DATASET_ID in out diff --git a/conftest.py b/conftest.py index 3fa68de6953..8ac8451e0ef 100644 --- a/conftest.py +++ b/conftest.py @@ -30,7 +30,8 @@ def cloud_config(): project=os.environ.get('GCLOUD_PROJECT'), storage_bucket=os.environ.get('CLOUD_STORAGE_BUCKET'), client_secrets=os.environ.get('GOOGLE_CLIENT_SECRETS'), - bigtable_instance=os.environ.get('BIGTABLE_CLUSTER')) + bigtable_instance=os.environ.get('BIGTABLE_CLUSTER'), + api_key=os.environ.get('API_KEY')) def get_resource_path(resource, local_path): diff --git a/datastore/api/quickstart_test.py b/datastore/api/quickstart_test.py new file mode 100644 index 00000000000..8545dfa04de --- /dev/null +++ b/datastore/api/quickstart_test.py @@ -0,0 +1,21 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import quickstart + + +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'Saved' in out diff --git a/datastore/api/snippets.py b/datastore/api/snippets.py index 22d5e35e526..211592b5919 100644 --- a/datastore/api/snippets.py +++ b/datastore/api/snippets.py @@ -539,7 +539,8 @@ def inequality_invalid(client): return list(query.fetch()) - except google.cloud.exceptions.BadRequest: + except (google.cloud.exceptions.BadRequest, + google.cloud.exceptions.GrpcRendezvous): pass @@ -579,7 +580,8 @@ def inequality_sort_invalid_not_same(client): return list(query.fetch()) - except google.cloud.exceptions.BadRequest: + except (google.cloud.exceptions.BadRequest, + google.cloud.exceptions.GrpcRendezvous): pass @@ -593,7 +595,8 @@ def inequality_sort_invalid_not_first(client): return list(query.fetch()) - except google.cloud.exceptions.BadRequest: + except (google.cloud.exceptions.BadRequest, + google.cloud.exceptions.GrpcRendezvous): pass diff --git a/language/cloud-client/quickstart_test.py b/language/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..bd9954c83bb --- /dev/null +++ b/language/cloud-client/quickstart_test.py @@ -0,0 +1,22 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import quickstart + + +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'Sentiment' in out diff --git a/logging/cloud-client/quickstart_test.py b/logging/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..1b49cd1263a --- /dev/null +++ b/logging/cloud-client/quickstart_test.py @@ -0,0 +1,22 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import quickstart + + +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'Logged' in out diff --git a/pubsub/cloud-client/quickstart_test.py b/pubsub/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..bbb3bd75f25 --- /dev/null +++ b/pubsub/cloud-client/quickstart_test.py @@ -0,0 +1,45 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from google.cloud import pubsub +import pytest + +import quickstart + + +# Must match the dataset listed in quickstart.py (there's no easy way to +# extract this). +TOPIC_NAME = 'my-new-topic' + + +@pytest.fixture +def temporary_topic(): + """Fixture that ensures the test dataset does not exist before or + after a test.""" + pubsub_client = pubsub.Client() + topic = pubsub_client.topic(TOPIC_NAME) + + if topic.exists(): + topic.delete() + + yield + + if topic.exists(): + topic.delete() + + +def test_quickstart(capsys, temporary_topic): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert TOPIC_NAME in out diff --git a/secrets.tar.enc b/secrets.tar.enc index dbf6a450242..5354a881a5c 100644 Binary files a/secrets.tar.enc and b/secrets.tar.enc differ diff --git a/storage/cloud-client/quickstart_test.py b/storage/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..cb0503972ff --- /dev/null +++ b/storage/cloud-client/quickstart_test.py @@ -0,0 +1,28 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import mock + +import quickstart + + +@mock.patch('google.cloud.storage.client.Client.create_bucket') +def test_quickstart(create_bucket_mock, capsys): + # Unlike other quickstart tests, this one mocks out the creation + # because buckets are expensive, globally-namespaced object. + create_bucket_mock.return_value = mock.sentinel.bucket + + quickstart.run_quickstart() + + create_bucket_mock.assert_called_with('my-new-bucket') diff --git a/testing/resources/test-env.tmpl.sh b/testing/resources/test-env.tmpl.sh index 65a53826f8f..0734f19fa27 100644 --- a/testing/resources/test-env.tmpl.sh +++ b/testing/resources/test-env.tmpl.sh @@ -1,6 +1,7 @@ # Environment variables for system tests. export GCLOUD_PROJECT=your-project-id export CLOUD_STORAGE_BUCKET=$GCLOUD_PROJECT +export API_KEY= export BIGTABLE_CLUSTER=bigtable-test export BIGTABLE_ZONE=us-central1-c diff --git a/translate/cloud-client/quickstart.py b/translate/cloud-client/quickstart.py index ad5436767dd..51ab697c8f0 100644 --- a/translate/cloud-client/quickstart.py +++ b/translate/cloud-client/quickstart.py @@ -27,16 +27,15 @@ def run_quickstart(): translate_client = translate.Client(api_key) # The text to translate - text = 'Hello, world!' + text = u'Hello, world!' # The target language target = 'ru' # Translates some text into Russian translation = translate_client.translate(text, target_language=target) - print('Text: {}'.format(text)) - print('Translation: {}'.format( - translation['translatedText'].encode('utf-8'))) + print(u'Text: {}'.format(text)) + print(u'Translation: {}'.format(translation['translatedText'])) # [END translate_quickstart] diff --git a/translate/cloud-client/quickstart_test.py b/translate/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..5914cb5a9dc --- /dev/null +++ b/translate/cloud-client/quickstart_test.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from google.cloud import translate +import mock +import pytest + +import quickstart + + +@pytest.fixture +def mock_client(cloud_config): + original_client_ctor = translate.Client + + def new_client_ctor(api_key): + # Strip api_key argument and replace with our api key. + return original_client_ctor(cloud_config.api_key) + + with mock.patch( + 'google.cloud.translate.Client', + side_effect=new_client_ctor): + yield + + +def test_quickstart(mock_client, capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert u'Привет мир!' in out diff --git a/vision/cloud-client/quickstart.py b/vision/cloud-client/quickstart.py index 68348799180..0453587fe9e 100644 --- a/vision/cloud-client/quickstart.py +++ b/vision/cloud-client/quickstart.py @@ -18,6 +18,7 @@ def run_quickstart(): # [START vision_quickstart] import io + import os # Imports the Google Cloud client library from google.cloud import vision @@ -26,11 +27,15 @@ def run_quickstart(): vision_client = vision.Client() # The name of the image file to annotate - fileName = './resources/wakeupcat.jpg' + file_name = os.path.join( + os.path.dirname(__file__), + 'resources/wakeupcat.jpg') # Loads the image into memory - with io.open(fileName, 'rb') as image_file: - image = vision_client.image(content=image_file.read()) + with io.open(file_name, 'rb') as image_file: + content = image_file.read() + image = vision_client.image( + content=content) # Performs label detection on the image file labels = image.detect_labels() diff --git a/vision/cloud-client/quickstart_test.py b/vision/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..d483d4131f7 --- /dev/null +++ b/vision/cloud-client/quickstart_test.py @@ -0,0 +1,21 @@ +# Copyright 2016 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import quickstart + + +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'Labels' in out