Skip to content

Commit 4c639cc

Browse files
author
Jon Wayne Parrott
authored
Quickstart tests (#569)
* Add tests for quickstarts * Update secrets
1 parent c969975 commit 4c639cc

File tree

15 files changed

+265
-12
lines changed

15 files changed

+265
-12
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ env:
1616
- GOOGLE_APPLICATION_CREDENTIALS=${TRAVIS_BUILD_DIR}/testing/resources/service-account.json
1717
- GOOGLE_CLIENT_SECRETS=${TRAVIS_BUILD_DIR}/testing/resources/client-secrets.json
1818
- GAE_ROOT=${HOME}/.cache/
19-
- secure: TQ0e6XKeFwVkoqgOJH9f/afyRouUSC6s7LC32C4JS+O2X4sXyXTPXACmzu5wCW0BXPc6HvITMLvkf7g6XXyGlCPkjM8Uw5Vg5F9+cwN1HMlI+gK6bMGTUfrwN5ruFT+KmEnD4F93NY3xkDbZd0fw23d/mVloTc6V0gUsxEUkuhM=
19+
- secure: f3aU0nf8ZBV2QfZ03oeqvR0f/JM69P/7IH3IGoBcRUWVIXXhQ6Esh9SmCUILPtis1ZKu11I9c+NDebZio7PFgTqfvLbKzAkrg0ucx+Bsyx6379/S1trbLeKunERSGA3GqK6+OCoR5q/9sKxNvlm/c/e9h7xZmPfP5W0qwVR/K0M=
2020
addons:
2121
apt:
2222
sources:
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import bigquery
16+
import pytest
17+
18+
import quickstart
19+
20+
21+
# Must match the dataset listed in quickstart.py (there's no easy way to
22+
# extract this).
23+
DATASET_ID = 'my_new_dataset'
24+
25+
26+
@pytest.fixture
27+
def temporary_dataset():
28+
"""Fixture that ensures the test dataset does not exist before or
29+
after a test."""
30+
bigquery_client = bigquery.Client()
31+
dataset = bigquery_client.dataset(DATASET_ID)
32+
33+
if dataset.exists():
34+
dataset.delete()
35+
36+
yield
37+
38+
if dataset.exists():
39+
dataset.delete()
40+
41+
42+
def test_quickstart(capsys, temporary_dataset):
43+
quickstart.run_quickstart()
44+
out, _ = capsys.readouterr()
45+
assert DATASET_ID in out

conftest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def cloud_config():
3030
project=os.environ.get('GCLOUD_PROJECT'),
3131
storage_bucket=os.environ.get('CLOUD_STORAGE_BUCKET'),
3232
client_secrets=os.environ.get('GOOGLE_CLIENT_SECRETS'),
33-
bigtable_instance=os.environ.get('BIGTABLE_CLUSTER'))
33+
bigtable_instance=os.environ.get('BIGTABLE_CLUSTER'),
34+
api_key=os.environ.get('API_KEY'))
3435

3536

3637
def get_resource_path(resource, local_path):

datastore/api/quickstart_test.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import quickstart
16+
17+
18+
def test_quickstart(capsys):
19+
quickstart.run_quickstart()
20+
out, _ = capsys.readouterr()
21+
assert 'Saved' in out

datastore/api/snippets.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ def inequality_invalid(client):
539539

540540
return list(query.fetch())
541541

542-
except google.cloud.exceptions.BadRequest:
542+
except (google.cloud.exceptions.BadRequest,
543+
google.cloud.exceptions.GrpcRendezvous):
543544
pass
544545

545546

@@ -579,7 +580,8 @@ def inequality_sort_invalid_not_same(client):
579580

580581
return list(query.fetch())
581582

582-
except google.cloud.exceptions.BadRequest:
583+
except (google.cloud.exceptions.BadRequest,
584+
google.cloud.exceptions.GrpcRendezvous):
583585
pass
584586

585587

@@ -593,7 +595,8 @@ def inequality_sort_invalid_not_first(client):
593595

594596
return list(query.fetch())
595597

596-
except google.cloud.exceptions.BadRequest:
598+
except (google.cloud.exceptions.BadRequest,
599+
google.cloud.exceptions.GrpcRendezvous):
597600
pass
598601

599602

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import quickstart
17+
18+
19+
def test_quickstart(capsys):
20+
quickstart.run_quickstart()
21+
out, _ = capsys.readouterr()
22+
assert 'Sentiment' in out
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import quickstart
17+
18+
19+
def test_quickstart(capsys):
20+
quickstart.run_quickstart()
21+
out, _ = capsys.readouterr()
22+
assert 'Logged' in out
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import pubsub
16+
import pytest
17+
18+
import quickstart
19+
20+
21+
# Must match the dataset listed in quickstart.py (there's no easy way to
22+
# extract this).
23+
TOPIC_NAME = 'my-new-topic'
24+
25+
26+
@pytest.fixture
27+
def temporary_topic():
28+
"""Fixture that ensures the test dataset does not exist before or
29+
after a test."""
30+
pubsub_client = pubsub.Client()
31+
topic = pubsub_client.topic(TOPIC_NAME)
32+
33+
if topic.exists():
34+
topic.delete()
35+
36+
yield
37+
38+
if topic.exists():
39+
topic.delete()
40+
41+
42+
def test_quickstart(capsys, temporary_topic):
43+
quickstart.run_quickstart()
44+
out, _ = capsys.readouterr()
45+
assert TOPIC_NAME in out

secrets.tar.enc

-512 Bytes
Binary file not shown.
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import mock
16+
17+
import quickstart
18+
19+
20+
@mock.patch('google.cloud.storage.client.Client.create_bucket')
21+
def test_quickstart(create_bucket_mock, capsys):
22+
# Unlike other quickstart tests, this one mocks out the creation
23+
# because buckets are expensive, globally-namespaced object.
24+
create_bucket_mock.return_value = mock.sentinel.bucket
25+
26+
quickstart.run_quickstart()
27+
28+
create_bucket_mock.assert_called_with('my-new-bucket')

testing/resources/test-env.tmpl.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Environment variables for system tests.
22
export GCLOUD_PROJECT=your-project-id
33
export CLOUD_STORAGE_BUCKET=$GCLOUD_PROJECT
4+
export API_KEY=
45
export BIGTABLE_CLUSTER=bigtable-test
56
export BIGTABLE_ZONE=us-central1-c
67

translate/cloud-client/quickstart.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@ def run_quickstart():
2727
translate_client = translate.Client(api_key)
2828

2929
# The text to translate
30-
text = 'Hello, world!'
30+
text = u'Hello, world!'
3131
# The target language
3232
target = 'ru'
3333

3434
# Translates some text into Russian
3535
translation = translate_client.translate(text, target_language=target)
3636

37-
print('Text: {}'.format(text))
38-
print('Translation: {}'.format(
39-
translation['translatedText'].encode('utf-8')))
37+
print(u'Text: {}'.format(text))
38+
print(u'Translation: {}'.format(translation['translatedText']))
4039
# [END translate_quickstart]
4140

4241

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2016 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
from google.cloud import translate
17+
import mock
18+
import pytest
19+
20+
import quickstart
21+
22+
23+
@pytest.fixture
24+
def mock_client(cloud_config):
25+
original_client_ctor = translate.Client
26+
27+
def new_client_ctor(api_key):
28+
# Strip api_key argument and replace with our api key.
29+
return original_client_ctor(cloud_config.api_key)
30+
31+
with mock.patch(
32+
'google.cloud.translate.Client',
33+
side_effect=new_client_ctor):
34+
yield
35+
36+
37+
def test_quickstart(mock_client, capsys):
38+
quickstart.run_quickstart()
39+
out, _ = capsys.readouterr()
40+
assert u'Привет мир!' in out

vision/cloud-client/quickstart.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
def run_quickstart():
1919
# [START vision_quickstart]
2020
import io
21+
import os
2122

2223
# Imports the Google Cloud client library
2324
from google.cloud import vision
@@ -26,11 +27,15 @@ def run_quickstart():
2627
vision_client = vision.Client()
2728

2829
# The name of the image file to annotate
29-
fileName = './resources/wakeupcat.jpg'
30+
file_name = os.path.join(
31+
os.path.dirname(__file__),
32+
'resources/wakeupcat.jpg')
3033

3134
# Loads the image into memory
32-
with io.open(fileName, 'rb') as image_file:
33-
image = vision_client.image(content=image_file.read())
35+
with io.open(file_name, 'rb') as image_file:
36+
content = image_file.read()
37+
image = vision_client.image(
38+
content=content)
3439

3540
# Performs label detection on the image file
3641
labels = image.detect_labels()
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import quickstart
16+
17+
18+
def test_quickstart(capsys):
19+
quickstart.run_quickstart()
20+
out, _ = capsys.readouterr()
21+
assert 'Labels' in out

0 commit comments

Comments
 (0)