Skip to content

Commit 4c639cc

Browse files
author
Jon Wayne Parrott
authored
Quickstart tests (GoogleCloudPlatform#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

Lines changed: 1 addition & 1 deletion
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:
Lines changed: 45 additions & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 21 additions & 0 deletions
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

Lines changed: 6 additions & 3 deletions
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

Lines changed: 22 additions & 0 deletions
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
Lines changed: 22 additions & 0 deletions
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
Lines changed: 45 additions & 0 deletions
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.
Lines changed: 28 additions & 0 deletions
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')

0 commit comments

Comments
 (0)